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

I am doing the guess a number game I have it running correctly the only thing I can not figure out is how to track the numbers guessed. It is suppose to keep track of the numbers the user guess and print an error message if they guess the same number more than once. This is what I have so far:

do { @numbers=(1..10); $index=int (rand (10)); $rannumber="$numbers[$index]\n"; game($rannumber); print "Would you like to play again(Y or N) "; do { chomp($answer=<STDIN>); $answer=uc($answer); if (($answer ne "Y") and ($answer ne "N")){ print "Invalid entry - PLEASE SELECT Y OR N\n"; } }while (($answer ne "Y") and ($answer ne "N")); } until ($answer eq "N"); sub game { my $rannumber=@_[0]; print "Guess a number between (1-10) "; do { my $guess=<STDIN>; @allguess=$guess; $track = grep(!/1..10/, $allguess); if ($track = $allguess) { print "Same guess try again\n"; } if ($guess > 10) { print "Invalid entry - Guess Again\n"; } elsif ($guess < 1) { print "Invalid entry - Guess Again\n"; } elsif ($guess == $rannumber) { print "You Win\n";} else {print "Guess Again\n";} } until ($guess == $rannumber); return; }

Replies are listed 'Best First'.
Re: Help with Guessing Game
by choroba (Cardinal) on May 11, 2013 at 13:11 UTC
    $allguess is not the same as @allguess. You are mixing sigils. You should use strict to avoid these problems. Moreover,
    @allguess = $guess
    overwrites the previous value of @allguess. Use push to add new elements to arrays.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      The problem domain seems only to be limited to numbers between one and ten. Forget push :) Just use an array as a boolean vector.

      if ( $test[$guess]++ ){ print "Duplicate\n"; }

      Dave

      I'm really confused at this point, my instructor told me to get all the guessing and put them in an array so are you saying I should use push to add onto the @allguess array each time the user enters a number. As you can see I'm very new to this, the other problem I'm having is using the grep function to see if the number has been guessed.

        1. I don't understand the intent of your first sentence here. Are you objecting that the PM solution offered (seems to you) to vary from your instructor's directions? If so, trust choroba's trouble-shooting. Push (see also next graf) allows you to add new elements to an array. When inside a loop, assigning a value to the array REPLACES whatever was in the array on the last iteration of the loop.
        2. Perhaps your instructor mentioned using your command line to obtain local help;
          C:\> perldoc -f push
          or
          C:\> perldoc -f grep
        There are many more sources of help local to your computer. See, for instance, perldoc perldoc and many others.

        If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: Help with Guessing Game
by BillKSmith (Monsignor) on May 12, 2013 at 03:47 UTC

    Your program has another serious bug. After it reports a win, it does not start a new game, but rather continues to prompt you to "Guess Again". Use strict and use warnings would find and report the error. (You have two variables named $guess) Of course this message tends to get lost in a sea of less serious error messages. Always use strict and warnings and fix all the errors that they report.

    I have several comments on your style.

    Most people (except perhaps old FORTRAN programers) find perl's do...while to be a bit quirky. Use them only when the offer a real advantage.

    Do not append a newline to $rannumber. Better to chomp the newline from $guess.

    You should separate code for validating input from real game code. Best to use a prompt modulue with a call back routine for validation.

    UPDATE: Corrected typos

    Bill

      Best to use a prompt module with call back for validation.

      Sounds to me like IO::Prompt::Hooked (shameless plug).


      Dave

        Actually, I use ActiveState::Prompt. I will checkout your suggestion.

        You also made a great suggestion for keeping track of previous guesses.

        Bill