2. (60 points) Write the following script for bash Submit a shell script version
ID: 3607470 • Letter: 2
Question
2. (60 points) Write the following script for bash Submit a shell script version of the HI-LO game that uses text data. Call this script “hilo". The game should be designed for two players. The program should start by prompting player 1 for a target word. The program should then read a word from the keyboard Once the word is entered, the screen should clear. Then, the game should allow player 2 to repeatedly enter guesses and indicate if the guess is too high, too low, or correct. When player 2 finally guesses player 1's target word, the program should display the number of guesses that player 2 needed The system should track the best performance by player 2 (and write it to a save file). If the number of guesses required by player 2 is the best performance to date, the system should display a message to player 2. Then, the new best performance (number of guesses) should be written to the file along with the current date/time and target word used in that game The script should have an option that allows for the ability to use numbers instead of text. If someone types hilo -n, then your program should read a target number from player 1 and track the guesses from player 2 until correct. This performance should be written to its own save file as well (number of guess, date/time, and target number)Explanation / Answer
#save file as game.sh
# execute using "sh game.sh"
# track the data
target=
guess=
attempts=0
prevrecord=0
clear
# input entered is hidden while typing
# check for parameter
if [ "$1" = "-n" ] ; then
stty -echo
echo "==> Player 1"
echo -n "Enter Target Number: "
read target
stty echo
clear
guess=0
echo "===> player 2"
# perform the check
while [ "$target" -ne "$guess" ]
do
echo -n "Guess: "
read guess
attempts=$(expr $attempts + 1)
if [ "$guess" -lt "$target" ] ; then
tput setaf 1
echo "Too Less"
elif [ "$guess" -gt "$target" ] ; then
tput setaf 1
echo "Too high"
else
tput setaf 2
echo "===> Matched <==="
fi
tput setaf 7
done
else
# perform the check
stty -echo
echo "==> Player 1"
echo -n "Enter Target Word: "
read target
stty echo
clear
echo "==> PLAYER 2"
while [ "$target" != "$guess" ]
do
echo -n "Guess: "
read guess
attempts=$(expr $attempts + 1)
if [ "$guess" < "$target" ] ; then
tput setaf 1
echo "Sorry, It is too Less"
elif [ "$guess" > "$target" ] ; then
tput setaf 1
echo "Sorry, It is too high"
else
tput setaf 2
echo "**** Matched ****"
fi
tput setaf 7
done
fi
echo -e " Total Number of Attempts : $attempts "
## File
if [ -f records.txt ] ; then
prevrecord=$(head -n 1 records.txt)
else
echo -e "$attempts $target $(date)" > records.txt
fi
#Check the performance up to date
if [ "$prevrecord" -gt "$attempts" ] ; then
tput setab 1
tput bold
echo "************************************"
echo "This is the best performance to date"
echo -e "************************************ "
echo -e "$attempts $target $(date)" > records.txt
tput sgr0
fi
## Write the performance to the file
echo -e "$attempts---$target---$(date)" >> allrecords.txt
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.