SenecaComputer.com

Tux

Create a simple number guessing game using the "if construct"

This is a Linux shell script for a simple 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 exits.

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.  A "while construct" is used to loop the game for a maximum of five loops (guesses).

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

#!/bin/bash
#########################################################################################################
#                                                                                                       #
#                                              game1.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.                                                                                #
#                                                                                                       #
#                                                                                                       #
#########################################################################################################
 
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 loop
do
  echo -e "Enter a number from 1 to 10: \c"      # get user input
  read INPUT

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

  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 "\nGood job, that's correct.  You got it on guess number $COUNTER. \n"
          exit                                   # exit game after correct guess
      elif [ $INPUT -lt $SECRET ]                # check for too low guess
        then echo -e "\nThat was guess number "$COUNTER".  You guessed too low \n"
      elif [ $INPUT -gt $SECRET ]                # check for too high guess
        then echo -e "\nThat was guess number "$COUNTER".  You guessed too high \n"
      fi
    else                                         # if bad input, tell user
      echo -e "\nThat was guess number "$COUNTER" but you entered "$INPUT "which is not a number between 1 and 10. \n"
  fi

  if [ $COUNTER -eq 5 ]                          # check guess counter, if 5th guess tell user and exit
    then echo -e "Sorry, that was your last guess.  Game over. \n"
      exit                                       # exit game after 5th guess
  fi

  echo -e "Try again. \c"                        # if wrong guess or bad input, tell user to try again

done                                             # finishes this loop cycle

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

chmod u+rwx game1.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