diff --git a/guess_number.sh b/guess_number.sh new file mode 100755 index 0000000..612ac66 --- /dev/null +++ b/guess_number.sh @@ -0,0 +1,95 @@ +#!/bin/bash +RANDOM_NUM=0 +ACTION_TO_TAKE="" +ATTEMPTS_NUM=7 +POOL_NUM=100 + +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 +} + +# for arg in "$@"; do +# if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then +# usage +# exit +# fi +# if [[ "$arg" == "--attempts" ]]; then +# test=$((arg+2)) +# ATTEMPTS_NUM=$((arg + 1)) +# #ACTION_TO_TAKE="create-group" +# echo "Setting attempts to $ATTEMPTS_NUM" +# echo "${!1}" +# fi +# done +while getopts a:p:h flag; do + case "${flag}" in + a) ATTEMPTS_NUM=${OPTARG};; + p) POOL_NUM=${OPTARG};; + h) usage;; + esac +done + +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" + #echo "El numero es $RANDOM_NUM lol" +} + +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 + if [[ $1 -lt $RANDOM_NUM ]]; then + echo "El número es demasiado bajo!" + ATTEMPTS_NUM=$((ATTEMPTS_NUM - 1)) + return 0 + fi + + fi +} + +checkLose() { + if [[ $ATTEMPTS_NUM == 0 ]]; then + echo "Pierdes! El número era $RANDOM_NUM :/" + exit + fi +} + +main() { + RANDOM_NUM=$(seq 0 $POOL_NUM | tr " " "\n" | shuf -n1) + while true; do + status + checkLose + read -p "Número: " num_attempt + checkWin $num_attempt + read -p "Pulse enter para continuar: " + done +} + +main