SenecaComputer.com

Tux

Create a simple number guessing game using the "case 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 "case 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
#########################################################################################################
#                                                                                                       #
#                                              game2.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

SECRET1=$[$SECRET-1]                             # set variable SECRET1 as $SECRET value -1
SECRET2=$[$SECRET+1]                             # set variable SECRET2 as $SECRET value +1

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

  case $INPUT in                                 # start case construct to check user input

    $SECRET)                                     # check for correct guess
      echo -e "\nGood job, that's correct.  You got it on guess number $COUNTER. \n"
      exit                                       # exit game after correct guess
    ;;

    [1-$SECRET1])                                # check for too low guess
      echo -e "\nThat was guess number "$COUNTER".  You guessed too low \n"
    ;;

    [$SECRET2-9]|10)                             # check for too high guess
      echo -e "\nThat was guess number "$COUNTER".  You guessed too high \n"
    ;;

    *)                                           # check for bad input
      echo -e "\nThat was guess number "$COUNTER" but you entered "$INPUT "which is not a number between 1 and 10. \n"
    ;;

  esac                                           # finish case construct after checking user input

  case $COUNTER in                               # start case construct to check guess counter

    5)                                           # if this is the 5th guess, perform exit steps
      echo -e "Sorry, that was your last guess.  Game over. \n"
      exit                                       # exit game after 5th guess
    ;;

  esac                                           # finish case construct after checking guess counter

  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 "game2.sh", and make it executable for the user with the command:

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