Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range
# 1  
Old 05-11-2014
Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2) asks the user to guess the value of the rolled dies, which would be in the range of 2 to 12 ($guess_value); (3) script generates a random roll of the 2 die ($RANDOM % $Spots +1”); (4) tell user the random value rolled ($roll_value); (5) compare user's guess ($guess_value) to random roll ($roll_value), telling user if values are equal, the game is won, otherwise, game is lost.


I, originally, wrote the script without verifying that the user enters a number within the range of 2 to 12, which works as intended (see #3 below). Then, I added an if...else loop to verify user's entry (i.e. ask for another entry if not in range), but do not attain the result expected if a number outside the range of 2 to 12 is entered.

Could someone please assist with this verification loop? Thank You.




2. Relevant commands, code, scripts, algorithms:

Below is the script that I'm having an issue with:
Code:
  clear
  die1=0
  die2=0
  Spots=6
   
  echo “Hello.  Welcome to Random Roll.”
  echo “The object of Random Roll is to guess the value of the 2 die rolled.”
  echo “You will be rolling 2 die.  Each of the two die have 6 sides.”
   
  echo “Please enter a number between  2 and 12.”
                          #max side of 6 could give a minimum roll value of 2 and a maximum roll value of 12
              read guess_value
   
  echo “You have guessed that you will roll a” $guess_value
   
   
  #Sanitize number input to ensure number is 2 to 12

if [[ $guess_value –lt 2 && $guess_value –gt 12 ]]
            then
            echo “Sorry.  Your guess is not within the range of 2 to 12.  Please enter a number in the range of 2 to 12.”
            else
            echo “You have guessed that you will roll a” $guess_value.”
fi

read guess_value
   
  echo “The dice have been rolled.”
  echo “Rolling….”
  echo “       Rolling…”
  echo “             Rolling…”
   
  let “die1 = $RANDOM  %  $Spots +1”                                                                     #die 1 roll
  let “die2 = $RANDOM  %  $Spots +1”                                                                     #die 2 roll
   
   
  let roll_value=die1+die2
   
  echo “You have rolled” $roll_value
   
  if test $roll_value –eq $guess_value
              then
              echo “Congratulations.  You have won Random Roll.”
              else
              echo “Sorry.  You have lost Random Roll.  Thank you for playing.”


3. The attempts at a solution (include all code and scripts):

The script works fine without the loop trying to verify that the user's guess is within the range of 2 to 12. That script is:
Code:
  clear
  die1=0
  die2=0
  spots=6
   
  echo “Hello.  Welcome to Random Roll.”
  echo “The object of Random Roll is to guess the value of the 2 die rolled.”
  echo “You will be rolling 2 die.  Each of the two die have 6 sides.”
   
  echo “Please enter a number between  2 and 24.”
                          #max side of 6 could give a minimum roll value of 2 and a maximum roll value of 12
              read guess_value
   
  echo “You have guessed that you will roll a” $guess_value
   
  echo “THE DICE ARE ROLLING...”
  echo “Rolling….”
  echo “       Rolling…”
  echo “             Rolling…”
   
  let “die1 = $RANDOM  %  $Spots +1”                                                                     #die 1 roll
  let “die2 = $RANDOM  %  $Spots +1”                                                                     #die 2 roll
   
  echo “Die 1 value is $die1.”
  echo “Die 2 value is $die2.”
   
  let roll_value=die1+die2
   
  echo “You have rolled” $roll_value
   
  if test $roll_value –eq $guess_value
              then
              echo “Congratulations.  You have won Random Roll.”
              else
              echo “Sorry.  You have lost Random Roll.  Thank you for playing.”
  fi


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Baker College of Auburn Hills (Auburn Hills, MI); U.S.A. Instructor: David Koppy; Lux211


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by Don Cragun; 05-12-2014 at 05:08 AM.. Reason: Remove hundreds of FONT tags; add CODE and ICODE tags.
# 2  
Old 05-12-2014
You should use the logical operator || instead of &&.
# 3  
Old 05-12-2014
Question Simple Solution: Bash Script for Dice Game; Issue with if...else loop to verify user guess is within

Good Morning Franklin52,

Thank you for the reply and solution. Omitting the && within the loop in question and adding the logical operator || instead, did the trick. Can't believe that the solution was so simple. Being new to bash scripting, I thought for sure that the loop structure was incorrect.

Again, Thank You.

Lauren
# 4  
Old 05-13-2014
Quote:
Originally Posted by LaurenRose
Good Morning Franklin52,

Thank you for the reply and solution. Omitting the && within the loop in question and adding the logical operator || instead, did the trick. Can't believe that the solution was so simple. Being new to bash scripting, I thought for sure that the loop structure was incorrect.

Again, Thank You.

Lauren
You're welcome.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Proper distribution of cards in terminal based crazy8's game in bash script

When I run the following script at the bottom it say cards remaining=44...It should be=35. Can anyone tell me what I'm doing wrong. I've spent hours trying to get this to work and I can't go any further until this part works. thank you in advance Cogiz #!/bin/bash # Date="November, 2016" #... (2 Replies)
Discussion started by: cogiz
2 Replies

2. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

3. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

4. Shell Programming and Scripting

Give user 5 chances to guess my favorite color

I wrote a script to give a user 5 guesses on what is my favorite color but I it doesn't work. I've only been scripting for a couple weeks and need some help it seems simple but how do I give the user 5 guesses? (3 Replies)
Discussion started by: noob
3 Replies

5. Shell Programming and Scripting

bash script range calculations

Hi, I have data in the following form: AB001 10 AB002 9 AB003 9 etc AB200 5 What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure... (3 Replies)
Discussion started by: chrissycc
3 Replies

6. UNIX for Advanced & Expert Users

Verify file was sftp'd via bash script

Hello Experts, I have a script that that transfers a file (via sftp) and it works fine but we ran into a snag where the target server asked for the ssh key and the script didn't know what to do. I want to add some logic to this script that at least sends an email that it didn't complete as... (4 Replies)
Discussion started by: Tiberius777
4 Replies

7. Shell Programming and Scripting

Bash script to test IP range on server

Hello, We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately. I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Discussion started by: boxgoboom
4 Replies

8. Shell Programming and Scripting

Bash Script for "simple" racing game.

Hello All, I was wondering if it would be possible to create a "racing" game in script. The game play would be as follows. Script will read the following input: Start |b| | | | | |r| | | | | First player (b) will roll a die to see how many spaces to move. This is will continue until the... (0 Replies)
Discussion started by: jl487
0 Replies

9. Shell Programming and Scripting

Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry. I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Discussion started by: spartiati
4 Replies

10. Shell Programming and Scripting

Bash Script w/ IP Range

Is there a basic IP range script or a site that has basic script written? I am making a script that is looking for a device on my network threw HTTP://. I and using a for loop with wget to download the page and using grep to search the code for the serial number. My range is 172.16.x.x/16 I... (3 Replies)
Discussion started by: captaindoogles
3 Replies
Login or Register to Ask a Question