paperjam

rps.sh

May 25th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.41 KB | None | 0 0
  1. #!/bin/env /bin/bash
  2. # Rock Paper Scissors mt 20170525
  3.  
  4. declare -a op; declare -a oc; declare -A rs
  5.  
  6. op=("Rock" "Paper" "Scissors")
  7. oc=("WIN" "Defeat" "Draw")
  8.  
  9. rs[0,0]=${oc[2]}; rs[0,1]=${oc[1]}; rs[0,2]=${oc[0]}
  10. rs[1,0]=${oc[0]}; rs[1,1]=${oc[2]}; rs[1,2]=${oc[1]}
  11. rs[2,0]=${oc[1]}; rs[2,1]=${oc[0]}; rs[2,2]=${oc[2]}
  12.  
  13. cs=0; us=0; ns=0; rd=0
  14.  
  15. printf "Hello! Welcome to %s %s %s Game!\n" ${op[0]} ${op[1]} ${op[2]}
  16.  
  17. while true; do
  18.  
  19.   read -e -p "${op[0]}:1, ${op[1]}:2, ${op[2]}:3, Quit:0. What's your pick?: " ui
  20.  
  21.   case "${ui}" in
  22.     0)
  23.       if [ "$us" -gt "$cs" ] ; then
  24.         bbmsg="${oc[0]}"
  25.       elif [ "$us" -lt "$cs" ] ; then
  26.         bbmsg="got ${oc[1]}ed by"
  27.       elif [ "$us" -eq "$cs" ] ; then
  28.         bbmsg="${oc[2]}ed with"
  29.       fi
  30.  
  31.       printf "After %d rounds, you %s the CPU with %d:%d points and %d ties.\n" $rd "${bbmsg}" $us $cs $ns
  32.  
  33.       exit 0
  34.       ;;
  35.  
  36.     [1-3])
  37.       let "rd++"
  38.       let "ui = $ui - 1"
  39.  
  40.       ci=$(shuf -i 0-2 -n 1)
  41.  
  42.       printf "Round : %d is a %s. You selected %s, while the CPU rolled %s\n" $rd ${rs["${ui}","${ci}"]}  ${op["${ui}"]} ${op["${ci}"]}
  43.  
  44.      case ${rs["${ui}","${ci}"]} in
  45.         ${oc[0]}) let "us++";;
  46.         ${oc[1]}) let "cs++";;
  47.         ${oc[2]}) let "ns++";;
  48.       esac
  49.  
  50.       printf "Player : %d, CPU : %d, Ties : %d\n" $us $cs $ns
  51.       ;;
  52.     *)
  53.       printf "Choose again from 0 to 3\n"
  54.       ;;
  55.  
  56.   esac
  57.  
  58. done
Advertisement
Add Comment
Please, Sign In to add comment