Blackjack in PowerShell

This is a little function that mimics a simplified version of blackjack. I have wrapped it in a function for cleanliness and so that it can be called again.

The rules are below:

  • Get a higher total than the dealer
  • Keep your total under 21 or you will be bust
  • That’s it

Here is the code for you to try this out yourself, hope you enjoy!

function blackjack{

    #CHANGING NAME OF WINDOW
    $pshost = Get-Host
    $pswindow = $pshost.UI.RawUI
    $pswindow.WindowTitle = "Blackjack" 

    #RESETTING GAME OVER VARIABLE
    $blackjack_game_over = $false

    #GENERATING A RANDOM TOTAL FOR THE DEALER
    $blackjack_dealer_total = Get-Random -Minimum 14 -Maximum 22

    #CREATING AN ARRAY FOR THE USERS CARD NUMBERS
    $blackjack_user_card_array = [System.Collections.ArrayList]::new("")

    #GENERATING A RANDOM NUMBER FOR THE USERS FIRST CARD
    $blackjack_user_first_card = Get-Random -Minimum 1 -Maximum 11

    #ADDING USERS FIRST CARD TO ARRAY
    $blackjack_user_card_array.Add($blackjack_user_first_card)

    #CREATING A VARIABLE TO COUNT USERS TOTAL
    $blackjack_user_total = $blackjack_user_first_card

    Clear-Host

    Write-Host "Your first card is $blackjack_user_first_card"

    #DO THIS (PLAY GAME) UNTIL THE GAMEOVER VARIABLE IS TRUE
    do {
        #GET USER INPUT
        do {$blackjack_input = Read-Host "Take another card? (Y or N)"}while (("y","n") -notcontains $blackjack_input)

        #IF USER INPUT IS VALID AND ISN'T BUST AND WANTS ANOTHER CARD
        if ($blackjack_input -eq "y" -and $blackjack_user_total -le 21){

            #GENERATE A NEW CARD FOR THE USER
            $blackjack_user_new_card = Get-Random -Minimum 1 -Maximum 11

            #ADD NEW CARD TO CARD ARRAY
            $blackjack_user_card_array.Add($blackjack_user_new_card)

            #ADD NEW CARD TO CARD TOTAL
            $blackjack_user_total = $blackjack_user_total + $blackjack_user_new_card

            Clear-Host

            Write-Host "You have $blackjack_user_card_array"

            #IF THE USER IS BUST
            if ($blackjack_user_total -gt 21){
                Write-Host "You went bust! The dealer won with " -ForegroundColor Red -NoNewline
                Write-Host $blackjack_dealer_total 
                $blackjack_game_over = $true
            }
            
        #IF THE USER DOESNT WANT ANOTHER CARD
        }else{

            Clear-Host
            
            #OUTPUTTING THE FINAL SCORE
            #Write-Host "You had $blackjack_user_total and the dealer had $blackjack_dealer_total"

            #SWITCH TO SEE WHO WON
            switch ($blackjack_user_total){
                {$_ -gt 21}{Write-Host "You went bust! The dealer won with " -ForegroundColor Red -NoNewline; Write-Host $blackjack_dealer_total; $blackjack_game_over = $true; break}
                {$_ -eq $blackjack_dealer_total}{Write-Host "It's a draw, the dealer also had $blackjack_dealer_total"; $blackjack_game_over = $true; break}
                {$_ -gt $blackjack_dealer_total}{Write-Host "You win! The dealer only had " -ForegroundColor Green -NoNewline; Write-Host $blackjack_dealer_total; $blackjack_game_over = $true; break}
                {$_ -lt $blackjack_dealer_total}{Write-Host "You lose! The dealer won with " -ForegroundColor Red -NoNewline; Write-Host $blackjack_dealer_total; $blackjack_game_over = $true; break}
                default {Write-Host "Something happeneds that wasn't accounted for!" -ForegroundColor Red; break}
            }
        }
    }until ($blackjack_game_over)

    #ASK USER IF THEY WANT TO REPLAY UNTIL INPUT IS A Y OR N
    do {$blackjack_play_again = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $blackjack_play_again)

    #SWITCH TO EITHER PLAY AGAIN OR GO TO MAIN MENU
    switch ($blackjack_play_again){
        "y" {blackjack}
        "n" {exit}
        default {exit}
    }   
}

 

