-
Notifications
You must be signed in to change notification settings - Fork 11
/
Hangman.sh
executable file
·298 lines (260 loc) · 6.36 KB
/
Hangman.sh
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
##These are the stick figures to be displayed if the user does a wrong guess
function wrong1 {
echo
echo " O "
echo
echo
echo
echo
echo
echo
}
function wrong2 {
echo
echo " O "
echo " | "
echo
echo
echo
echo
echo
}
function wrong3 {
echo
echo " O "
echo " |\ "
echo
echo
echo
echo
echo
}
function wrong4 {
echo
echo " O "
echo " /|\ "
echo
echo
echo
echo
echo
}
function wrong5 {
echo
echo " O "
echo " /|\ "
echo " / "
echo
echo
echo
echo
}
function wrong6 {
echo
echo " O "
echo " /|\ "
echo " / \ "
echo
echo
echo
echo
}
function wrong7 {
echo
echo " __________ "
echo " | | "
echo " O | "
echo " /|\ | "
echo " / \ | "
echo " ______________|___"
echo
}
function display {
DATA[0]=" # # # # # ##### # # # # #"
DATA[1]=" # # # # ## # # # ## ## # # ## #"
DATA[2]=" # # # # # # # # # # # # # # # # #"
DATA[3]=" ####### # # # # # # #### # # # # # # # #"
DATA[4]=" # # ####### # # # # # # # ####### # # #"
DATA[5]=" # # # # # ## # # # # # # # ##"
DATA[6]=" # # # # # # ##### # # # # # #"
echo
# virtual coordinate system is X*Y ${#DATA} * 8
## This is to put the title in the centre
REAL_OFFSET_X=$(($((`tput cols` - 56)) / 2))
REAL_OFFSET_Y=$(($((`tput lines` - 6)) / 2))
draw_char() {
V_COORD_X=$1
V_COORD_Y=$2
tput cup $((REAL_OFFSET_Y + V_COORD_Y)) $((REAL_OFFSET_X + V_COORD_X))
printf %c ${DATA[V_COORD_Y]:V_COORD_X:1}
}
trap 'exit 1' INT TERM
tput civis
clear
tempp=8
while :; do
tempp=`expr $tempp - 8`
for ((c=1; c <= 7; c++)); do
tput setaf $c
for ((x=0; x<${#DATA[0]}; x++)); do
for ((y=0; y<=6; y++)); do
draw_char $x $y
done
done
done
sleep 1
clear
break
done
}
##The main menu where you will be asked to choose the categories.
##And also if the user wants custom words, he/she can add the file path
function menu() {
## Supresses the error message that comes with the usage of GTK+
exec 2> /dev/null
## Uses the zenity module, which comes pre-installed with Debian
selection=$(zenity --list "Play the game" "Choose a topic" "Exit" --column="" --text="Choose an option" --title="Game options" --cancel-label="Quit")
case "$selection" in
"Play the game") main;;
"Choose a topic") choice;;
"Exit") exit;;
esac
echo
}
##This function allows the user to choose a topic or add one
function choice() {
choose=$(zenity --list "Movies" "Bollywood" "English words" "Select a file" --column="" --text="Choose a list" --title="Game options" --cancel-label="Back")
case $choose in
"Movies") filename="movies";;
"Bollywood") filename="bollywood";;
"English words") filename="/usr/share/dict/american-english";;
"Select a file") file_select;;
esac
menu
}
function file_select() {
filename=$(zenity --file-selection --title="Select a file")
case $? in
0)
echo "\"$filename\" selected";;
1)
echo "No file selected" ;;
-1)
echo "Unexpected error occurred" ;;
esac
}
function main() {
##The function used to read the word list
readarray a < $filename
randind=`expr $RANDOM % ${#a[@]}`
movie=${a[$randind]}
guess=()
guesslist=()
guin=0
movie=`echo $movie | tr -dc '[:alnum:] \n\r' | tr '[:upper:]' '[:lower:]'`
len=${#movie}
for ((i=0;i<$len;i++)); do
guess[$i]="_"
done
mov=()
for ((i=0;i<$len;i++)); do
mov[$i]=${movie:$i:1}
# echo -n "${mov[$i]} "
done
for ((j=0;j<$len;j++)); do
if [[ ${mov[$j]} == " " ]]; then
guess[$j]=" "
fi
done
## Display the initial setup
wrong=0
while [[ $wrong -lt 7 ]]; do
case $wrong in
0)echo " "
;;
1)wrong1
;;
2)wrong2
;;
3)wrong3
;;
4)wrong4
;;
5)wrong5
;;
6)wrong6
;;
esac
if [[ wrong -eq 0 ]]; then
for i in {1..7}
do
echo
done
fi
notover=0
for ((j=0;j<$len;j++)); do
if [[ ${guess[$j]} == "_" ]]; then
notover=1
fi
done
echo Guess List: ${guesslist[@]}
echo Wrong guesses: $wrong
for ((k=0;k<$len;k++)); do
echo -n "${guess[$k]} "
done
echo
echo
if [[ notover -eq 1 ]]; then
echo -n "Guess a letter: "
read -n 1 -e letter
letter=$(echo $letter | tr [A-Z] [a-z])
guesslist[$guin]=$letter
guin=`expr $guin + 1`
fi
f=0;
for ((i=0;i<$len;i++)); do
if [[ ${mov[$i]} == $letter ]]; then
guess[$i]=$letter
f=1
fi
done
if [[ f -eq 0 ]]; then
wrong=`expr $wrong + 1`
fi
if [[ notover -eq 0 ]]; then
echo
echo You Win!
echo $movie
echo
play_again
fi
clear
done
wrong7
echo You lost!
echo The word was: $movie
play_again
}
function play_again(){
echo
echo -n "Would you like to play again? (y/n) "
read -n 1 choice
case $choice in
[yY]) clear
main
;;
esac
clear
echo "Thanks for playing!"
tput cnorm
exit
}
function init(){
clear
##This is the default file name if the user doesn't select any file
filename="movies"
echo
display
menu
}
init