Your skill will accomplishwhat the force of many cannot PerlMonks

### Re^3: Number Guessing Game

by roboticus (Chancellor)
 on Sep 23, 2012 at 17:47 UTC ( #995224=note: print w/replies, xml ) Need Help??

in reply to Re^2: Number Guessing Game

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.

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

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++;
}

Re^4: Number Guessing Game
by randomhero1270 (Novice) on Sep 23, 2012 at 17:57 UTC

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

To select a random number in a particular range:

```my \$low=65;
my \$high=75;
my \$guess = int(rand(\$high-\$low+1))+\$low;

Since the range is 65 to 75, there are only 11 choices. So the left part chooses a number from 0 to 10, and then we add the \$low end of the range back to turn it into 65 to 75.

Update: as one popular signature goes: Figure out how you'd do it by hand, then program the computer. When you're playing against another person, you don't just guess a random number between 1 and 10 each time, do you? If you did, people would stare at you in puzzlement when you guess 5 they say "high" and then you guess 3. So think about how you do your guessing, and make the computer do it the same way.

...roboticus

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

Create A New User
Node Status?
node history
Node Type: note [id://995224]
help
Chatterbox?
 Corion sees more yaks to be shaved on the horizon. This "remote-browser" browser extension also installs on Microsoft Edge, but fails there. So I could invest time into making it work there too. [Corion]: Thus having one WWW::Mechanize that supports four browsers, Safari, FF, Chrome and Edge. [Corion]: I guess I'll burn that bridge once I get to it ;)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2018-04-19 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (72 votes). Check out past polls.

Notices?