2008 Winter Scripting Games

Solution to Beginner Windows PowerShell Event 8: Random Guess

Event 8 Solution


Windows PowerShell solution to Event 8 in the 2008 Winter Scripting Games.

Solutions are also available for VBScript and Perl.

*

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 = New-Object Random
$a = $a.Next($low,$high)

while ($true)
{
    $guess = read-host "Enter a number between 1 and 50: "
    $guesses++

    if ($guess -eq $a)
    {
        "Random Number: " + $guess
        "Number of Guesses: " + $guesses
        break
    }
    elseif ($guess -gt $a)
    {
        "Too high"
    }
    else
    {
        "Too low"
    }
}

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 = New-Object Random
$a = $a.Next($low,$high)

This part was pretty easy to figure out –if you read the Scripting Games Tips. You can generate a random number in Windows PowerShell by using the New-Object cmdlet to create an instance of the Random object, then calling the Next method on that object. The Next method takes two parameters: the starting and ending numbers in the range from which the random number will be generated.

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

while ($true)

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 continues as long as $true is true, which is basically 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:

$guess = read-host "Enter a number between 1 and 50: "

Not only are we displaying instructions, but we’re also calling the Read-Host cmdlet to read the number that is entered in response to this prompt. Whatever the user enters will be assigned to the variable $guess.

Next we increment the counter that’s keeping track of how many tries it takes to guess the correct number:

$guesses++

If you’re not used to it this syntax might look a little strange. But this is just Windows PowerShell 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 -eq $a)

Here’s we simply checking to see if the number guessed is equal to (-eq) the random number. If it is, we display the random number along with the number of tries it took to guess correctly:

"Random Number: " + $guess
"Number of Guesses: " + $guesses

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 break statement:

break

The break 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 elseif statement:

elseif ($guess -gt $a)
{
    "Too high"
}

If the guess wasn’t equal to the random number, we fall into the elseif statement and check to see if the guess is greater than (-gt) 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:

else
{
    "Too low"
}

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