In: Computer Science
creating a bash script called guessingGame.sh
The guessingGame.sh should generate a random number that the user has to guess. Each time after a guess input the program will let the user know if their guess is too high or too low until they guess the right number. The script should keep a count of how many guesses have currently been made. If the user the has made five guesses without guessing correctly the program should give a hint and let the user know if the number is odd or even. After ten guesses the number should be given and the user should be notified that a new number has been picked and they have ten guesses again. The program should end once the user finally guesses the right number.
random_number=$(( $RANDOM + 1 )) # Generate Random Number
cnt="0" # define a variable to track the loop count
while true # infinite loop
do # starting of loop
let "cnt++" # increament the cnt value
echo -n "Guess a number : "
read VAR # taking input from player
if [[ $VAR -eq $random_number ]] # chech the equall condtion
then
echo "Correct guess"
break # game over ...right guess
elif [[ $VAR -lt $random_number ]] # made guess less the number
then
echo "your guess number is less than Number ."
else
echo "your guess number is greater than Number"
fi
# check for the loop count (5) and give hint
if [[ $cnt -eq 5 ]]; then
echo Hint :
if [ $((random_number%2)) -eq 0 ]
then
echo "Guess a Even Number."
else
echo "Guess a Odd Number."
fi
fi
# If you have made more than 10 time wrong guesses
# it's time to reviel the number
if [[ $cnt -eq 10 ]]; then
echo Generated Number was $random_number
echo New number has been generated ...guess that one
let "random_number=$(( $RANDOM + 1 ))"
let "cnt=0"
fi
done # end of while loop