Blackjack Game in PowerShell

Another game post today, this time something more complex than number guessing games. I will of course be adding this to my PowerShell games file and all that jazz. You can see this addition in my Games in PowerShell project, coincidentally, linked here

Lets dive right in. I cheated a little with the dealers card, or lack there of, and put a get-random argument instead. This is because, as you can see, keeping track of just the users cards is a ball ache enough. For this simple game, this method will suffice.

I also added “error identifiers” in each hands if statements because I was getting weird errors were it would just exit without telling the user who won or not. This should sort that or at least let me know where it is failing. You can remove this if you want 🙂

First of all, I started by creating all my variables, as you can see below:


[string][int]$script:dealer = Get-Random -Minimum 15 -Maximum 24

[string][int]$script:startingcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:secondcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:thirdcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:fourthcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:fifthcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:finalcard = Get-Random -Minimum 1 -Maximum 11

I know some people, including me, play the game a little differently, for example a Jack is 11, Queen is 12 and King is 13 but I did a bit of reading and I just decided to play it safe (and easier) to have it this way.

I then started the actual code. I started by created the first hand, funnily enough in the firsthand function. This can be see below:


function firsthand{
Clear-Host
Write-Host "You have a $startingcard and a $secondcard"
do {$decision1 = Read-Host "Do you want to pick up another card?"} while (("y","n") -notcontains $decision1)
if ($decision1 -eq "y"){
Secondhand
}elseif ($decision1 -eq "n"){
#if two starting cards are greater than dealer's and less than or equal to 21
if (([int]$startingcard + [int]$secondcard -gt [int]$dealer) -and ([int]$startingcard + [int]$secondcard -le 21)){
Write-Host "You won, the dealer had $dealer"
#if dealer's is greater than two starting cards and less than or equal to 21
}elseif (([int]$dealer -gt [int]$startingcard + [int]$secondcard) -and ([int]$dealer -le 21)){
write-host "the dealer won with $dealer"
#if dealer is same as starting cards
}elseif ([int]$dealer -eq [int]$startingcard + [int]$secondcard){
write-host "You have drawn with the dealer, you both has $dealer"
}else {
Write-Host "the error is in hand one"
}
}
}

From the code above, you can see what each function, or hand, will end up doing. Starting my asking if the users wants another card, checking the total of all the cards, if yes then it moves to the next hand function and if no then it calculates who has won. The dealer or the player (you, or person with a lot more time on their hands who is playing this game). I have even used comments to make it easier to understand.

Notice that this stage is different from the others because the player cannot go bust since they can only have two cards with a max value of 20.

Now on to the second hand, this is pretty much just like all the other hands left to go apart from the final hand. Which is the end. Obviously. And special.

The second hand is once again appropriately named. And also has a bit more meat to it than the first hand because the player can now go bust. You can see my code for the second hand below;


function secondhand{
Clear-Host
Write-Host "You now have $startingcard, $secondcard and $thirdcard"
#adds up all three cards
[int]$secondhandtotal = [int]$startingcard + [int]$secondcard + [int]$thirdcard
#checks if three cards are over 21
if ([int]$secondhandtotal -gt 21){
write-host "You have bust with $secondhandtotal"
pause;
exit
}else{}
#Asks if you want to pick another card or not
do {$decision2 = Read-Host "Do you want to pick another card?"} while (("y","n") -notcontains $decision2)
if ($decision2 -eq "y"){
thirdhand
}elseif ($decision2 -eq "n"){
#if three cards are greater than dealers and less than or equal to 21
if (([int]$secondhandtotal -gt [int]$dealer) -and ([int]$secondhandtotal -le 21)){
Write-Host "You won, the dealer had $dealer"
#if dealer cards are greater than 3 cards but less than or equal to 21
}elseif (([int]$dealer -gt [int]$secondhandtotal) -and ([int]$dealer -le 21)){
Write-Host "The dealer won with $dealer"
#if dealer and third hand cards are the same
}elseif ([int]$dealer -eq [int]$secondhandtotal){
Write-Host "You have drawn with the dealer, you both had $dealer"
}else{
write-host "The error is in hand two"
}
}
}

The next two hand functions are pretty much the same, you can see the differences in the full code exert at the bottom.

Now onto the final hand. This hand is quite simple because the way I play blackjack is that, if you have 5 cards and are still not bust, then you win. Regardless of what the dealer has. That’s why in this function, I also check if the player has gone over 21 or not since I know that they must have 5 cards at this point. You can see my code below:


function finalhand{
Clear-Host
#adds up all the cards
[int]$finaltotal = [int]$startingcard + [int]$secondcard + [int]$thirdcard + [int]$fourthcard + [int]$fifthcard
if ([int]$finaltotal -gt 21){
Write-Host "You have bust, the dealer won with $dealer"
pause
exit
}elseif ([int]$finaltotal -le 21){
Write-Host "With more than 5 cards under a total of 21, you win by default. The dealer had $dealer"
}
}

There you have it. My code for blackjack in PowerShell. You may have noticed that it has a few issues. For example, there is the OBSCENELY SMALL possibility that all 5 cards could come out at the same amount. Which in real blackjake, unless you were using two decks, would be impossible.

Here is the full code:


[string][int]$script:dealer = Get-Random -Minimum 15 -Maximum 24

[string][int]$script:startingcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:secondcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:thirdcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:fourthcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:fifthcard = Get-Random -Minimum 1 -Maximum 11
[string][int]$script:finalcard = Get-Random -Minimum 1 -Maximum 11

