#!/bin/mksh
#  dice.ksh - simulates rolling a pair of dice
#

header() {
echo
echo " Welcome to Kornshell Dice!"
echo "   ⚀  ⚁  :  ⚂  ⚃  :  ⚄  ⚅  "
}

usage() {
echo '
  usage:  dice.sh [-h|-p]|[n [t]]

      -h = prints dice.sh usage info
      -p = pauses between rolls; continuous if n omitted
       n = # of rolls of dice desired
       t = secs between rolls (2 default)
'
}

die() {
case $1 in
  1) printf '\n\t+-------+\n\t|       |\n\t|   0   |\n\t|       |\n\t+-------+\n' ;;
  2) printf '\n\t+-------+\n\t|     0 |\n\t|       |\n\t| 0     |\n\t+-------+\n' ;;
  3) printf '\n\t+-------+\n\t|     0 |\n\t|   0   |\n\t| 0     |\n\t+-------+\n' ;;
  4) printf '\n\t+-------+\n\t| 0   0 |\n\t|       |\n\t| 0   0 |\n\t+-------+\n' ;;
  5) printf '\n\t+-------+\n\t| 0   0 |\n\t|   0   |\n\t| 0   0 |\n\t+-------+\n' ;;
  6) printf '\n\t+-------+\n\t| 0   0 |\n\t| 0   0 |\n\t| 0   0 |\n\t+-------+\n' ;;
esac
}

diceterms() {
case "$(($1 + $2))" in
           2)  printf '\n\t=> Snake Eyes !! \n\n' ;;
           3)  printf '\n\t=> Ace caught a Duce !! \n\n' ;;
           5)  printf '\n\t=> Fever Five !! \n\n' ;;
           7)  printf '\n\t=> Big Red !! \n\n' ;;
  [4,6,8]|10)  (($1 != $2)) && printf "\n\t$(($1+$2)) the Easy Way !!\n\n"
               (($1 == $2)) && printf "\n\t$(($1+$2)) the Hard Way !!\n\n" ;;
           9)  printf '\n\t=> Center Field Nine !! \n\n' ;;
          11)  printf '\n\t=> Yo-Leven !! \n\n' ;;
          12)  printf '\n\t=> Box Cars !! \n\n' ;;
esac
}

roll() {
cnt=0
set -A Die
while (( $cnt < 2 )) ;do
  n=$((RANDOM/1000))
  if (($n > 0)) && (($n < 7)) ;then
    Die[$cnt]=$n
    die $n
    cnt=$((cnt+1))
  fi
done
diceterms ${Die[@]}
echo
}

pause() {
N=1
while [[ "$REPLY" != [qQ] ]] ;do
  clear
  header
  printf "\n  roll #$N:\n"
  roll && N=$((N+1))
  printf 'press ANY KEY to continue ; Q to quit\n\n'
  read -n1
  [[ "$REPLY" == [qQ] ]] || (($N > $1)) && printf '' && return 0
done
}


# main
case "$1" in
  [0-9]*)  N=1
           until (( $N > $1 )) ;do
             clear
             header
             printf "\n  roll #$N:\n"
             roll && N=$((N+1))
             t=${2:-2}
             sleep $t
           done  ;;
      -p)  pause $2 ;;
    -h|*)  usage ;;
esac