Sample Code (rps.sh)
cpw@courtwilson.com
#!/bin/sh
# Rock Paper Scissors Player
# to use, either type "chmod +x rps.sh" and then "./rps.sh", or just "sh rps.sh"
# this program DOES NOT cheat
RPS_MEM=("./rps.mem")
# checks to see if mem file exists
# if it does, data is loaded
# if it does not then mem file is created
if test -f "$RPS_MEM"
then
r=$(sed -n 1p "$RPS_MEM")
p=$(sed -n 2p "$RPS_MEM")
s=$(sed -n 3p "$RPS_MEM")
score_you=$(sed -n 4p "$RPS_MEM")
score_me=$(sed -n 5p "$RPS_MEM")
else
r=1 && p=1 && s=1 && score_you=0 && score_me=0 && touch "$RPS_MEM"
&& echo -e "$r\n$p\n$s\n$score_you\n$score_me\n" > "$RPS_MEM"
fi
# repeatedly prompts for next round of input
# if 'q' then quits program
while echo "SCORE: (You: $score_you, Me: $score_me)" && echo -e "\n" && echo "Input (r=rock,
p=paper, s=scissors, n=new, q=quit):" && read l && [[ "$l" == "r" ]] || [[ "$l" == "p" ]]
|| [[ "$l" == "s" ]] || [[ "$l" == "n" ]]
do
# if 'n' then wipes the mem file
if [[ "$l" == "n" ]]
then
r=1 && p=1 && s=1 && n_r=0 && n_p=0 && n_s=0 && score_me=0 && score_you=0
echo "New Game Started!"
else
# three inputs allowed: 'r' (rock), 'p' (paper), or 's' (scissors)
if [[ "$l" == "r" ]]
then
n_r=1 && n_p=0 && n_s=0
echo "YOU: rock"
elif [[ "$l" == "p" ]]
then
n_r=0 && n_p=1 && n_s=0
echo "YOU: paper"
elif [[ "$l" == "s" ]]
then
n_r=0 && n_p=0 && n_s=1
echo "YOU: scissors"
else
echo "ERROR! Try Again"
fi
# prepares to make an r/p/s guess using mem file
t=$(($r + $p + $s))
# r/p/s probabilities calculated
prob_r=$(echo "$r/$t" | bc -l)
prob_p=$(echo "$p/$t" | bc -l)
prob_s=$(echo "$s/$t" | bc -l)
# r/p/s probabilities translated into integer sub-ranges
range_r=$(echo "$prob_r*32767" | bc -l)
range_p=$(echo "$prob_p*32767" | bc -l)
range_s=$(echo "$prob_s*32767" | bc -l)
# r/p/s ranges mapped to overall random integer range
limit_r=$(echo "$range_r" | bc -l)
limit_p=$(echo "$range_r+$range_p" | bc -l)
limit_s=$(echo "$range_r+$range_p+$range_s" | bc -l)
# ranges are rounded to the nearest integer
limit_int_r=$(printf "%.f" "$limit_r")
limit_int_p=$(printf "%.f" "$limit_p")
limit_int_s=$(printf "%.f" "$limit_s")
# random integer between 1 and 32767 is rolled
rand=$RANDOM
# if range is 'r', then 'p' is selected
# if range is 'p', then 's' is selected
# if range is 's', then 'r' is selected
if (( "$rand" <= "$limit_int_r" ))
then
roll="p"
echo "ME: paper"
elif (( "$rand" <= "$limit_int_p" ))
then
roll="s"
echo "ME: scissors"
elif (( "$rand" <= "$limit_int_s" ))
then
roll="r"
echo "ME: rock"
else
echo "ERROR! Try Again"
fi
# roll is compared to user input to calculate who (if anyone) won the round
if [[ "$l" == "r" ]]
then
if [[ "$roll" == "r" ]]
then
echo "DRAW"
elif [[ "$roll" == "p" ]]
then
echo "I WIN!"
score_me=$(echo "$score_me+1" | bc -l)
elif [[ "$roll" == "s" ]]
then
echo "YOU WIN!"
score_you=$(echo "$score_you+1" | bc -l)
fi
elif [[ "$l" == "p" ]]
then
if [[ "$roll" == "r" ]]
then
echo "YOU WIN!"
score_you=$(echo "$score_you+1" | bc -l)
elif [[ "$roll" == "p" ]]
then
echo "DRAW"
elif [[ "$roll" == "s" ]]
then
echo "I WIN!"
score_me=$(echo "$score_me+1" | bc -l)
fi
elif [[ "$l" == "s" ]]
then
if [[ "$roll" == "r" ]]
then
echo "I WIN!"
score_me=$(echo "$score_me+1" | bc -l)
elif [[ "$roll" == "p" ]]
then
echo "YOU WIN!"
score_you=$(echo "$score_you+1" | bc -l)
elif [[ "$roll" == "s" ]]
then
echo "DRAW"
fi
fi
# round is added to the overall score, which is displayed
fi
r=$(echo "$r+$n_r" | bc -l) && p=$(echo "$p+$n_p" | bc -l) && s=$(echo "$s+$n_s" | bc -l)
# mem file's data is updated
echo -e "$r\n$p\n$s\n$score_you\n$score_me\n" > "$RPS_MEM"
done