function firsthand{
Clear-Host
Write-Host "You have a $startingcard and a $secondcard"
do {$decision1 = Read-Host "Do you want to pick up another card?"} while (("y","n") -notcontains $decision1)
if ($decision1 -eq "y"){
Secondhand
}elseif ($decision1 -eq "n"){
#if two starting cards are greater than dealer's and less than or equal to 21
if (([int]$startingcard + [int]$secondcard -gt [int]$dealer) -and ([int]$startingcard + [int]$secondcard -le 21)){
Write-Host "You won, the dealer had $dealer"
#if dealer's is greater than two starting cards and less than or equal to 21
}elseif (([int]$dealer -gt [int]$startingcard + [int]$secondcard) -and ([int]$dealer -le 21)){
write-host "the dealer won with $dealer"
#if dealer is same as starting cards
}elseif ([int]$dealer -eq [int]$startingcard + [int]$secondcard){
write-host "You have drawn with the dealer, you both has $dealer"
}else {
Write-Host "the error is in hand one"
}
}
}

function secondhand{
Clear-Host
Write-Host "You now have $startingcard, $secondcard and $thirdcard"
#adds up all three cards
[int]$secondhandtotal = [int]$startingcard + [int]$secondcard + [int]$thirdcard
#checks if three cards are over 21
if ([int]$secondhandtotal -gt 21){
write-host "You have bust with $secondhandtotal"
pause;
exit
}else{}
#Asks if you want to pick another card or not
do {$decision2 = Read-Host "Do you want to pick another card?"} while (("y","n") -notcontains $decision2)
if ($decision2 -eq "y"){
thirdhand
}elseif ($decision2 -eq "n"){
#if three cards are greater than dealers and less than or equal to 21
if (([int]$secondhandtotal -gt [int]$dealer) -and ([int]$secondhandtotal -le 21)){
Write-Host "You won, the dealer had $dealer"
#if dealer cards are greater than 3 cards but less than or equal to 21
}elseif (([int]$dealer -gt [int]$secondhandtotal) -and ([int]$dealer -le 21)){
Write-Host "The dealer won with $dealer"
#if dealer and third hand cards are the same
}elseif ([int]$dealer -eq [int]$secondhandtotal){
Write-Host "You have drawn with the dealer, you both had $dealer"
}else{
write-host "The error is in hand two"
}
}
}

function thirdhand{
Clear-Host
Write-Host "You now have $startingcard, $secondcard, $thirdcard and $fourthcard"
#adds up all four cards
[int]$thirdhandtotal = [int]$startingcard + [int]$secondcard + [int]$thirdcard + [int]$fourthcard
#checks if four cards are over 21
if ([int]$thirdhandtotal -gt 21){
write-host "You have bust with $thirdhandtotal"
pause;
exit
}else{}
#asks if you want to pick another card or not
do {$decision3 = Read-Host "Do you want to pick another card?"} while (("y","n") -notcontains $decision3)
if ($decision3 -eq "y"){
fourthhand
}elseif ($decision3 -eq "n"){
#if four cards are greater than dealers and less than or equal to 21
if (([int]$thirdhandtotal -gt [int]$dealer) -and ([int]$thirdhandtotal -le 21)){
Write-Host "You won, the dealer had $dealer"
#if dealer cards are greater than four cards but less than or equal to 21
}elseif (([int]$dealer -gt [int]$thirdhandtotal) -and ([int]$dealer -le 21)){
Write-Host "The dealer won with $dealer"
#if dealer and third hand cards are the same
}elseif ([int]$dealer -eq [int]$thirdhandtotal){
Write-Host "You have drawn with the dealer, you both had $dealer"
}else {
Write-Host "the error is in hand three"
}
}

}

function fourthhand{
Clear-Host
Write-Host "You now have 5 cards, a $startingcard, $secondcard, $thirdcard, $fourthcard and $fifthcard"
#adds up all five cards
[int]$fourthhandtotal = [int]$startingcard + [int]$secondcard + [int]$thirdcard + [int]$fourthcard + [int]$fifthcard
#checks if five cards are over 21
if ([int]$fourthhandtotal -gt 21){
Write-Host "You have bust with $fourthhandtotal"
pause
exit
}else{}
#asks if you want to pick another card or not
do {$decision4 = Read-Host "do you want to pick another card?"} while (("y","n") -notcontains $decision4)
if ($decision4 -eq "y"){
finalhand
}elseif ($decision4 -eq "n"){
#if five cards are greater than dealers and less than or equal to 21
if (([int]$fourthhandtotal -gt [int]$dealer) -and ([int]$fourthhandtotal -le 21)){
write-host "You have won, the dealer had $dealer"
#if dealer cards are greater than 5 cards but less than or equal to 21
}elseif (([int]$dealer -gt [int]$fourthhandtotal) -and ([int]$dealer -le 21)){
Write-Host "The dealer won with $dealer"
#if dealer and fourth hand cards are the same
}elseif ([int]$dealer -eq [int]$fourthhandtotal){
Write-Host "You have drawn with the dealer, you both have $dealer"
}else {write-host "the error is in hand four"
}
}
}

function finalhand{
Clear-Host
#adds up all the cards
[int]$finaltotal = [int]$startingcard + [int]$secondcard + [int]$thirdcard + [int]$fourthcard + [int]$fifthcard
if ([int]$finaltotal -gt 21){
Write-Host "You have bust, the dealer won with $dealer"
pause
exit
}elseif ([int]$finaltotal -le 21){
Write-Host "With more than 5 cards under a total of 21, you win by default. The dealer had $dealer"
}
}

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

firsthand

Enjoy!

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