Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Number Guessing Game

by tobyink (Abbot)
on Sep 23, 2012 at 20:19 UTC ( #995245=note: print w/ replies, xml ) Need Help??


in reply to Number Guessing Game

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'


Comment on Re: Number Guessing Game
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-07-23 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (146 votes), past polls