#!/bin/bash # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Copyright (c) 2010 FSOCA final project | http://joaosantacruz.com # This script is licensed under GNU GPL version 2.0 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Create a BASH script (guessing-game.sh) that uses the provided function to generate # a pseudo-random number between 1-100 stored in NUMBER: # NUMBER=$[ ( $RANDOM % 100 ) + 1 ] # The user should be asked to guess the number ($GUESS) and be told if they are too high # ($GUESS > $NUMBER) Òtoo highÓ, or if they are too low ($GUESS < $NUMBER) Òtoo # lowÓ. If the user guesses the correct number then they are told that they have won and the # program exits. (Ctrl-C Ð force terminates). Extension: Give the user a maximum of 8 # guesses before the program says that they have lost. If the user loses or wins, they should be # asked if they would like to play again. If the user enters a guess of ÒqÓ the script should exit. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - clear echo "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" echo " Guessing Game " echo "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" echo "" random_number=$((RANDOM%100+1)) score=8; echo "Hello $USER, what's the scecret number (1-99)? " read input_number while [[ 1==1 ]] do if [[ $input_number == "q" && $input_number ]] then echo -e "\nGAME OVER! You pressed the 'quit[q]' key!\n" break fi if echo $input_number | egrep -q '^[0-9]+$' #check if input_number is a numbet then score=`expr $score - 1` if [ $score -lt 1 ] then echo -e "\nGAME OVER! Your are out of guesses to reach this number.\n" break elif [ $input_number -lt $random_number ] # Check if directory exists then echo "To low, try again! [$score guesses left]" elif [ $input_number -gt $random_number ] then echo "To hight, try again! [$score guesses left]" elif [ $input_number -eq $random_number ] then echo -e "\nThat's it! The sectret number is '$input_number'. Your score is '$score'.\n" break fi else echo "Invalid input, try again! [$score guesses left]" fi read input_number done echo "" echo "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" echo "Press [enter] key to continue. . ."; read pressedKey