1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
#!/bin/bash
# nim.sh: Game of Nim
# Author: Mendel Cooper
# Reldate: 15 July 2008
# License: GPL3
ROWS=5 # Five rows of pegs (or matchsticks).
WON=91 # Exit codes to keep track of wins/losses.
LOST=92 # Possibly useful if running in batch mode.
QUIT=99
peg_msg= # Peg/Pegs?
Rows=( 0 5 4 3 2 1 ) # Array holding play info.
# ${Rows[0]} holds total number of pegs, updated after each turn.
# Other array elements hold number of pegs in corresponding row.
instructions ()
{
clear
tput bold
echo "Welcome to the game of Nim."; echo
echo -n "Do you need instructions? (y/n) "; read ans
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
clear
echo -e '\E[33;41m' # Yellow fg., over red bg.; bold.
cat <<INSTRUCTIONS
Nim is a game with roots in the distant past.
This particular variant starts with five rows of pegs.
1: | | | | |
2: | | | |
3: | | |
4: | |
5: |
The number at the left identifies the row.
The human player moves first, and alternates turns with the bot.
A turn consists of removing at least one peg from a single row.
It is permissable to remove ALL the pegs from a row.
For example, in row 2, above, the player can remove 1, 2, 3, or 4 pegs.
The player who removes the last peg loses.
The strategy consists of trying to be the one who removes
the next-to-last peg(s), leaving the loser with the final peg.
To exit the game early, hit ENTER during your turn.
INSTRUCTIONS
echo; echo -n "Hit ENTER to begin game. "; read azx
echo -e "\033[0m" # Restore display.
else tput sgr0; clear
fi
clear
}
tally_up ()
{
let "Rows[0] = ${Rows[1]} + ${Rows[2]} + ${Rows[3]} + ${Rows[4]} + \
${Rows[5]}" # Add up how many pegs remaining.
}
display ()
{
index=1 # Start with top row.
echo
while [ "$index" -le "$ROWS" ]
do
p=${Rows[index]}
echo -n "$index: " # Show row number.
# ------------------------------------------------
# Two concurrent inner loops.
indent=$index
while [ "$indent" -gt 0 ]
do
echo -n " " # Staggered rows.
((indent--)) # Spacing between pegs.
done
while [ "$p" -gt 0 ]
do
echo -n "| "
((p--))
done
# -----------------------------------------------
echo
((index++))
done
tally_up
rp=${Rows[0]}
if [ "$rp" -eq 1 ]
then
peg_msg=peg
final_msg="Game over."
else # Game not yet over . . .
peg_msg=pegs
final_msg="" # . . . So "final message" is blank.
fi
echo " $rp $peg_msg remaining."
echo " "$final_msg""
echo
}
player_move ()
{
echo "Your move:"
echo -n "Which row? "
while read idx
do # Validity check, etc.
if [ -z "$idx" ] # Hitting return quits.
then
echo "Premature exit."; echo
tput sgr0 # Restore display.
exit $QUIT
fi
if [ "$idx" -gt "$ROWS" -o "$idx" -lt 1 ] # Bounds check.
then
echo "Invalid row number!"
echo -n "Which row? "
else
break
fi
# TODO:
# Add check for non-numeric input.
# Also, script crashes on input outside of range of long double.
# Fix this.
done
echo -n "Remove how many? "
while read num
do # Validity check.
if [ -z "$num" ]
then
echo "Premature exit."; echo
tput sgr0 # Restore display.
exit $QUIT
fi
if [ "$num" -gt ${Rows[idx]} -o "$num" -lt 1 ]
then
echo "Cannot remove $num!"
echo -n "Remove how many? "
else
break
fi
done
# TODO:
# Add check for non-numeric input.
# Also, script crashes on input outside of range of long double.
# Fix this.
let "Rows[idx] -= $num"
display
tally_up
if [ ${Rows[0]} -eq 1 ]
then
echo " Human wins!"
echo " Congratulations!"
tput sgr0 # Restore display.
echo
exit $WON
fi
if [ ${Rows[0]} -eq 0 ]
then # Snatching defeat from the jaws of victory . . .
echo " Fool!"
echo " You just removed the last peg!"
echo " Bot wins!"
tput sgr0 # Restore display.
echo
exit $LOST
fi
}
bot_move ()
{
row_b=0
while [[ $row_b -eq 0 || ${Rows[row_b]} -eq 0 ]]
do
row_b=$RANDOM # Choose random row.
let "row_b %= $ROWS"
done
num_b=0
r0=${Rows[row_b]}
if [ "$r0" -eq 1 ]
then
num_b=1
else
let "num_b = $r0 - 1"
# Leave only a single peg in the row.
fi # Not a very strong strategy,
#+ but probably a bit better than totally random.
let "Rows[row_b] -= $num_b"
echo -n "Bot: "
echo "Removing from row $row_b ... "
if [ "$num_b" -eq 1 ]
then
peg_msg=peg
else
peg_msg=pegs
fi
echo " $num_b $peg_msg."
display
tally_up
if [ ${Rows[0]} -eq 1 ]
then
echo " Bot wins!"
tput sgr0 # Restore display.
exit $WON
fi
}
# ================================================== #
instructions # If human player needs them . . .
tput bold # Bold characters for easier viewing.
display # Show game board.
while [ true ] # Main loop.
do # Alternate human and bot turns.
player_move
bot_move
done
# ================================================== #
# Exercise:
# --------
# Improve the bot's strategy.
# There is, in fact, a Nim strategy that can force a win.
# See the Wikipedia article on Nim: http://en.wikipedia.org/wiki/Nim
# Recode the bot to use this strategy (rather difficult).
# Curiosities:
# -----------
# Nim played a prominent role in Alain Resnais' 1961 New Wave film,
#+ Last Year at Marienbad.
#
# In 1978, Leo Christopherson wrote an animated version of Nim,
#+ Android Nim, for the TRS-80 Model I.
|