Question

In: Computer Science

creating a bash script called guessingGame.sh The guessingGame.sh should generate a random number that the user...

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.

Solutions

Expert Solution

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





Related Solutions

Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
Write a short bash script that takes in two arguments from a user, asks the user...
Write a short bash script that takes in two arguments from a user, asks the user whether they would like to add, subtract, multiply, or divide. Each of these operations must be a function that returns data.
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Write a bash script file that tests each file entry in the current directory. It should...
Write a bash script file that tests each file entry in the current directory. It should determine if it is a file or directory. If it is a file, it will determine if it is readable or not. If it is readable, it will display only the first 4 lines to the terminal in sorted order (just sort the first 4 lines). If it is a directory, it should display the contents of that directory (the files and subdirectories). NOTE:...
Create a class called RandomGuess. In this game, generate and store a random number between 1...
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop): Ask the user to enter a number between 1 and 100 Let the user know if the guess is high or low, until the user enters the correct value If the user enters the correct value, then display a count of the number of attempts it took and exit
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT