#!/bin/bash

#10 PRINT TAB(21);"GAME OF ROCK, SCISSORS, PAPER"
#20 PRINT TAB(15);"CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
#25 PRINT:PRINT:PRINT
printf "%*s GAME OF ROCK, SCISSORS, PAPER\n" 21
printf "%*s CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY\n" 15
echo ; echo

#30 INPUT "HOW MANY GAMES";Q
#40 IF Q<11 THEN 60
#50 PRINT "SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY.": GOTO 30
while true; do
	printf "HOW MANY GAMES "
	read NUMBER_OF_GAMES
	[ $NUMBER_OF_GAMES -ge 11 ] || break;
	echo "SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY."
done

COMPUTER_WINS=0
HUMAN_WINS=0
TIES=0

#60 FOR G=1 TO Q
for GAME_NUMBER in $( seq 1 $NUMBER_OF_GAMES )
do
	#70 PRINT: PRINT "GAME NUMBER";G
	echo
	printf "GAME NUMBER %s\n" $GAME_NUMBER

	#80 X=INT(RND(1)*3+1)
	COMPUTER_PICK=$((RANDOM%3+1))

	#90 PRINT "3=ROCK...2=SCISSORS...1=PAPER"
	#100 INPUT "1...2...3...WHAT'S YOUR CHOICE";K
	#110 IF (K-1)*(K-2)*(K-3)<>0 THEN PRINT "INVALID.": GOTO 90
	while true; do
		echo "3=ROCK...2=SCISSORS...1=PAPER"
		printf "1...2...3...WHAT'S YOUR CHOICE "
		read HUMAN_PICK
		[ $(( (HUMAN_PICK-1)*(HUMAN_PICK-2)*(HUMAN_PICK-3) )) -eq 0 ] && break;
		echo "INVALID."
	done

	
	#120 PRINT "THIS IS MY CHOICE..."
	#130 ON X GOTO 140,150,160
	#140 PRINT "...PAPER": GOTO 170
	#150 PRINT "...SCISSORS": GOTO 170
	#160 PRINT "...ROCK"
	printf "THIS IS MY CHOICE..."
	if [ $COMPUTER_PICK -eq 1 ]; then
		echo "...PAPER"
	elif [ $COMPUTER_PICK -eq 2 ]; then
		echo "...SCISSORS"
	else
		echo "...ROCK"
	fi

	#170 IF X=K THEN 250
	#180 IF X>K THEN 230
	#190 IF X=1 THEN 210
	#200 PRINT "YOU WIN!!!":H=H+1: GOTO 260
	#210 IF K<>3 THEN 200
	#220 PRINT "WOW!  I WIN!!!":C=C+1:GOTO 260
	#230 IF K<>1 OR X<>3 THEN 220
	#240 GOTO 200
	#250 PRINT "TIE GAME.  NO WINNER."
	#260 NEXT G
	if [ $COMPUTER_PICK -eq $HUMAN_PICK ]; then
		echo "TIE GAME.  NO WINNER."
		((TIES+=1))
		continue
	elif ([ $COMPUTER_PICK -gt $HUMAN_PICK ] && ([ $COMPUTER_PICK -ne 3 ] || [ $HUMAN_PICK -ne 1 ])) \
		|| ([ $COMPUTER_PICK -eq 1 ] && [ $HUMAN_PICK -eq 3 ]); then
		echo "WOW!  I WIN!!!"
		((COMPUTER_WINS+=1))
		continue
	else
		echo "YOU WIN!!!"
		((HUMAN_WINS+=1))
		continue
	fi
done


#270 PRINT: PRINT "HERE IS THE FINAL GAME SCORE:"
#280 PRINT "I HAVE WON";C;"GAME(S)."
#290 PRINT "YOU HAVE WON";H;"GAME(S)."
#300 PRINT "AND";Q-(C+H);"GAME(S) ENDED IN A TIE."
#310 PRINT: PRINT "THANKS FOR PLAYING!!"
echo
echo "HERE IS THE FINAL GAME SCORE:"
printf "I HAVE WON %d GAME(S).\n" $COMPUTER_WINS
printf "YOU HAVE WON %d GAME(S).\n" $HUMAN_WINS
printf "AND %d GAME(S) ENDED IN A TIE.\n" $TIES
echo
echo "THANKS FOR PLAYING!!"

#320 END
