Number Guessing Games in PowerShell

CLICK HERE TO SEE THESE GAMES AND A MENU IN A SINGLE FILE.

This guide will show you how to make a two types of number guessing games in the coding language of PowerShell.

——————–1-10 With A Single Guess———————

Below is my code for a 1 to 10 guessing game in which the player has a single try to guess the number correctly:


function single-guess{

clear
Write-Host "You only have one guess!"
Start-Sleep -Seconds 1
Write-Host "Are you ready?"
start-sleep -Seconds 1

$random1to10 =  Get-Random -Minimum 1 -Maximum 11

do {
try {
$numOk = $true
[int]$input1 = Read-host "Make a guess between 1 and 10"
}
catch {$numOK = $false}
}
until (($input1 -ge 1 -and $input1 -lt 11) -and $numOK)

if ($input1 -eq $random1to10){
Write-Host "You got it right!"
}elseif ($input1 -ne $random1to10){
Write-Host "You got it wrong! the number was $random1to10"

Start-Sleep -Seconds 2
}}

From this you can see that I start with a little intro of “Are you ready?” and other various starting lines. I then create a random number between 1 and 11 (Actually between 1 and 10 as the -maximum in this case signifies the highest amount the number cannot be), and then put that into  a variable. I then ask the user for a number (guess) between 1 and 10 and then use an if statement to test whether the guess was the same as the randomly generated number.

Now that you have the basic 1 to 10 game, we will move onto a 1 to 100 guess game with unlimited guesses.

——————1-100 With Unlimited Guesses——————

This code is a bit different since the user can have unlimited guesses. You can see how I made this possible in the code below:


function 1to100guessgame{

clear

[int]$random100 = Get-Random -Minimum 1 -Maximum 101

Write-Host "Try and guess my number between 1 and 100"

while ($input100 -ne $random100){
Write-Host "enter your guess below"
$input100 = [int] (Read-Host)

$option1 = if ($input100 -gt $random100) {Write-Host "$input100 was too high"}
$option2 = if ($input100 -lt $random100) {Write-Host "$input100 was too low"}
$option3 = if ($input100 -gt [int](100) -or $input -lt [int](0)) {Write-Host 'between 1 and 100!'}

} Write-Host "Correct! the number was $input100"
}

In this code I still created a random number and put it into a variable. But I used a “while” statement instead of an “if” statement, meaning that it loops until the user has guessed the correct number. I still haven’t figured out how to make sure that the user inputted date is an actual number, which if it’s not then it will output an error.

The 1 to 100 guessing game also informs the user whether the number they entered is too high or too low, this helps the user guess the number quicker.

——————————–CONCLUSION——————————–

These games a fun and all but they aren’t very well accessible or re-run-able (if that’s even a word). So I think I will create a menu for these and any future games I make so that they can all be played in one place and in one file. Till then!

Remember! If you want to be able to run scripts then you need to change you execution policy. You can see how to do that here!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s