Código de Linux/Unix Shell Scripting - Juego del ahorcado en Bash

20191101
estrellaestrellaestrellaestrellaestrella(1)

Publicado el 6 de Marzo del 2020gráfica de visualizaciones de la versión: 20191101
1.439 visualizaciones desde el 6 de Marzo del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

Contenido de los archivos con las palabras

dict_en.dat


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
telephone
helicopter
strawberry
hypotenuse
beer
deer
cheer
giraffe
saw
tea
coagulation
zip
excess
jupiter
saturn
venus
earth
dirt
worm
rummage
rubbage
shank
crank
tank
monkey
orange
apple
pear
constipated
fiber
bread
beans
bean
little
massive
super
single
ornery
grimmace
quaint
quail
quill
ridiculous
piglet
faggot
homes
gangster
acorn
unary
lit
harp
mother
father
sister
brother
turtle
club
get
with
you
sex
tums
attempt
genius
luscious
clouds
mushroom
nuclear
bomb
bamboo
detroit
dandylion
pole
pail
poland
canada
barn
tooth
rafter
train
support
alcohol
alcoholic
cocaine
ham
slip
lip
happy
sad
community
license
channel
curve
asphault
bell
obtuse
acute
light
trailer
isoceles
evolution
creation
duck
crawling
place
post
wire
gnarly
central
basketball
primary


dic_es.dat


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
teléfono
helicóptero
fresa
hipotenusa
cerveza
ciervo
animar
jirafa
Sierra
coagulación
Código Postal
exceso
Júpiter
Saturno
Venus
tierra
suciedad
gusano
hurgar
basura
caña
manivela
tanque
mono
naranja
manzana
Pera
estreñido
fibra
pan de molde
frijoles
frijol
pequeño
masivo
súper
soltero
desagradable
tristeza
pintoresco
codorniz
pluma
ridículo
cerdito
maricón
casas
gángster
bellota
unario
iluminado
arpa
madre
padre
hermana
hermano
Tortuga
club
obtener
con
sexo
tums
intento
genio
delicioso
nubes
seta
nuclear
bomba
bambú
Detroit
dandylion
polo
cubo
Polonia
Canadá
granero
diente
viga
entrenar
apoyo
alcohol
alcohólico
cocaína
jamón
resbalón
labio
feliz
triste
comunidad
licencia
canal
curva
asfalto
campana
obtuso
agudo
ligero
remolque
isoceles
evolución
creación
Pato
gateando
sitio
enviar
cable
retorcido
central
baloncesto
primario

Forma parte de GitHub
 
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/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
# ---
# Date: Fall 2019
# Forked and modified by Victorhck to add a Spanish and English option in words to guess and questions
 
#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
idioma=0
resp_idioma=0
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
NC="\033[0m" #No color
 
