Debian Codegraph @

nim.sh

raw symbols SCIP
1 #!/bin/bash
2 # nim.sh: Game of Nim
3
4 # Author: Mendel Cooper
5 # Reldate: 15 July 2008
6 # License: GPL3
7
8 ROWS=5 # Five rows of pegs (or matchsticks).
9 WON=91 # Exit codes to keep track of wins/losses.
10 LOST=92 # Possibly useful if running in batch mode.
11 QUIT=99
12 peg_msg= # Peg/Pegs?
13 Rows=( 0 5 4 3 2 1 ) # Array holding play info.
14 # ${Rows[0]} holds total number of pegs, updated after each turn.
15 # Other array elements hold number of pegs in corresponding row.
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' # Yellow fg., over red bg.; bold.
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" # Restore display.
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]}" # Add up how many pegs remaining.
67 }
68
69
70 display ()
71 {
72 index=1 # Start with top row.
73 echo
74
75 while [ "$index" -le "$ROWS" ]
76 do
77 p=${Rows[index]}
78 echo -n "$index: " # Show row number.
79
80 # ------------------------------------------------
81 # Two concurrent inner loops.
82
83 indent=$index
84 while [ "$indent" -gt 0 ]
85 do
86 echo -n " " # Staggered rows.
87 ((indent--)) # Spacing between pegs.
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 # Game not yet over . . .
110 peg_msg=pegs
111 final_msg="" # . . . So "final message" is blank.
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 # Validity check, etc.
129
130 if [ -z "$idx" ] # Hitting return quits.
131 then
132 echo "Premature exit."; echo
133 tput sgr0 # Restore display.
134 exit $QUIT
135 fi
136
137 if [ "$idx" -gt "$ROWS" -o "$idx" -lt 1 ] # Bounds check.
138 then
139 echo "Invalid row number!"
140 echo -n "Which row? "
141 else
142 break
143 fi
144 # TODO:
145 # Add check for non-numeric input.
146 # Also, script crashes on input outside of range of long double.
147 # Fix this.
148
149 done
150
151 echo -n "Remove how many? "
152 while read num
153 do # Validity check.
154
155 if [ -z "$num" ]
156 then
157 echo "Premature exit."; echo
158 tput sgr0 # Restore display.
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 # TODO:
171 # Add check for non-numeric input.
172 # Also, script crashes on input outside of range of long double.
173 # Fix this.
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 # Restore display.
185 echo
186 exit $WON
187 fi
188
189 if [ ${Rows[0]} -eq 0 ]
190 then # Snatching defeat from the jaws of victory . . .
191 echo " Fool!"
192 echo " You just removed the last peg!"
193 echo " Bot wins!"
194 tput sgr0 # Restore display.
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 # Choose random row.
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 # Leave only a single peg in the row.
221 fi # Not a very strong strategy,
222 #+ but probably a bit better than totally random.
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 # Restore display.
244 exit $WON
245 fi
246
247 }
248
249
250 # ================================================== #
251 instructions # If human player needs them . . .
252 tput bold # Bold characters for easier viewing.
253 display # Show game board.
254
255 while [ true ] # Main loop.
256 do # Alternate human and bot turns.
257 player_move
258 bot_move
259 done
260 # ================================================== #
261
262 # Exercise:
263 # --------
264 # Improve the bot's strategy.
265 # There is, in fact, a Nim strategy that can force a win.
266 # See the Wikipedia article on Nim: http://en.wikipedia.org/wiki/Nim
267 # Recode the bot to use this strategy (rather difficult).
268
269 # Curiosities:
270 # -----------
271 # Nim played a prominent role in Alain Resnais' 1961 New Wave film,
272 #+ Last Year at Marienbad.
273 #
274 # In 1978, Leo Christopherson wrote an animated version of Nim,
275 #+ Android Nim, for the TRS-80 Model I.