Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Adding one last conditional in a random-number guessing game

by Anonymous Monk
on Feb 22, 2014 at 01:48 UTC ( [id://1075805]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!
I am supposed to write a small script for a random-guessing number between 1-10, which guesses a number, then asks the user for a guess and if the user guesses a lower number or a higher number it keeps asking.
My question is, how can I add another requirement, which is that the number the program guesses can't be repeated...?
My code is the followind:
my $program_guess = int(rand 10)+1; while (1) { print "Please give a number between 1 and 10\n"; my $user_guess = <STDIN>; chomp $user_guess; if ($user_guess > $program_guess) { print "The number you guessed is higher, please try again!\n"; next; } elsif ($user_guess < $program_guess) { print "The number you guessed is lower, please try again!\n"; next; } print "You got the correct number!\n"; last; }

Replies are listed 'Best First'.
Re: Adding one last conditional in a random-number guessing game
by toolic (Bishop) on Feb 22, 2014 at 02:20 UTC
    Store the guesses in a hash:
    use warnings; use strict; my %guesses; my $program_guess = int(rand 10)+1; while (1) { print "Please give a number between 1 and 10\n"; my $user_guess = <STDIN>; chomp $user_guess; if (exists $guesses{$user_guess}) { print "The number you guessed is a repeat, please try again!\n +"; next; } else { $guesses{$user_guess}++; } if ($user_guess > $program_guess) { print "The number you guessed is higher, please try again!\n"; next; } elsif ($user_guess < $program_guess) { print "The number you guessed is lower, please try again!\n"; next; } print "You got the correct number!\n"; last; }
Re: Adding one last conditional in a random-number guessing game
by AnomalousMonk (Archbishop) on Feb 22, 2014 at 12:51 UTC
Re: Adding one last conditional in a random-number guessing game
by ww (Archbishop) on Feb 22, 2014 at 12:57 UTC

    So you want the record of $program_guesses carried over from game to game? You appear to confirm that in your Re^2: Adding one last conditional in a random-number guessing gamein response to hdb's Re: Adding one last conditional in a random-number guessing game.

    And do you want the guess record carried over from execution to execution of the game?

    That would entail keeping your record of the guesses -- hash, array or list -- in a (tied-)file
          ...and would mean the game could never be "played" more than 10 times!

    Update: that is "never" without making the record inaccessible to the script (in effect, deleting the record).

    Are you sure you understood your homework assignment correctly? Could it be that the user should not be allowed to guess the same number twice in the course of a single game?

    Come, let us reason together: Spirit of the Monastery
Re: Adding one last conditional in a random-number guessing game
by hdb (Monsignor) on Feb 22, 2014 at 07:47 UTC

    You want to play repeatedly and the program will not guess the same number again?

      exactly that is what I want to do... But can I do it without creating a hash?

        But can I do it without creating a hash?

        I can do it :D But I would just use a hash anyway :)

Re: Adding one last conditional in a random-number guessing game
by wind (Priest) on Feb 23, 2014 at 02:02 UTC

    Another way is to continually adjust the range of acceptable numbers.

    As you see, it's often good practice to separate constants at the top of your program so that things can be adjusted easily at a later date.

    use strict; use warnings; my $min = 1; my $max = 10; my $target = $min + int(rand ($max - $min + 1)); print "Please give a number between $min and $max\n"; while () { my $guess = <STDIN>; chomp $guess; if ($guess < $min || $guess > $max) { print "Outside of range, please give a number between $min and + $max\n"; } elsif ($guess > $target) { $max = $guess - 1; print "Too high, please give a number between $min and $max\n" +; } elsif ($guess < $target) { $min = $guess + 1; print "Too low, please give a number between $min and $max\n"; } else { print "You got the correct number!\n"; last; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found