#this function reads the list of words from the dict.dat file and
#put them into the word array
function readfile
{
 
exec 3<&0
while [[ $resp_idioma != 1 && $resp_idioma != 2 ]]
    do
        # Borra pantalla
        printf "\e[2J\e[H"
        echo -ne "¿Español (Es) / English (En)? \n"
        echo -n "Es = 1 / En = 2: "
        read resp_idioma
        char=$resp_idioma
    done
if [ ${resp_idioma} -eq "1" ]
    then
        exec 0< dict_es.dat
    else
        exec 0< dict_en.dat
fi
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
 
    if [ ${resp_idioma} -eq "1" ]
        then
            echo -n "Adivina una letra: "
        else
            echo -n "Guess a letter: "
    fi
	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
printf "$GREEN"
echo "          __________"
echo "         |         |"
echo "         |         |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "      _____________|_____"
echo "                           "
printf "$NC"
}
function gallows_head
{
clear
printf "$GREEN"
echo "          __________"
echo "         |         |"
echo "         |         |"
echo "       _/_\_       |"
echo "        |_|        |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "      _____________|_____"
echo "                           "
printf "$NC"
}
function gallows_body {
clear
printf "$GREEN"
echo "          __________"
echo "         |         |"
echo "         |         |"
echo "       _/_\_       |"
echo "        |_|        |"
echo "         |         |"
echo "         |         |"
echo "         |         |"
echo "         |         |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "      _____________|_____"
echo "                           "
printf "$NC"
}
function gallows_arm1 {
clear
printf "$YELLOW"
echo "          __________"
echo "         |         |"
echo "         |         |"
echo "       _/_\_       |"
echo "        |_|        |"
echo "         |         |"
echo "      ---|         |"
echo "         |         |"
echo "         |         |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "      _____________|_____"
echo "                           "
printf "$NC"
}
function gallows_arm2 {
clear
printf "$YELLOW"
echo "          __________"
echo "         |         |"
echo "         |         |"
echo "       _/_\_       |"
echo "        |_|        |"
echo "         |         |"
echo "      ---|---      |"
echo "         |         |"
echo "         |         |"
echo "                   |"
echo "                   |"
echo "                   |"
echo "      _____________|_____"
echo "                           "
printf "$NC"
}
function gallows_leg1 {
clear
printf "$RED"
echo "          __________"
echo "         |         |"
echo "         |         |"
echo "       _/_\_       |"
echo "        |_|        |"
echo "         |         |"
echo "      ---|---      |"
echo "         |         |"
echo "         |         |"
echo "        /          |"
echo "       /           |"
echo "                   |"
echo "      _____________|_____"
echo "                           "
printf "$NC"
}
function gallows_leg2 {
clear
printf "$RED"
echo "          __________"
echo "         |         |"
echo "         |         |"
echo "       _/_\_       |"
echo "        |_|        |"
echo "         |         |"
echo "      ---|---      |"
echo "         |         |"
echo "         |         |"
echo "        / \        |"
echo "       /   \       |"
echo "                   |"
echo "      _____________|_____"
echo "                           "
printf "$NC"
}
 
#this function prints the win screen
function win_es {
echo " ____  _              _ "
echo "| __ )(_) ___ _ __   | |"
echo "|  _ || |/ _ | '_  | | |"
echo "| |_) | || __/ | | | |_|"
echo "|____/|_||___|_| |_| (_)"
echo -en "\n\n\n"
}
 
#this function prints the win screen
function win_en {
echo "*     *  *******  *    *  * "
echo "*     *     *     **   *  * "
echo "*     *     *     * *  *  * "
echo "*     *     *     *  * *  * "
echo " * * *      *     *   **    "
echo "  * *    *******  *    *  * "
echo -en "\n\n\n"
}
 
 
#this function prints the lose screen
function lose_en {
echo "lose..."
echo "The word was: ${word[$word_index]}"
}
 
#this function prints the lose screen
function lose_es {
echo "Perdiste..."
echo "La palabra era: ${word[$word_index]}"
}
 
#this function prints the guessed letters to the screen and
#underscores for letters that havent been guessed
function print_alpha {
    if [ ${resp_idioma} -eq "1" ]
        then
            echo -e "\nLetras ya dichas:"
        else
            echo -e "\nLetters Guessed:"
    fi
	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 {
    if [ ${resp_idioma} -eq "1" ]
        then
            echo -ne "\nPalabra: "
        else
            echo -ne "\nWord: "
    fi
	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
    if [ ${resp_idioma} -eq "1" ]
        then
            win_es;
        else
            win_en;
    fi
	gameover=1
	fi
	if [ $incorrect == "6" ]
	then
 
    if [ ${resp_idioma} -eq "1" ]
        then
            lose_es;
        else
            lose_en;
    fi
	gameover=1
	fi
 
	if [ "$gameover" == "1" ]
	then
    if [ ${resp_idioma} -eq "1" ]
        then
            echo -e "\n\n ¿Jugar de nuevo? (s/n)"
        else
            echo -e "\n\n Play again? (y/n)"
        fi
		read answer
		if [ $answer == 'y' ] || [ $answer == 's' ]
		then
			gameover=0
		fi
		clear
	fi
done
exit 0



Comentarios sobre la versión: 20191101 (1)

Imágen de perfil
7 de Marzo del 2020
estrellaestrellaestrellaestrellaestrella
bien.

debería hacer solo una modificaron sencilla. Que al principio tome la palabra a buscar o adivinar.

No tomada el asar, ya que nadie sabe que palabra tomara.

Pero muy bueno gracias
Responder

Comentar la versión: 20191101

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6000