How to resolve the algorithm Rock-paper-scissors step by step in the UNIX Shell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Rock-paper-scissors step by step in the UNIX Shell programming language
Table of Contents
Problem Statement
Implement the classic children's game Rock-paper-scissors, as well as a simple predictive AI (artificial intelligence) player. Rock Paper Scissors is a two player game. Each player chooses one of rock, paper or scissors, without knowing the other player's choice. The winner is decided by a set of rules:
If both players choose the same thing, there is no winner for that round. For this task, the computer will be one of the players. The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.
Support additional choices additional weapons.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Rock-paper-scissors step by step in the UNIX Shell programming language
Source code in the unix programming language
#!/bin/bash
choices=(rock paper scissors)
# comparison function, works like Perl
# winner x y = 2 if y beats x, 1 if x beats 1, 0 if it's a tie
winner() {
local left="$1" right="$2"
echo $(( (3 + left - right) % 3 ))
}
human_counts=(1 1 1)
human_count=3
computer_counts=(0 0 0)
games=0 human=0 computer=0
PS3="What do you throw? "
while true; do
select choice in rock paper scissors quit; do
if [ -z "$choice" ]; then choice="$REPLY"; fi
if [ "$choice" = "quit" ]; then
break 2
fi
for (( h=0; h<${#choices[@]}; ++h )); do
if [ "${choices[h]}" = "$choice" ]; then
break
fi
done
if (( h < 3 )); then
break
fi
echo "Unrecognized choice. Try again!"
done
let n=RANDOM%human_count
for (( c=0; c<${#human_counts[@]}; ++c )); do
let n-=${human_counts[c]}
if (( n < 0 )); then
break
fi
done
let computer_counts[c]+=1
echo
echo "You chose ${choices[h]^^}"
echo "I chose ${choices[c]^^}"
w="$(winner "$c" "$h")"
case "$w" in
2) echo "YOU WIN!"; let human+=1;;
0) echo "TIE!";;
1) echo "I WIN!"; let computer+=1;;
*) echo "winner returned weird result '$w'";;
esac
echo
let games+=1
(( human_counts[(h+1)%3]+=1, human_count+=1 ))
done
echo
echo "We played $games games. You won $human, and I won $computer."
for (( i=0; i<3; ++i )); do
echo "You picked ${choices[i]} $(( human_counts[ (i+1)%3 ] - 1 )) times."
echo "I picked ${choices[i]} $(( computer_counts[i] )) times."
done
You may also check:How to resolve the algorithm Empty program step by step in the bootBASIC programming language
You may also check:How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Go programming language
You may also check:How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Partial function application step by step in the Clojure programming language