Skip to content

Commit 6546d8d

Browse files
Create Hangman.sh
1 parent 2429f7e commit 6546d8d

File tree

1 file changed

+298
-0
lines changed

1 file changed

+298
-0
lines changed

Hangman.sh

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
##These are the stick figures to be displayed if the user does a wrong guess
2+
3+
function wrong1 {
4+
echo
5+
echo " O "
6+
echo
7+
echo
8+
echo
9+
echo
10+
echo
11+
echo
12+
}
13+
function wrong2 {
14+
echo
15+
echo " O "
16+
echo " | "
17+
echo
18+
echo
19+
echo
20+
echo
21+
echo
22+
}
23+
function wrong3 {
24+
echo
25+
echo " O "
26+
echo " |\ "
27+
echo
28+
echo
29+
echo
30+
echo
31+
echo
32+
}
33+
function wrong4 {
34+
echo
35+
echo " O "
36+
echo " /|\ "
37+
echo
38+
echo
39+
echo
40+
echo
41+
echo
42+
}
43+
function wrong5 {
44+
echo
45+
echo " O "
46+
echo " /|\ "
47+
echo " / "
48+
echo
49+
echo
50+
echo
51+
echo
52+
}
53+
function wrong6 {
54+
echo
55+
echo " O "
56+
echo " /|\ "
57+
echo " / \ "
58+
echo
59+
echo
60+
echo
61+
echo
62+
}
63+
function wrong7 {
64+
echo
65+
echo " __________ "
66+
echo " | | "
67+
echo " O | "
68+
echo " /|\ | "
69+
echo " / \ | "
70+
echo " ______________|___"
71+
echo
72+
}
73+
74+
function display {
75+
DATA[0]=" # # # # # ##### # # # # #"
76+
DATA[1]=" # # # # ## # # # ## ## # # ## #"
77+
DATA[2]=" # # # # # # # # # # # # # # # # #"
78+
DATA[3]=" ####### # # # # # # #### # # # # # # # #"
79+
DATA[4]=" # # ####### # # # # # # # ####### # # #"
80+
DATA[5]=" # # # # # ## # # # # # # # ##"
81+
DATA[6]=" # # # # # # ##### # # # # # #"
82+
echo
83+
84+
85+
# virtual coordinate system is X*Y ${#DATA} * 8
86+
## This is to put the title in the centre
87+
88+
REAL_OFFSET_X=$(($((`tput cols` - 56)) / 2))
89+
REAL_OFFSET_Y=$(($((`tput lines` - 6)) / 2))
90+
91+
draw_char() {
92+
V_COORD_X=$1
93+
V_COORD_Y=$2
94+
95+
tput cup $((REAL_OFFSET_Y + V_COORD_Y)) $((REAL_OFFSET_X + V_COORD_X))
96+
97+
printf %c ${DATA[V_COORD_Y]:V_COORD_X:1}
98+
}
99+
100+
trap 'exit 1' INT TERM
101+
102+
tput civis
103+
clear
104+
tempp=8
105+
while :; do
106+
tempp=`expr $tempp - 8`
107+
for ((c=1; c <= 7; c++)); do
108+
tput setaf $c
109+
for ((x=0; x<${#DATA[0]}; x++)); do
110+
for ((y=0; y<=6; y++)); do
111+
draw_char $x $y
112+
done
113+
done
114+
done
115+
sleep 1
116+
clear
117+
break
118+
done
119+
}
120+
121+
##The main menu where you will be asked to choose the categories.
122+
##And also if the user wants custom words, he/she can add the file path
123+
function menu() {
124+
## Supresses the error message that comes with the usage of GTK+
125+
exec 2> /dev/null
126+
## Uses the zenity module, which comes pre-installed with Debian
127+
selection=$(zenity --list "Play the game" "Choose a topic" "Exit" --column="" --text="Choose an option" --title="Game options" --cancel-label="Quit")
128+
case "$selection" in
129+
"Play the game") main;;
130+
"Choose a topic") choice;;
131+
"Exit") exit;;
132+
esac
133+
echo
134+
}
135+
##This function allows the user to choose a topic or add one
136+
function choice() {
137+
choose=$(zenity --list "Movies" "Bollywood" "English words" "Select a file" --column="" --text="Choose a list" --title="Game options" --cancel-label="Back")
138+
139+
case $choose in
140+
"Movies") filename="movies";;
141+
"Bollywood") filename="bollywood";;
142+
"English words") filename="/usr/share/dict/american-english";;
143+
"Select a file") file_select;;
144+
esac
145+
menu
146+
}
147+
148+
function file_select() {
149+
filename=$(zenity --file-selection --title="Select a file")
150+
case $? in
151+
0)
152+
echo "\"$filename\" selected";;
153+
1)
154+
echo "No file selected" ;;
155+
-1)
156+
echo "Unexpected error occurred" ;;
157+
esac
158+
}
159+
160+
function main() {
161+
##The function used to read the word list
162+
readarray a < $filename
163+
164+
randind=`expr $RANDOM % ${#a[@]}`
165+
166+
movie=${a[$randind]}
167+
168+
guess=()
169+
170+
guesslist=()
171+
guin=0
172+
173+
movie=`echo $movie | tr -dc '[:alnum:] \n\r' | tr '[:upper:]' '[:lower:]'`
174+
len=${#movie}
175+
176+
for ((i=0;i<$len;i++)); do
177+
guess[$i]="_"
178+
done
179+
180+
mov=()
181+
182+
for ((i=0;i<$len;i++)); do
183+
mov[$i]=${movie:$i:1}
184+
# echo -n "${mov[$i]} "
185+
done
186+
187+
for ((j=0;j<$len;j++)); do
188+
if [[ ${mov[$j]} == " " ]]; then
189+
guess[$j]=" "
190+
fi
191+
done
192+
193+
## Display the initial setup
194+
195+
wrong=0
196+
197+
while [[ $wrong -lt 7 ]]; do
198+
case $wrong in
199+
0)echo " "
200+
;;
201+
1)wrong1
202+
;;
203+
2)wrong2
204+
;;
205+
3)wrong3
206+
;;
207+
4)wrong4
208+
;;
209+
5)wrong5
210+
;;
211+
6)wrong6
212+
;;
213+
esac
214+
215+
if [[ wrong -eq 0 ]]; then
216+
for i in {1..7}
217+
do
218+
echo
219+
done
220+
fi
221+
222+
notover=0
223+
for ((j=0;j<$len;j++)); do
224+
if [[ ${guess[$j]} == "_" ]]; then
225+
notover=1
226+
fi
227+
done
228+
229+
echo Guess List: ${guesslist[@]}
230+
echo Wrong guesses: $wrong
231+
for ((k=0;k<$len;k++)); do
232+
echo -n "${guess[$k]} "
233+
done
234+
echo
235+
echo
236+
237+
if [[ notover -eq 1 ]]; then
238+
echo -n "Guess a letter: "
239+
read -n 1 -e letter
240+
letter=$(echo $letter | tr [A-Z] [a-z])
241+
guesslist[$guin]=$letter
242+
guin=`expr $guin + 1`
243+
fi
244+
245+
f=0;
246+
for ((i=0;i<$len;i++)); do
247+
if [[ ${mov[$i]} == $letter ]]; then
248+
guess[$i]=$letter
249+
f=1
250+
fi
251+
done
252+
if [[ f -eq 0 ]]; then
253+
wrong=`expr $wrong + 1`
254+
fi
255+
256+
if [[ notover -eq 0 ]]; then
257+
echo
258+
echo You Win!
259+
echo $movie
260+
echo
261+
play_again
262+
fi
263+
clear
264+
done
265+
266+
wrong7
267+
echo You lost!
268+
echo The word was: $movie
269+
play_again
270+
}
271+
272+
function play_again(){
273+
echo
274+
echo -n "Would you like to play again? (y/n) "
275+
read -n 1 choice
276+
case $choice in
277+
[yY]) clear
278+
main
279+
;;
280+
esac
281+
clear
282+
echo "Thanks for playing!"
283+
tput cnorm
284+
exit
285+
}
286+
287+
function init(){
288+
clear
289+
##This is the default file name if the user doesn't select any file
290+
filename="movies"
291+
292+
echo
293+
display
294+
295+
menu
296+
}
297+
298+
init

0 commit comments

Comments
 (0)