2025-01-13 09:27:30 +01:00
|
|
|
#!/bin/bash
|
|
|
|
RANDOM_NUM=0
|
|
|
|
ATTEMPTS_NUM=7
|
|
|
|
POOL_NUM=100
|
2025-01-15 19:52:08 +01:00
|
|
|
RE='^[0-9]+$'
|
2025-01-13 09:27:30 +01:00
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
# Muestra la ayuda y sale del programa
|
2025-01-13 09:27:30 +01:00
|
|
|
usage ()
|
|
|
|
{
|
|
|
|
echo "Usage: ./guess_number.sh -p 50 -a 30"
|
|
|
|
echo ""
|
|
|
|
echo "PARAMETERS"
|
|
|
|
echo " -h (Show this help prompt)"
|
|
|
|
echo " -a $ (set the amount of available attempts)"
|
|
|
|
echo " -p $ (set the amount of numbers that will be generated)"
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
# Recoge parámetros al ejecutar el script
|
2025-01-13 09:27:30 +01:00
|
|
|
while getopts a:p:h flag; do
|
|
|
|
case "${flag}" in
|
|
|
|
a) ATTEMPTS_NUM=${OPTARG};;
|
|
|
|
p) POOL_NUM=${OPTARG};;
|
|
|
|
h) usage;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
# Muestra la cantidad actual de números y vidas restantes
|
2025-01-13 09:27:30 +01:00
|
|
|
status() {
|
|
|
|
clear
|
|
|
|
cat <<'END_CAT'
|
|
|
|
______________
|
|
|
|
< Buena suerte >
|
|
|
|
--------------
|
|
|
|
\ ^__^
|
|
|
|
\ (oo)\_______
|
|
|
|
(__)\ )\/\
|
|
|
|
||----w |
|
|
|
|
|| ||
|
|
|
|
END_CAT
|
|
|
|
echo "Intenta adivinar el número entre 0 y $POOL_NUM!"
|
|
|
|
echo "Intentos restantes: $ATTEMPTS_NUM"
|
|
|
|
}
|
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
# Comprueba si el número introducido es correcto,
|
|
|
|
# si no es correcto, comprueba si es mayor o menos antes
|
|
|
|
# de devolver el resultado
|
2025-01-13 09:27:30 +01:00
|
|
|
checkWin() {
|
|
|
|
if [[ "$1" == "$RANDOM_NUM" ]]; then
|
|
|
|
echo "Enhorabuena! El número era $RANDOM_NUM!"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$1" != "$RANDOM_NUM" ]]; then
|
|
|
|
if [[ $1 -gt $RANDOM_NUM ]]; then
|
|
|
|
echo "El número es demasiado alto!"
|
|
|
|
ATTEMPTS_NUM=$((ATTEMPTS_NUM - 1))
|
|
|
|
return 0
|
|
|
|
fi
|
2025-01-16 20:39:29 +01:00
|
|
|
|
|
|
|
# Ya se que este código podría ser considerablemente
|
|
|
|
# acortado, pero prefiero Bash lo más explícito posible
|
|
|
|
# para evitar posibles complicaciones
|
|
|
|
|
2025-01-13 09:27:30 +01:00
|
|
|
if [[ $1 -lt $RANDOM_NUM ]]; then
|
|
|
|
echo "El número es demasiado bajo!"
|
|
|
|
ATTEMPTS_NUM=$((ATTEMPTS_NUM - 1))
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
# Comprueba si el número de intentos es igual a cero
|
|
|
|
# para terminar la partida
|
2025-01-13 09:27:30 +01:00
|
|
|
checkLose() {
|
|
|
|
if [[ $ATTEMPTS_NUM == 0 ]]; then
|
|
|
|
echo "Pierdes! El número era $RANDOM_NUM :/"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
# Función principal
|
2025-01-13 09:27:30 +01:00
|
|
|
main() {
|
2025-01-16 20:39:29 +01:00
|
|
|
# Genera un número aleatorio desde 0 hasta el número máximo
|
|
|
|
# configurado por el usuario
|
2025-01-20 08:23:59 +01:00
|
|
|
RANDOM_NUM=$(seq 0 $POOL_NUM | shuf -n1)
|
2025-01-15 19:52:08 +01:00
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
for (( ; ; )); do
|
2025-01-13 09:27:30 +01:00
|
|
|
status
|
|
|
|
checkLose
|
2025-01-15 19:52:08 +01:00
|
|
|
|
2025-01-16 20:39:29 +01:00
|
|
|
# Comprobación de entrada de usuario:
|
|
|
|
# Prohíbe cualquier entrada que no sea un número y que sea mayor al límite
|
|
|
|
while true; do
|
2025-01-15 19:52:08 +01:00
|
|
|
read -p "Número: " num_attempt
|
2025-01-16 20:39:29 +01:00
|
|
|
if [[ $num_attempt =~ $RE ]] && (( $num_attempt <= $POOL_NUM )); then
|
|
|
|
break
|
|
|
|
fi
|
2025-01-15 19:52:08 +01:00
|
|
|
done
|
|
|
|
|
2025-01-13 09:27:30 +01:00
|
|
|
checkWin $num_attempt
|
|
|
|
read -p "Pulse enter para continuar: "
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
main
|