Think about Loose Coupling PerlMonks

### Re: Number Guessing Game

by flexvault (Monsignor)
 on Sep 23, 2012 at 17:30 UTC ( #995220=note: print w/replies, xml ) Need Help??

in reply to Number Guessing Game

What is the exact question? If ran for me, except the guesses was off by 1(too high).

The script said 4 tries, when I only typed 3 times. Easy to fix!

Good Luck!

"Well done is better than well said." - Benjamin Franklin

Replies are listed 'Best First'.
Re^2: Number Guessing Game
by randomhero1270 (Novice) on Sep 23, 2012 at 17:36 UTC

I fixed the counter so now it works. My questions are, how do I make it actually guess higher when I type "low" or guess lower when I type "high" and why does it still guess 0? Here is my updated code

use strict; my (\$number, \$new_guess, \$my_hint, @guesses); my \$range = 9; my \$count = 1; \$number = int(rand(\$range)); #min of 1 and high of 10, range does 0-gi +ven int so i made the range 9 (0-9) and added one to it -> (1-10) print "My guess is: \$number \n"; print "High, low, or right?\n"; while(\$my_hint ne "right"){ chomp(\$my_hint = <STDIN>); @guesses[\$count] = \$my_hint; if (\$my_hint eq "low"){ \$new_guess = int(rand(\$number)); print "I guess: " . \$new_guess . "\n"; print "High, low, or right?\n"; } elsif (\$my_hint eq "high"){ \$new_guess = int(rand(\$number)); print "I guess: " . \$new_guess . "\n"; print "High, low, or right?\n"; } elsif (\$my_hint eq "right"){ print "It took me " . \$count . " tries\n"; } else{ print "What? Am I high, low, or right?\n"; } \$count++; }

The problem is that you're selecting a number at random throughout the range, and you're never adjusting the range. For example, if we were playing the game using 1-100 as the range and you guessed 65 and I said "low", then you'd want to guess a number between 66 and 100. If you then guessed 75 and I said "high", you'd want to guess a number between 66 and 74.

So you need to update the upper or lower limit based on the feedback from the user.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

I made it stop guessing zero. I just don't really understand how to set a new minimum or maximum and have it still guess a random number. Updated code below

use strict; my (\$number, \$new_guess, \$my_hint, @guesses); my \$range = 9; my \$count = 1; my \$number = 1 + int rand 10; #min of 1 and high of 10, range does 0-g +iven int so i made the range 9 (0-9) and added one to it -> (1-10) print "My guess is: \$number \n"; print "High, low, or right?\n"; while(\$my_hint ne "right"){ chomp(\$my_hint = <STDIN>); @guesses[\$count] = \$my_hint; if (\$my_hint eq "low"){ \$new_guess = ((1 + int rand 10); print "I guess: " . \$new_guess . "\n"; print "High, low, or right?\n"; } elsif (\$my_hint eq "high"){ \$new_guess = (1 + int rand 10); print "I guess: " . \$new_guess . "\n"; print "High, low, or right?\n"; } elsif (\$my_hint eq "right"){ print "It took me " . \$count . " tries\n"; } else{ print "What? Am I high, low, or right?\n"; } \$count++; }

thanks I understand what you are saying. How do you define a range?

Create A New User
Node Status?
node history
Node Type: note [id://995220]
help
Chatterbox?
 [choroba]: erix Yes, but in the later concert :-)

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-12-12 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (335 votes). Check out past polls.

Notices?