Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Number Guessing Game

by randomhero1270 (Novice)
on Sep 23, 2012 at 17:10 UTC ( #995217=perlquestion: print w/ replies, xml ) Need Help??
randomhero1270 has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I am making a guessing game where the program guesses a number one through ten and the user inputs "high", "low", or "right". If the input is "high" the program guesses a lower number. If the input is "low" the program guesses a higher number. If the input is "right" the program prints a message and how many tries it took. I can get it to guess numbers but it doesn't guess lower if I type "high" and it doesn't guess higher if I type "low". My code is below.

use strict; my ($number, $new_guess, $my_hint, @guesses, $count); my $range = 9; $number = int(rand($range+1)); #min of 1 and high of 10, range does 0- +given 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 " . scalar @guesses . " tries\n"; } else{ print "What? Am I high, low, or right?\n"; } foreach $number (@guesses){ $count++; } }

Comment on Number Guessing Game
Download Code
Re: Number Guessing Game
by flexvault (Parson) on Sep 23, 2012 at 17:30 UTC

    randomhero1270,

    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

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

        randomhero1270:

        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.

Re: Number Guessing Game
by tobyink (Abbot) on Sep 23, 2012 at 20:19 UTC
    use v5.14; package TheGame { use Moo; use POSIX qw(floor ceil); has in => (is => 'ro', default => sub { \*STDIN }); has out => (is => 'ro', default => sub { \*STDOUT }); has lower_bound => (is => 'rw'); has upper_bound => (is => 'rw'); has guess_count => (is => 'rw', default => sub { 0 }); has guess_technique => (is => 'ro', default => sub { 'random' }); sub play { my $self = shift->new(@_); printf {$self->out} ( "Choose a number between %d and %d!\n", $self->lower_bound, $self->upper_bound, ); sleep(2); GUESS: { my $guess = $self->make_guess; my $wrong = $self->analyse_guess($guess); if ($wrong and $self->upper_bound <= $self->lower_bound) { die "stop cheating!"; } if ($wrong > 0) { # too high $self->upper_bound($guess - 1); redo GUESS; } if ($wrong < 0) { # too low $self->lower_bound($guess + 1); redo GUESS; } } printf {$self->out} ( "Got it in %d guesses!\n", $self->guess_count, ); } sub make_guess { my $self = shift; state $toggle = 0; my $guess; if ($self->guess_technique =~ /good/) { my $diff = $self->upper_bound - $self->lower_bound; $guess = floor( + ($self->upper_bound / 2) + ($self->lower_bound / 2) + ($diff / 10) -rand($diff / 5) ); } elsif ($self->guess_technique =~ /halves/) { $guess = floor( + ($self->upper_bound / 2) + ($self->lower_bound / 2) ); } elsif ($self->guess_technique =~ /thirds/) { my $upper_or_lower = (++$toggle % 2) ? $self->upper_bound : $self->lower_bound; $guess = floor( + ($self->upper_bound / 3) + ($self->lower_bound / 3) + ($upper_or_lower / 3) ); } else # random number in the range { $guess = floor( + $self->lower_bound + rand($self->upper_bound - $self->lower_bound) ); } $guess++ while $guess < $self->lower_bound; $guess-- while $guess > $self->upper_bound; $self->guess_count( $self->guess_count + 1 ); return $guess; } sub analyse_guess { my ($self, $guess) = @_; printf {$self->out} ( "My guess is %d. Am I 'too high', 'too low' or 'right'? " +, $guess, ); ANSWER: { my $in = $self->in; my $answer = <$in>; return 0 if $answer =~ m{ (right) | (correct) }x; return 1 if $answer =~ m{ (too \s* high) | (lower) }x; return -1 if $answer =~ m{ (too \s* low) | (higher) }x; print {$self->out} "Sorry... what was that? "; redo ANSWER; } } } TheGame->play(lower_bound => 1, upper_bound => 1000, guess_technique = +> 'good');
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Number Guessing Game
by Shaveta_Chawla (Acolyte) on Sep 24, 2012 at 07:11 UTC
    my $random = int (rand(100)); $count = 0; while ($random != $choice) { print "enter your choice = "; $choice = <STDIN>; $count++; print "you entered= $choice"; if($choice == $random) { print "Bravo-------------------you win"; print "\nTotal number of counts= $count\n"; exit; } elsif ($choice gt $random) { print "\nyour choice is TOO High\n"; } else { print "\nyour choice is too low\n"; } print "\nto quit press q OR press enter to continue "; $value = <STDIN>; if($value eq "q\n") { print "Sorry!! you lost :-(\n"; print "\nTotal number of counts= $count\n"; exit; } }
      That's not gonna work. gt, eq, etc are for strings, not for numbers.




      I'm too lazy to be proud of being impatient.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995217]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-11-27 23:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (190 votes), past polls