SenecaComputer.com

Tux

Create a more complex number guessing game using the "if construct" and nested loops

This is a Linux shell script for a more complex number guessing game.  The game generates a random number between 1 and 10.  The player has five chances to guess this random number.  After a successful guess or five unsuccessful guesses, the game does not exit, but allows the user to play again. 

This probably not a practical game, but it is intended to serve as a fun scripting exercise.  This script uses the "if construct" for decision making.  However it differs from game 1 and game 2 as the guess loop is nested inside a second "while construct" loop that allows the user to play 100 games before the game exits to the command prompt.

Either download the script here, or copy the text below and create a script with your favorite Linux text editor.

#!/bin/bash
#########################################################################################################
#                                                                                                       #
#                                              game3.sh                                                 #
#                                                                                                       #
# This is a number guessing game.  The user has 5 chances to guess a random number between 1 and 10.    #
#                                                                                                       #
# The game will only accept valid numerical numbers between 1 and 10.  Invalid input guesses will count #
# towards the 5 guesses.                                                                                #
#                                                                                                       #
# This program will permit multiple plays without exiting to bash shell command prompt.                 #
#                                                                                                       #
#                                                                                                       #
#########################################################################################################
 
clear                                            # clear the screen

GAMEON=no                                        # sets $GAMEON to no

GAMECOUNT=0                                      # set game counter to zero
while [ $GAMECOUNT -lt 100 ]                     # start game counter loop - can play 100 times
do

  ANSWER=0                                       # set $ANSWER to 0 value

  if [ $GAMEON = yes ]                           # if a game has already has been played
    then
      echo -e "That was game number "$GAMECOUNT".  Would you like to play again? (y/n)--> \c"
    else                                         # if this is the first game
      echo -e "Would you like to play a game? (y/n)--> \c"
  fi

  read ANSWER                                    # should be y or n response

  if [ $ANSWER = y -o $ANSWER = Y -o $ANSWER = n -o $ANSWER = N ]    # check for valid response
    then

    if [ $ANSWER = y -o $ANSWER = Y ]            # if response is y
      then
      GAMEON=yes                                 # set $GAMEON to yes, indicates games have started
      GAMECOUNT=`expr $GAMECOUNT + 1`            # increment game counter up 1 count

      clear                                      # clear the screen

      echo -e "This is a number guessing game.  You have 5 trys at guessing a number between 1 and 10. \n"

      SECRET=$((RANDOM%10+1))                    # generate a random number between 1 and 10

      COUNTER=0                                  # set guess counter to zero
      while [ $COUNTER -lt 5 ]                   # start guess loop
      do
        echo -e "Enter a number from 1 to 10: \c"   # get user guess input
        read INPUT

        COUNTER=`expr $COUNTER + 1`              # increment guess counter up 1 count

        echo                                     # echo blank line to screen

        if [ $INPUT -lt 11 -a $INPUT -gt 0 ] 2>/dev/null  # check for valid numerical input from 1 to 10
          then                                   # if good input, proceed
            if [ $SECRET = $INPUT ]              # check for correct guess
              then echo -e "Good job, that's correct.  You got it on guess number $COUNTER. \n"
              COUNTER=10                         # set $COUNTER to 10 to indicate game won
            elif [ $INPUT -lt $SECRET ]          # check for too low guess
              then echo -e "That was guess number "$COUNTER".  You guessed too low \n"
            elif [ $INPUT -gt $SECRET ]          # check for too high guess
              then echo -e "That was guess number "$COUNTER".  You guessed too high \n"
            fi
          else                                   # if bad input, tell user
            echo -e "That was guess number "$COUNTER" but you entered "$INPUT "which is not a number between 1 and 10 \n"
        fi

        if [ $COUNTER -eq 10 ]                   # game was won, reset for next game
          then echo                              # send blank line to screen   
        elif [ $COUNTER -eq 5 ]                  # check guess counter, if 5th guess tell user game over
          then echo -e "Sorry, that was your last guess.  Game over. \n"
        else echo -e "Try again. \c"             # if guess was too low or too high
        fi

      done                                       # finishes guess loop

    elif  [ $ANSWER = n -o $ANSWER = N ]         # user does not to start game or play again
      then
        if [ $GAMEON = yes ]                     # if user played the game
          then
            clear                                # clear the screen
            echo -e "\nGoodby, thanks for playing my game, I hope you had fun. \n"
            exit                                 # terminates program
        else                                     # if user does not want to start game
            clear                                # clear the screen
            echo -e "\nI hope you will come back later and play. \n"
            exit                                 # terminates program
        fi
    fi
  
    elif [ ! $ANSWER = n -o ! $ANSWER = N ]      # if bad input from user   

      then echo -e "\nYou did not answer y or n, please try again. \n"

  fi

done                                             # finishes game loop

Name the script file "game3.sh", and make it executable for the user with the command:

chmod u+rwx game3.sh

This script is offered under the GNU Public License without any warranty or guarantee
that it will function correctly on your system.    Enjoy!

Back to Scripts Page

Back to Seneca Computer Home Page

Page created April 26, 2012