#!/usr/local/bin/bash
# UN JUEGO DE AHORCADO ESCRITO PARA BASH
#
# Para jugar de permisos de ejecución al script e ingrese ./ahorcado.sh
#
# Este juego necesita una lista de palabras en palabras.dat en el mismo
# directorio del script ahorcado.sh
#
# Autor: Ashton Seth Reimer
# Date: Summer 2010
# License: See the license file included in the repository on github:
# https://github.com/asreimer/bash_hangman
#
#incializa las palabras y arreglos que neceistamos
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
#esta función lee la lista de palabras de palabras.dat y las pone en un
#arreglo de palabras.
function readfile
{
exec 3<&0
exec 0< palabras.dat
while read LINE
do
word[i]=$LINE
i=`expr $i + 1`
done
exec 0<&3
}
#esta función elige una palabra al azar y la pone en un arreglo de
#palabras.
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
}
#esta función solicita una letra y revisa si es válida/correcta/incorrecta
function guess
{
j=0
correct=0
echo -n "Adivina una letra: "
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
}
#Las siguientes funciones dibjuan la horca y al ahorcado.
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 " "
}
#esta función imprime la pantalla de triunfo.
function win {
echo " ____ _ _ "
echo "| __ )(_) ___ _ __ | |"
echo "| _ || |/ _ | '_ | | |"
echo "| |_) | || __/ | | | |_|"
echo "|____/|_||___|_| |_| (_)"
echo -en "\n\n\n"
}
#esta función imprime la pantalla de perder
function lose {
echo "Perdiste..."
echo "La palabra era ${word[$word_index]}"
}
#esta función imprime las letras adividadas en la pantalla y subraya las
#letras que no han sido adivinadas.
function print_alpha {
echo -e "\nLetras ya dichas:"
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=""
}
#Función para imprimir las letras correctamente adivinadas o tacha las palabras no adivinadas aún.
function print_word {
echo -ne "\nPalabra: "
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"
}
#######################################
# el programa principal comienza aquí.
#######################################
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;
#revisa las condiciones de ganar/perder y actualiza el status
#del ahorcado
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 Jugar de nuevo? (s/n)"
read answer
if [ "$answer" == "s" ]
then
gameover=0
fi
clear
fi
done
exit 0