More Games In PowerShell

This is, yet again, another improvement to a script I created earlier, you can find this here.

What I have done is added some new games (3 to be exact) and also created some unnecessary “fancy” bits such as a loading screen and a “Goodbye” exiting message. I have also changed the menu to be a single “echo” command instead of the multiple “write-host” which makes the code look cleaner.

The three games I have added are pretty much all the same game. I was getting sick of guessing games and since making actual games in PowerShell is VERY complex. I decided to make a math game. Specifically one that give the user a math question and the user has to work out the correct answer. An example would be “4 * ? = 12”. Where the user would have to work out “?”.

I made three difficulties of this, easy (1-10), medium (1-20) and hard (1-50). This means that the answers in the easy section are from 1 to 10, the answers in the medium are from 1 to 20 and so on…

I also added a function to change the size of the window to something more appropriate and changed the window title to something more…fun.

Anyway, below is the code for this. Unfortunately, I didn’t break the “300 lines of code” achievement I had set for myself:

function changescreensizeforgames{
 $pshost = get-host
 $pswindow = $pshost.ui.rawui
 $newsize = $pswindow.buffersize
 $newsize.height = 500
 $newsize.width = 500
 $pswindow.buffersize = $newsize
 $newsize = $pswindow.windowsize
 $newsize.height = 15
 $newsize.width = 51
 $pswindow.windowsize = $newsize
 $pswindow.WindowTitle = "Games Galore!"
}
changescreensizeforgames

function fakeloadingbar{
 cls
 echo "-....."
 Start-Sleep -Milliseconds 150
 cls
 echo ".-...."
 Start-Sleep -Milliseconds 150
 cls
 echo "..-..."
 Start-Sleep -Milliseconds 150
 cls
 echo "...-.."
 Start-Sleep -Milliseconds 150
 cls
 echo "....-."
 Start-Sleep -Milliseconds 150
 cls
 echo ".....-"
 Start-Sleep -Milliseconds 150
 cls
 echo "....-."
 Start-Sleep -Milliseconds 150
 cls
 echo "...-.."
 Start-Sleep -Milliseconds 150
 cls
 echo "..-..."
 Start-Sleep -Milliseconds 150
 cls
 echo ".-...."
 Start-Sleep -Milliseconds 150
 cls
 echo "-....."
 Start-Sleep -Milliseconds 150
 cls
}
fakeloadingbar

function exitscreenforgames{
 cls
 echo @"
####   #    # #####  ##
#   #   #  #  #      ##
#####    ##   ####   ##
#    #   ##   #      ##
#    #   ##   #
#####    ##   #####  ##
"@
 Start-Sleep -Seconds 1
 exit
}

#Window size change

#INTRO (Start Menu)
function menu2{

function Show-Menu{
 param (
 [string]$Title = 'My Menu'
 )
 Clear-Host
 write-host @"
================ $Title ================

Guessing Games------------
1: Press '1' to play 1-10 with 1 guess
2: Press '2' for 1-100 with unlimited guesses

Multiplication Games------
3: Press '3' for easy mulitplication game (1-10)
4: Press '4' for medium multiplication game (1-20)
5: Press '5' for hard multiplication game (1-50)

Q: Press 'Q' to quit.
"@
 }

 Show-Menu -Title 'Select an option'
 $selection = read-host "please make a selection"

 if ($selection -eq "1"){
  write-host "You chose option one" (1to10guessgame)
 }elseif ($selection -eq "2"){
 Write-Host "you chose option two" (1to100guessgame)
 }elseif ($selection -eq "3"){
  write-host "You chose option three" (easymathgame)
 }elseif ($selection -eq "4"){
  write-host "You chose option four" (mediummathgame)
 }elseif ($selection -eq "5"){
  write-host "You chose option four" (hardmathgame)
 }elseif ($selection -eq "q"){
  clear-host
 Do {$areyousuremessage = Read-Host "Are you sure you want to quit? Y or N?"} while (("y","n") -notcontains $areyousuremessage)
  switch ($areyousuremessage){

   "y" {exitscreenforgames}
   "n" {menu2}

  }

 }else {
  (menu2)
 }
}

