B24S_4_CIUGUREAN
Ciugiu
sh
2 years ago
2.4 kB
15
Indexable
#!/bin/bash
# Initialize the game counter
gameCount=0
while true; do
# Increment the game counter
((gameCount++))
# Ask the user to choose the difficulty level
echo "Choose the difficulty level:"
echo "EASY (E), MEDIUM (M), HARD (H)"
read -p "Enter your choice (E/M/H): " level
# Set the difficulty level and max tries based on the user's choice
maxTries=10
k=1
if [[ "$level" =~ [Mm] ]]; then
maxTries=8
k=3
elif [[ "$level" =~ [Hh] ]]; then
maxTries=6
k=10
fi
# Generate a random number between 10 and 99
num=$((10 + RANDOM % 90))
# Initialize the number of tries
tries=0
while true; do
# Increment the number of tries
((tries++))
# Ask the user to guess the number
read -p "Guess the number: " guess
# Check if the guess is valid
if ((guess < 10 || guess > 99)); then
echo "INPUT ERROR"
continue
fi
# Check if the guess is correct
if ((guess == num)); then
score=$(( (maxTries - tries + 1) * k ))
echo "YOU WIN score: $score"
echo "Score: $score, Date: $(date)" >> "${USER}.txt"
break
fi
# Check if the guess is too small or too big
if ((guess < num)); then
echo "TOO SMALL"
else
echo "TOO BIG"
fi
# Check if the maximum number of tries has been reached
if ((tries >= maxTries)); then
echo "YOU LOSE It was: $num"
break
fi
done
# Display the best score if more than one game has been played
if ((gameCount > 1)); then
bestScore=0
while read -r line; do
score=$(echo "$line" | cut -d',' -f1 | cut -d':' -f2)
if ((score > bestScore)); then
bestScore=$score
fi
done < "${USER}.txt"
echo "== BEST SCORE $bestScore =="
fi
# Ask the user if they want to play again
read -p "Do you want to play again (Y/N)? " playAgain
if [[ "$playAgain" =~ [Nn] ]]; then
echo "Goodbye!"
break
elif [[ ! "$playAgain" =~ [Yy] ]]; then
echo "Invalid choice"
break
fi
doneEditor is loading...
Leave a Comment