Skip to content

Instantly share code, notes, and snippets.

@AnakinTrotter
Last active April 10, 2022 23:33
Show Gist options
  • Save AnakinTrotter/f0976b009291cb6d28d73e5fa19143e0 to your computer and use it in GitHub Desktop.
Save AnakinTrotter/f0976b009291cb6d28d73e5fa19143e0 to your computer and use it in GitHub Desktop.
#!/bin/bash
player_wins=0
ai_wins=0
ties=0
while true; do
echo ""
echo "Rock, paper, scissors, or quit (r, p, s, q): "
read user_input
player_move=-1
[ "$user_input" == "r" ] && player_move="ROCK"
[ "$user_input" == "p" ] && player_move="PAPER"
[ "$user_input" == "s" ] && player_move="SCISSORS"
[ "$user_input" == "q" ] && break
[ $player_move != -1 ] || continue
ai_input=$(($RANDOM % 3))
[ $ai_input -eq 0 ] && ai_move="ROCK"
[ $ai_input -eq 1 ] && ai_move="PAPER"
[ $ai_input -eq 2 ] && ai_move="SCISSORS"
echo "You played: ${player_move}"
echo "Opponent played: ${ai_move}"
if [[ $player_move == $ai_move ]]; then
echo "It's a tie!"
((ties++))
else
player_won=1
if [ $player_move == "ROCK" ] && [ $ai_move == "PAPER" ]; then
player_won=0
elif [ $player_move == "PAPER" ] && [ $ai_move == "SCISSORS" ]; then
player_won=0
elif [ $player_move == "SCISSORS" ] && [ $ai_move == "ROCK" ]; then
player_won=0
fi
[ $player_won -eq 1 ] && echo "You win!" && ((player_wins++))
[ $player_won -eq 0 ] && echo "You lose!" && ((ai_wins++))
fi
done
echo ""
echo "Session results:" && echo "Session results:" >> results.txt
echo "Player wins: ${player_wins}" && echo "Player wins: ${player_wins}" >> results.txt
echo "Computer wins: ${ai_wins}" && echo "Computer wins: ${ai_wins}" >> results.txt
echo "Ties: ${ties}" && echo "Ties: ${ties}" >> results.txt
echo "" >> results.txt
echo ""
echo "Press enter to exit"
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment