-
Notifications
You must be signed in to change notification settings - Fork 8
/
hangman
executable file
·371 lines (344 loc) · 7.39 KB
/
hangman
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash
#
#
# A HANGMAN GAME WRITTEN FOR THE BOURNE AGAIN SHELL!
#
# This game was coded for fun while I was trying to learn bash. To play the game
# ensure that this script has execute permissions and then type ./hangman in the
# terminal.
#
# This game reads a list of words from a dict.dat file in the same directory as
# the hangman script.
#
# Author: Ashton Seth Reimer
# Date: Summer 2010
# License: See the license file included in the repository on github:
# https://github.com/asreimer/bash_hangman
#
#initialize the variables and arrays we need
declare -a word
declare -a word_img
declare -a alpha_img
i=0
incorrect=0
wordindex=0
correct=0
alpha=("a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z")
char=0
#this function reads the list of words from the dict.dat file and
#put them into the word array
function readfile
{
exec 3<&0
exec 0< dict.dat
while read LINE
do
word[i]=$LINE
i=`expr $i + 1`
done
exec 0<&3
}
#this function randomly selects a word from the word array.
function readword {
word_index=$RANDOM
while [ $word_index -ge $i ]
do
word_index=$RANDOM
done
a=0
while [ $a -lt ${#word[${word_index}]} ]
do
word_img[$a]=0
a=`expr $a + 1`
done
}
#this function solicits a guess from the user and then checks
#the guess to see if it is valid/correct/incorrect.
function guess
{
j=0
correct=0
echo -n "Guess a letter: "
read guess
char=$guess
if [ ${#guess} -eq "1" ]
then
guess=`echo $guess | tr "[:upper:]" "[:lower:]"`
while [ $j -lt ${#word[${word_index}]} ]
do
if [ "$guess" == "${word[${word_index}]:$j:1}" ]
then
word_img[${j}]=1
correct=1
fi
j=`expr $j + 1`
done
fi
r=0
numletter=0
while [ ! $r == ${#word[${word_index}]} ]
do
numletter=`expr $numletter + ${word_img[$r]}`
r=`expr $r + 1`
done
}
#the following functions draw the gallows and hanged man to the terminal
function gallows
{
clear
echo " __________"
echo " | |"
echo " | |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " _____________|_____"
echo " "
}
function gallows_head
{
clear
echo " __________"
echo " | |"
echo " | |"
echo " _/_\_ |"
echo " |_| |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " |"
echo " _____________|_____"
echo " "
}
function gallows_body {
clear
echo " __________"
echo " | |"
echo " | |"
echo " _/_\_ |"
echo " |_| |"
echo " | |"
echo " | |"
echo " | |"
echo " | |"
echo " |"
echo " |"
echo " |"
echo " _____________|_____"
echo " "
}
function gallows_arm1 {
clear
echo " __________"
echo " | |"
echo " | |"
echo " _/_\_ |"
echo " |_| |"
echo " | |"
echo " ---| |"
echo " | |"
echo " | |"
echo " |"
echo " |"
echo " |"
echo " _____________|_____"
echo " "
}
function gallows_arm2 {
clear
echo " __________"
echo " | |"
echo " | |"
echo " _/_\_ |"
echo " |_| |"
echo " | |"
echo " ---|--- |"
echo " | |"
echo " | |"
echo " |"
echo " |"
echo " |"
echo " _____________|_____"
echo " "
}
function gallows_leg1 {
clear
echo " __________"
echo " | |"
echo " | |"
echo " _/_\_ |"
echo " |_| |"
echo " | |"
echo " ---|--- |"
echo " | |"
echo " | |"
echo " / |"
echo " / |"
echo " |"
echo " _____________|_____"
echo " "
}
function gallows_leg2 {
clear
echo " __________"
echo " | |"
echo " | |"
echo " _/_\_ |"
echo " |_| |"
echo " | |"
echo " ---|--- |"
echo " | |"
echo " | |"
echo " / \ |"
echo " / \ |"
echo " |"
echo " _____________|_____"
echo " "
}
#this function prints the win screen
function win {
echo "* * ******* * * * "
echo "* * * ** * * "
echo "* * * * * * * "
echo "* * * * * * * "
echo " * * * * * ** "
echo " * * ******* * * * "
echo -en "\n\n\n"
}
#this function prints the lose screen
function lose {
echo "lose..."
echo "The word was ${word[$word_index]}"
}
#this function prints the guessed letters to the screen and
#underscores for letters that haven't been guessed
function print_alpha {
echo -e "\nLetters Guessed:"
e=0
while [ ! "$e" == "26" ]
do
if [ "$char" == "${alpha[$e]}" ]
then
alpha_img[$e]="1"
fi
if [ ${alpha_img[$e]} == "1" ]
then
echo -n ${alpha[$e]}
else
echo -n "-"
fi
if [ $e == "12" ]
then
echo -e "\n"
fi
e=`expr $e + 1`
done
echo -ne "\n\n"
char=""
}
#a function to print the correctly guessed letters of the word to the
#screen or else an underscore for letters no yet guessed
function print_word {
echo -ne "\nWord: "
t=0
while [ ! $t == ${#word[${word_index}]} ]
do
if [ ${word_img[${t}]} == "1" ]
then
echo -n "${word[${word_index}]:$t:1}"
else
echo -n "-"
fi
t=`expr $t + 1`
done
echo -e "\n\n"
}
#####################################
# beginning of the main program here!
#####################################
readfile;
gameover=0
incorrect=0
correct=0
while [ "$gameover" == "0" ]
do
a=0
while [ ! "$a" == "26" ]
do
alpha_img[$a]=0
a=`expr $a + 1`
done
word_img=0
alpha_img=0
incorrect=0
correct=0
readword;
a=0
gallows;
print_alpha;
print_word;
#check for winning/losing conditions and update status of hanged man
while [[ ! "${numletter}" == "${#word[${word_index}]}" && ! "$incorrect" == "6" ]]
do
guess;
if [ $correct == "0" ]
then
incorrect=`expr $incorrect + 1`
fi
if [ $incorrect == "0" ]
then
gallows;
elif [ $incorrect == "1" ]
then
gallows_head;
elif [ $incorrect == "2" ]
then
gallows_body;
elif [ $incorrect == "3" ]
then
gallows_arm1;
elif [ $incorrect == "4" ]
then
gallows_arm2;
elif [ $incorrect == "5" ]
then
gallows_leg1;
elif [ $incorrect == "6" ]
then
gallows_leg2;
fi
print_alpha;
print_word;
done
if [ "${numletter}" == "${#word[${word_index}]}" ]
then
clear
win;
gameover=1
fi
if [ $incorrect == "6" ]
then
lose;
gameover=1
fi
if [ "$gameover" == "1" ]
then
echo -e "\n\n Play again? (y/n)"
read answer
if [ "$answer" == "y" ]
then
gameover=0
fi
clear
fi
done
exit 0