giladh

Random Number Guessing Game - Bash Script

Jul 6th, 2011
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.20 KB | None | 0 0
  1. #!/bin/bash -e
  2. #
  3. #This is a random number guessing game
  4.  
  5. #Declare variables and create scorefiles
  6. SCOREFILE="/tmp/scorefile"
  7. TMPFILE="/tmp/tmpscorefile"
  8. if [[ ! -f "$SCOREFILE" ]]; then
  9.    touch "$SCOREFILE"
  10. fi
  11.  
  12.  
  13. ### BEGIN FUNCTIONS ###
  14.  
  15. #Generate new statistics
  16. genstats () {
  17.    cat "$SCOREFILE" | sort -k1n | head -10 >"$TMPFILE" && mv "$TMPFILE" "$SCOREFILE"
  18.    HSCORETEN=$(cat "$SCOREFILE")
  19.    HSCORENUM=$(head -1 "$SCOREFILE" | awk -F',' '{ print $1 }')
  20.    HSCOREPER=$(head -1 "$SCOREFILE" | awk -F',' '{ print $2 }')
  21.    HSCORETHR=$(tail -1 "$SCOREFILE" | awk -F',' '{ print $1 }')
  22.    echo -e "The Top 10 scorers are:\n" && \
  23.    awk -F',' 'BEGIN { printf "%-10s %-10s %-10s\n", "RANK","NAME","SCORE"
  24.                      printf "%-10s %-10s %-10s\n", "----","----","-----" }  
  25.             { printf "%-10s %-10s %-10s\n", " "NR".",$2," "$1 }' "$SCOREFILE"
  26.    echo ""             
  27. }
  28.  
  29.    
  30. #Get player's guess and Validate (nested func)
  31. getguess () {
  32.    valguess () {
  33.       while ! [[ "$GUESS" -lt "101" ]] || ! [[ "$GUESS" -gt "0" ]]; do
  34.         read -p ""$PLAYER", That was not a valid guess.  Please enter an integer between 1-100: " GUESS
  35.       done  
  36.    }      
  37.  
  38.    if [[ "$g" -eq "1" ]]; then # if this is the first guess
  39.      read -p "Hi "$PLAYER"... Please enter your first guess: " GUESS
  40.      valguess
  41.      let FIRSTGUESS="$GUESS"
  42.    else
  43.      read -p "Enter a new guess: " GUESS
  44.      valguess
  45.      let NEWGUESS="$GUESS"
  46.    fi
  47. }
  48.  
  49.  
  50. #Generate the random number
  51. gennumber () {
  52.    let R=$RANDOM%100
  53. }
  54.  
  55.  
  56. #Calculate the difference between the players guesses as absolute value
  57. gdiff () {
  58.    OLDGDIFF=$(($R - $OLDGUESS))   #calculate diff btwn oldguess and number
  59.    NEWGDIFF=$(($R - $NEWGUESS))   #calculate diff btwn newguess and number
  60.  
  61.    if [[ "$OLDGDIFF" -lt 0 ]]; then
  62.       let "ABSVAL_OLDGDIFF=( 0 - $OLDGDIFF )"          
  63.    else
  64.       let ABSVAL_OLDGDIFF="$OLDGDIFF"
  65.    fi  
  66.    if [[ "$NEWGDIFF" -lt 0 ]]; then
  67.       let "ABSVAL_NEWGDIFF=( 0 - $NEWGDIFF )"          
  68.    else
  69.       let ABSVAL_NEWGDIFF="$NEWGDIFF"
  70.    fi
  71. }
  72.  
  73.  
  74. #Calculate high scores
  75. calcscore () {
  76.    if [[ "$g" -lt "$HSCORENUM" ]]; then
  77.      echo -e "CONGRATULATIONS "$PLAYER"... You have the new high score!"  
  78.      echo -e "The previous holder of this record was "$HSCOREPER"\n"
  79.    elif [[ "$g" -eq "$HSCORENUM" ]]; then
  80.      echo -e "Congratulations "$PLAYER"... You are tied with "$HSCOREPER" for 1st place!\n"  
  81.    elif [[ "$g" -lt "$HSCORETHR" ]]; then
  82.      echo -e "Congratulations "$PLAYER"... You made it into the Top 10 list!\n"
  83.    else    
  84.      echo -e "I'm sorry "$PLAYER", you did not make the Top 10 list this time. Please try again!\n"
  85.    fi
  86.    echo ""$g","$PLAYER"" >>"$SCOREFILE"  
  87.    genstats
  88. }
  89.  
  90.  
  91. #Repeat game function
  92. repeat () {
  93.   read -n 1 -p "Play Again? (Y/N): " REPEAT
  94.   case $REPEAT in
  95.     Y|y)
  96.       echo ""  
  97.       gennumber && playgame
  98.       ;;
  99.     N|n|*)
  100.       echo -e "\n\nThanks for playing!\n"
  101.       exit 0   
  102.       ;;
  103.   esac
  104. }
  105.  
  106.  
  107. #Play the game
  108. playgame () {
  109.    #To see the number for debugging, uncomment the following line
  110.    #echo "the random number is "$R""
  111.    echo -e "\nThis is a guessing game...  You are to guess a number between 1-100...\n"
  112.    genstats
  113.    if [[ ! "$PLAYER" ]]; then
  114.      read -p "Enter your name: " PLAYER
  115.    fi  
  116.    echo ""
  117.    let g=1 && getguess
  118.      
  119.    if [[ "$FIRSTGUESS" = "$R" ]]; then
  120.       echo -e "\n"$PLAYER", You must be very special... You guessed it on the first try!\n"
  121.       calcscore && repeat
  122.    else
  123.       let OLDGUESS="$FIRSTGUESS"       
  124.       echo "Sorry, please try again..."
  125.       let g=2 && getguess
  126.  
  127.       for ((g=2; NEWGUESS != "$R"; ++g)); do
  128.      gdiff  # call func gdiff to calculate difference between guesses    
  129.      if [[ "$ABSVAL_OLDGDIFF" -lt "$ABSVAL_NEWGDIFF" ]]; then      
  130.             let OLDGUESS="$NEWGUESS"
  131.         echo "You're getting colder..."
  132.         getguess
  133.          else
  134.             let OLDGUESS="$NEWGUESS"  
  135.             echo "You're getting warmer..."
  136.         getguess
  137.          fi  
  138.       done
  139.  
  140.       echo -e "\n"$PLAYER"... You guessed it in "$g" tries!\n"
  141.       calcscore && repeat
  142.    fi
  143. }
  144.  
  145. ### END FUNCTIONS ###
  146.  
  147. # Generate the random number and start the game
  148. gennumber && playgame
Advertisement
Add Comment
Please, Sign In to add comment