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}
}
}