#Single guess game
function 1to10guessgame{

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

 $random =  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 $random){
  Write-Host "You got it right!"
 }else{
  Write-Host "You got it wrong! the number was $random"
 }

 Start-Sleep -Seconds 2

 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

 if ($playagain1 -eq "y"){
  (1to10guessgame)
 }elseif ($playagain1 -eq "n"){
  (menu2)
 }

}

#1-100 Guess game
function 1to100guessgame{

 clear-host

 [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"

 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

 if ($playagain1 -eq "y"){
  (1to100guessgame)
 }elseif ($playagain1 -eq "n"){
  (menu2)
 }
}

#easy math game 1-10
function easymathgame{
 $easynewnumber1 = get-random -minimum 1 -maximum 11
 $easynewnumber2 = Get-Random -Minimum 1 -Maximum 11

 $easynumbermultiplied = $easynewnumber1 * $easynewnumber2
 Clear-Host
 Write-Host "Here is your question, can you work out '?'"
 write-host "$easynewnumber2 * ? = $easynumbermultiplied"

 do{
  try{
   $numOk = $true
   [int]$easynewnumberinput = Read-Host "Enter your answer here..."
  }catch {$numOk = $false}
 }until (($easynewnumberinput -ge 1 -and $easynewnumberinput -lt 11) -and $numOk)

 if ($easynewnumberinput -eq $easynewnumber1){
  Write-Host "You got it right!"
 }elseif ($easynewnumberinput -ne $easynewnumber1){
  Write-Host "You got it wrong, the answer was $easynewnumber1"
 } Start-Sleep -Seconds 1

 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

 if ($playagain1 -eq "y"){
  easymathgame
 }elseif ($playagain1 -eq "n"){
  (menu2)
 }
}

#medium math game 1-20
function mediummathgame{ 
 $mediumnewnumber1 = get-random -minimum 1 -maximum 20
 $mediumnewnumber2 = Get-Random -Minimum 1 -Maximum 20

 $mediumnumbermultiplied = $mediumnewnumber1 * $mediumnewnumber2
 Clear-Host
 Write-Host "Here is your question, can you work out '?'"
 write-host "$mediumnewnumber2 * ? = $mediumnumbermultiplied"

 do{
  try{
   $numOk = $true
   [int]$mediumnewnumberinput = Read-Host "Enter your answer here..."
  }catch {$numOk = $false}
 }until (($mediumnewnumberinput -ge 1 -and $mediumnewnumberinput -lt 20) -and $numOk)

 if ($mediumnewnumberinput -eq $mediumnewnumber1){
  Write-Host "You got it right!"
 }elseif ($mediumnewnumberinput -ne $mediumnewnumber1){
  Write-Host "You got it wrong, the answer was $mediumnewnumber1"
 } Start-Sleep -Seconds 1
 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

 if ($playagain1 -eq "y"){
  mediummathgame
 }elseif ($playagain1 -eq "n"){
  (menu2)
 }
}

#Difficult math game 1-50
function hardmathgame{
 $hardnewnumber1 = get-random -minimum 1 -maximum 50
 $hardnewnumber2 = Get-Random -Minimum 1 -Maximum 50

 $hardnumbermultiplied = $hardnewnumber1 * $hardnewnumber2
 Clear-Host
 Write-Host "Here is your question, can you work out '?'"
 write-host "$hardnewnumber2 * ? = $hardnumbermultiplied"

 do{
  try{
   $numOk = $true
   [int]$hardnewnumberinput = Read-Host "Enter your answer here..."
  }catch {$numOk = $false}
 }until (($hardnewnumberinput -ge 1 -and $hardnewnumberinput -lt 51) -and $numOk)

 if ($hardnewnumberinput -eq $hardnewnumber1){
  Write-Host "You got it right!"
 }elseif ($hardnewnumberinput -ne $hardnewnumber1){
  Write-Host "You got it wrong, the answer was $hardnewnumber1"
 } Start-Sleep -Seconds 1
 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

 if ($playagain1 -eq "y"){
  hardmathgame
 }elseif ($playagain1 -eq "n"){
  (menu2)
 } 
}
menu2

Hopefully you’re as happy with this code as I am, if you have any improvements please let me know 🙂

Enjoy!


Leave a Reply