| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
ROWS=5 |
| 9 |
WON=91 |
| 10 |
LOST=92 |
| 11 |
QUIT=99 |
| 12 |
peg_msg= |
| 13 |
Rows=( 0 5 4 3 2 1 ) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
instructions () |
| 18 |
{ |
| 19 |
clear |
| 20 |
tput bold |
| 21 |
echo "Welcome to the game of Nim."; echo |
| 22 |
echo -n "Do you need instructions? (y/n) "; read ans |
| 23 |
|
| 24 |
if [ "$ans" = "y" -o "$ans" = "Y" ]; then |
| 25 |
clear |
| 26 |
echo -e '\E[33;41m' |
| 27 |
cat <<INSTRUCTIONS |
| 28 |
|
| 29 |
Nim is a game with roots in the distant past. |
| 30 |
This particular variant starts with five rows of pegs. |
| 31 |
|
| 32 |
1: | | | | | |
| 33 |
2: | | | | |
| 34 |
3: | | | |
| 35 |
4: | | |
| 36 |
5: | |
| 37 |
|
| 38 |
The number at the left identifies the row. |
| 39 |
|
| 40 |
The human player moves first, and alternates turns with the bot. |
| 41 |
A turn consists of removing at least one peg from a single row. |
| 42 |
It is permissable to remove ALL the pegs from a row. |
| 43 |
For example, in row 2, above, the player can remove 1, 2, 3, or 4 pegs. |
| 44 |
The player who removes the last peg loses. |
| 45 |
|
| 46 |
The strategy consists of trying to be the one who removes |
| 47 |
the next-to-last peg(s), leaving the loser with the final peg. |
| 48 |
|
| 49 |
To exit the game early, hit ENTER during your turn. |
| 50 |
INSTRUCTIONS |
| 51 |
|
| 52 |
echo; echo -n "Hit ENTER to begin game. "; read azx |
| 53 |
|
| 54 |
echo -e "\033[0m" |
| 55 |
else tput sgr0; clear |
| 56 |
fi |
| 57 |
|
| 58 |
clear |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
tally_up () |
| 64 |
{ |
| 65 |
let "Rows[0] = ${Rows[1]} + ${Rows[2]} + ${Rows[3]} + ${Rows[4]} + \ |
| 66 |
${Rows[5]}" |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
display () |
| 71 |
{ |
| 72 |
index=1 |
| 73 |
echo |
| 74 |
|
| 75 |
while [ "$index" -le "$ROWS" ] |
| 76 |
do |
| 77 |
p=${Rows[index]} |
| 78 |
echo -n "$index: " |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
indent=$index |
| 84 |
while [ "$indent" -gt 0 ] |
| 85 |
do |
| 86 |
echo -n " " |
| 87 |
((indent--)) |
| 88 |
done |
| 89 |
|
| 90 |
while [ "$p" -gt 0 ] |
| 91 |
do |
| 92 |
echo -n "| " |
| 93 |
((p--)) |
| 94 |
done |
| 95 |
|
| 96 |
|
| 97 |
echo |
| 98 |
((index++)) |
| 99 |
done |
| 100 |
|
| 101 |
tally_up |
| 102 |
|
| 103 |
rp=${Rows[0]} |
| 104 |
|
| 105 |
if [ "$rp" -eq 1 ] |
| 106 |
then |
| 107 |
peg_msg=peg |
| 108 |
final_msg="Game over." |
| 109 |
else |
| 110 |
peg_msg=pegs |
| 111 |
final_msg="" |
| 112 |
fi |
| 113 |
|
| 114 |
echo " $rp $peg_msg remaining." |
| 115 |
echo " "$final_msg"" |
| 116 |
|
| 117 |
|
| 118 |
echo |
| 119 |
} |
| 120 |
|
| 121 |
player_move () |
| 122 |
{ |
| 123 |
|
| 124 |
echo "Your move:" |
| 125 |
|
| 126 |
echo -n "Which row? " |
| 127 |
while read idx |
| 128 |
do |
| 129 |
|
| 130 |
if [ -z "$idx" ] |
| 131 |
then |
| 132 |
echo "Premature exit."; echo |
| 133 |
tput sgr0 |
| 134 |
exit $QUIT |
| 135 |
fi |
| 136 |
|
| 137 |
if [ "$idx" -gt "$ROWS" -o "$idx" -lt 1 ] |
| 138 |
then |
| 139 |
echo "Invalid row number!" |
| 140 |
echo -n "Which row? " |
| 141 |
else |
| 142 |
break |
| 143 |
fi |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
done |
| 150 |
|
| 151 |
echo -n "Remove how many? " |
| 152 |
while read num |
| 153 |
do |
| 154 |
|
| 155 |
if [ -z "$num" ] |
| 156 |
then |
| 157 |
echo "Premature exit."; echo |
| 158 |
tput sgr0 |
| 159 |
exit $QUIT |
| 160 |
fi |
| 161 |
|
| 162 |
if [ "$num" -gt ${Rows[idx]} -o "$num" -lt 1 ] |
| 163 |
then |
| 164 |
echo "Cannot remove $num!" |
| 165 |
echo -n "Remove how many? " |
| 166 |
else |
| 167 |
break |
| 168 |
fi |
| 169 |
done |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
let "Rows[idx] -= $num" |
| 176 |
|
| 177 |
display |
| 178 |
tally_up |
| 179 |
|
| 180 |
if [ ${Rows[0]} -eq 1 ] |
| 181 |
then |
| 182 |
echo " Human wins!" |
| 183 |
echo " Congratulations!" |
| 184 |
tput sgr0 |
| 185 |
echo |
| 186 |
exit $WON |
| 187 |
fi |
| 188 |
|
| 189 |
if [ ${Rows[0]} -eq 0 ] |
| 190 |
then |
| 191 |
echo " Fool!" |
| 192 |
echo " You just removed the last peg!" |
| 193 |
echo " Bot wins!" |
| 194 |
tput sgr0 |
| 195 |
echo |
| 196 |
exit $LOST |
| 197 |
fi |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
bot_move () |
| 202 |
{ |
| 203 |
|
| 204 |
row_b=0 |
| 205 |
while [[ $row_b -eq 0 || ${Rows[row_b]} -eq 0 ]] |
| 206 |
do |
| 207 |
row_b=$RANDOM |
| 208 |
let "row_b %= $ROWS" |
| 209 |
done |
| 210 |
|
| 211 |
|
| 212 |
num_b=0 |
| 213 |
r0=${Rows[row_b]} |
| 214 |
|
| 215 |
if [ "$r0" -eq 1 ] |
| 216 |
then |
| 217 |
num_b=1 |
| 218 |
else |
| 219 |
let "num_b = $r0 - 1" |
| 220 |
|
| 221 |
fi |
| 222 |
|
| 223 |
|
| 224 |
let "Rows[row_b] -= $num_b" |
| 225 |
echo -n "Bot: " |
| 226 |
echo "Removing from row $row_b ... " |
| 227 |
|
| 228 |
if [ "$num_b" -eq 1 ] |
| 229 |
then |
| 230 |
peg_msg=peg |
| 231 |
else |
| 232 |
peg_msg=pegs |
| 233 |
fi |
| 234 |
|
| 235 |
echo " $num_b $peg_msg." |
| 236 |
|
| 237 |
display |
| 238 |
tally_up |
| 239 |
|
| 240 |
if [ ${Rows[0]} -eq 1 ] |
| 241 |
then |
| 242 |
echo " Bot wins!" |
| 243 |
tput sgr0 |
| 244 |
exit $WON |
| 245 |
fi |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
instructions |
| 252 |
tput bold |
| 253 |
display |
| 254 |
|
| 255 |
while [ true ] |
| 256 |
do |
| 257 |
player_move |
| 258 |
bot_move |
| 259 |
done |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|