Rock, Paper, Scissors In PowerShell

Another game today, I’ll be adding this to my big Games Project soon. Just need to implement it into the script.

First I started off by making the PowerShell window the correct size. I did this by adding:

$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 500
$newsize.width = 500
$pswindow.buffersize = $newsize
$newsize = $pswindow.windowsize
$newsize.height = 22
$newsize.width = 85
$pswindow.windowsize = $newsize
$pswindow.WindowTitle = "Rock, Paper, Scissors"

to the start of the function.

Next I created the meat of the game, here its just a simple do statement until either the score of the user or the score of the computer has reached or exceeded 3. Best of three sort of thing. Heres that part:

do {
 $options = @("Rock","Paper","Scissors")
 $option = $options | Get-Random -Count 1
 Clear-Host
 Write-Host "Your Score - $score" -ForegroundColor Green
 Write-Host "Computer's Score - $computerscore" -ForegroundColor Red
 do {$selection = Read-Host "Selection"}while (("Rock","Paper","Scissors") -notcontains $selection)
  if ($selection -eq "rock" -and $option -eq "paper"){
   Write-Host "You lose, the computer chose $option";
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "paper" -and $option -eq "scissors"){
   Write-Host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "scissors" -and $option -eq "rock"){
   write-host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif ($selection -eq $option){
   Write-Host "Its a draw"
   Start-Sleep -Seconds 1
  }else{
   Write-Host "You won, the computer chose $option"
   Start-Sleep -Seconds 1
   $score = $score + 1
  }
 }until (($score -eq 3) -or ($computerscore -eq 3))
 if ($computerscore -gt $score){
  Clear-Host
  Write-Host "You lost, the computer scored $computerscore points whilst you only scored $score points" -ForegroundColor Red
 }else{
  Clear-Host
  Write-Host "You won, you got $score points. More than the computer's $computerscore" -ForegroundColor Green
 }
}

Finally, I ended the code of by showing the user who won and giving then the option to exit the game or play again. Here is the code for that:

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

===================================================================================================

Hopefully you can see how this all comes together. Here is the entire script for the game:

function RPS{
 $pshost = get-host
 $pswindow = $pshost.ui.rawui
 $newsize = $pswindow.buffersize
 $newsize.height = 500
 $newsize.width = 500
 $pswindow.buffersize = $newsize
 $newsize = $pswindow.windowsize
 $newsize.height = 22
 $newsize.width = 85
 $pswindow.windowsize = $newsize
 $pswindow.WindowTitle = "Rock, Paper, Scissors"

 [int]$score = 0
 [int]$computerscore = 0

 do {
  $options = @("Rock","Paper","Scissors")
  $option = $options | Get-Random -Count 1
  Clear-Host
  Write-Host "Your Score - $score" -ForegroundColor Green
  Write-Host "Computer's Score - $computerscore" -ForegroundColor Red
  do {$selection = Read-Host "Selection"}while (("Rock","Paper","Scissors") -notcontains $selection)
  if ($selection -eq "rock" -and $option -eq "paper"){
   Write-Host "You lose, the computer chose $option";
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "paper" -and $option -eq "scissors"){
   Write-Host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "scissors" -and $option -eq "rock"){
   write-host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif ($selection -eq $option){
   Write-Host "Its a draw"
   Start-Sleep -Seconds 1
  }else{
   Write-Host "You won, the computer chose $option"
   Start-Sleep -Seconds 1
   $score = $score + 1
  }
 }until (($score -eq 3) -or ($computerscore -eq 3)) 
 if ($computerscore -gt $score){
  Clear-Host
  Write-Host "You lost, the computer scored $computerscore points whilst you only scored $score points" -ForegroundColor Red
 }else{
  Clear-Host
  Write-Host "You won, you got $score points. More than the computer's $computerscore" -ForegroundColor Green
 }

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

I was quite surprised at how short I got the script to be, I think I may try to make the Blackjake game shorter as, lets me honest. Its barbaric in its construction.

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!