Games In PowerShell – Version 4

Quite a large update to my Games in PowerShell project, adding a new game, changing some of the styling of the game and changing the loading screen of the game.

You’ll be pleased and proud to know that the games code is now over a thousand lines long…At least fake being proud then.

As mentioned earlier, I have added a new game. This one is different from my other games because it is a text based adventure game about a dino that needs capturing. The player only successfully completes the game once they have captured the dino and also not been killed by it. Tall order, I know.

I also changed how the Quick Fire Character Counting game works. It now starts by giving the player a full second to see the word and guess how many characters it contains, then, each time the user gets a correct answer reduces the viewing time of the word by 30 miliseconds. That way the game gradually gets more difficult instead of being stuck at the same length, which is boring.

Here is the link to my code for the game

I will be updating my project page with this update as well. Enjoy!

Verb Guessing Game in PowerShell

Verb guesser Featured image

Another game today. In PowerShell of all things. Who would have thought?

Anyway, in this blog post I will be showing you the code I used to create a random word guessing in PowerShell. The script works by taking all the verb information from the

Get-Verb

command and selecting only the verb part of each one.

The script then selects on at random, does some formatting and removes the vowels. That part is important. Pretty much is the single defining feature that makes this thing a game. With the vowels now removed, the user has to guess the verb. If they get it correct then a point is added to their score. If they get it wrong then the game resets and they start again from 0 points. (Well, 1 point if you’re being technical)

Here the code:

function verbguessinggame{
 $getverbs = Get-Verb | select verb
 [int]$verbamount = $getverbs.count
 $score = 0
 do{
  $random = Get-Random -Minimum 0 -Maximum $verbamount
  $selectedword = $getverbs[$random] -replace "@{verb=","" -replace "}",""
  $selectedwordlength = $selectedword.length
  $vowelsremoved = $selectedword -replace '[aeiou]',""

  $score = $score + 1
  Clear-Host
  Write-Host $vowelsremoved -ForegroundColor Red -BackgroundColor Black
  $guess = Read-Host "What is the word?"
 }until($guess -ne $selectedword)
 Clear-Host
 Write-Host "incorrect, the verb was $selectedword"
 Write-Host "Your score was $score"
 Start-Sleep -Seconds 1.5
 Clear-Host

 do {$playagainselection = Read-Host "Do you want to play again? (Y or N)"} while (("y","n") -notcontains $playagainselection)
 if ($playagainselection -eq "y"){
  verbguessinggame
 }elseif ($playagainselection -eq "n"){
  exit
 }
}

verbguessinggame

I will be updating my Games in PowerShell project with this game as well as making some custom changes to this game and others in the project.

Quick Fire Character Counting Game In PowerShell

Another pointless activity in PowerShell. The premis of this game is that you have a short amount of time to look at a word, you then have to guess how many letters are in the word.

If you get the number correct, the game continues and your score increases. If you input and incorrect number, then the game ends, shows your score and exits.

As always I have added this to my “Games In PowerShell” project. Below is the code for the game:

function quickfirecounting{
 $getverbs = Get-Verb | select verb
 [int]$verbamount = $getverbs.count
 $scoreforquickfire = 0
 do{
  $random = Get-Random -Minimum 0 -Maximum $verbamount
  $selectedword = $getverbs[$random] -replace "@{verb=","" -replace "}",""
  $selectedwordlength = $selectedword.Length

  $scoreforquickfire = $scoreforquickfire + 1
  Clear-Host
  Write-Host "$selectedword"
  Start-Sleep -Milliseconds 550
  Clear-Host
  $guess = Read-Host "how many letters are in the word?"
 }until ($guess -ne $selectedwordlength)
 Clear-Host
 Write-Host "Incorrect! There were $selectedwordlength letters"
 Start-Sleep -Seconds 1
 Clear-Host
 Write-Host "Your score was $scoreforquickfire"
 Start-Sleep -Seconds 1
 Clear-Host

 do {$playagainselection = Read-Host "do you want to play again? (Y or N)"} while (("y","n") -notcontains $playagainselection)
 if ($playagainselection -eq "y"){
  ####GO TO GAME FUNCTION TO PLAY AGAIN####
 }elseif ($playagainselection -eq "n"){
  ####GO TO THE MAIN MENU####
 }
}

As you can probably see, I am using the verb section of PowerShell to get the random words. This is better and easier than inputting my own words individually.

Enjoy!

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!

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!