2008 Winter Scripting Games

Solution to Beginner Perl Event 8: Random Guess

Event 8 Solution


Perl solution to Event 8 in the 2008 Winter Scripting Games.

Solutions are also available for VBScript and Windows PowerShell.

*

Event 8 – Random Guess

To complete this event you needed to learn several things, including how to generate random numbers and how to read input from the command line. Here’s how we solved this event:

$guesses = 0;

$low = 1;
$high = 50;

$a = int(rand($high) + $low);

while ()
{
    print "\nEnter a number from 1 to 50: ";
    $guess = <STDIN>;
    chomp;

    $guesses++;

    if ($guess == $a)
    {
        print "Random Number: $a\n";
        print "Number of Guesses: $guesses\n";
        last;
    }
    elsif ($guess > $a)
    {
        print "Too high\n";
    }
    else
    {
        print "Too low\n";
    }
}

We start the script by initializing some variables:

$guesses = 0;

$low = 1;
$high = 50;

We’ll use these variables to keep track of the total number of tries it takes to guess the random number ($guesses) and the range ($low to $high) for the randomly-generated number. Next we generate that random number:

$a = int(rand($high) + $low);

This part was pretty easy to figure out – if you read the Scripting Games Tips. You can generate a random number in Perl by calling the rand function, passing it the highest value the number could be, then adding the result to the lowest number it could be. We then pass the entire thing to the int function to make sure the number is a whole number.

Now that we have a randomly-generated number (stored in the variable $a), we enter an endless loop:

while ()

We want the script to continue until the user enters the correct number (the number that matches the randomly-generated number). We do this by putting everything into a while loop that has no ending condition, which means the loop continues forever. We’ll see in a moment how we get out of this seemingly-endless loop.

The first thing we do inside the loop is display a message to the user instructing him or her on what they need to do:

print "\nEnter a number from 1 to 50: ";

We start with a newline character (\n) to separate each line of output, then simply prompt for a number. Next comes this line:

$guess = <STDIN>;

This line of code waits for the user to enter something. When he or she does that and presses ENTER, the script reads the value that was entered and assigns it to the variable $guess.

This next line is an interesting one:

chomp;

When the user enters a number then presses ENTER, not only is the number read in but so is the ENTER. In other words if the user entered the number 10, the variable $guess would be equal to 10 followed by a newline character. The numeric comparisons we’re going to need to do won’t work well with a newline character tacked on to the end of this number, so we need to remove it. That’s exactly what the chomp function does; it removes the trailing character from the last statement. After executing the chomp statement our $guess variable contains the number entered, and only the number.

The use has made a guess as to what the random number is, so we increment our guess counter:

$guesses++;

If you’re not used to it this syntax might look a little strange. But this is just Perl shorthand for this:

$guesses = $guesses + 1;

Now it’s time to compare the number entered to the random number. We start with this if statement:

if ($guess == $a)

Here we’re simply checking to see if the number guessed is equal to the random number. Notice the double equal sign (==). This is the numeric equality comparison operator. If we were to use a single equal sign here, the if statement would be assigning the value of $a to $guess, and the statement would always be true. In order to compare $a to $guess for equality, we need to use the double-equal sign.

If the two numbers are equal, we display the random number along with the number of tries it took to guess correctly:

print "Random Number: $guess\n";
print "Number of Guesses: $guesses\n";

The next line is very important. Remember how we said we were in an endless loop? Well, when the user guesses the number correctly we want to break out of the loop, which in this case will end the script. We do that with the last statement:

last;

The last statement will immediately end whatever loop you’re in. Because there’s no code in our script after the end of the loop, the script ends.

So what happens if the user doesn’t guess the number correctly? Well, we need to do something else, which, nicely enough, takes us to our elsif statement (notice the spelling – no e at the end of else in elsif):

elsif ($guess > $a)
{
    print "Too high\n";
}

If the guess wasn’t equal to the random number, we fall into the elsif statement and check to see if the guess is greater than (>) the random number. If the guess is higher we simply display a message saying “Too high”. We then go back to the top of the loop and ask the user to enter another number.

If the numbers aren’t equal, and the guess isn’t too high, then it must be too low, which takes us to the else statement (we get to spell else correctly this time):

else
{
    print "Too low\n";
}

If the guess is lower than the random number, we display the message “Too low” and once again go back to the beginning of the loop and ask for another number.

And that’s it. The script will display “Too High” or “Too Low” as long as the user continues to enter numbers. If the user guesses correctly a message will display that fact and the script ends.

Now, if that’s not the most fun you’ve had playing a game in a long time, well, you obviously have no concept of how to actually have fun.


Top of pageTop of page