Blackjack Step By Step

Blackjack Step By Step 3,6/5 8290 votes

Step 6: End Hand and Payouts. We now need a method to handle the end of a hand and payouts or collections. During this time, the GameState is Payout. At the end of a hand of Blackjack, the following things can happen: If the player has Blackjack, they get paid 1.5 times their original bet. Must be 21 or older to purchase or redeem tickets. Overall odds vary by game. All sales are final. In accordance with ADA, these materials may be made available in an alternative format. Gambling problem? Call 1.800.NEXT STEP (1-800-639-8783) or VISIT SITE. Please Play Responsibly™. Interested in programming? Like to read about programming without seeing a constant flow of technology and political news into your proggit? That's what /r/coding is for.

In the previous posts in this series, we have built our Blackjack in Blazor demo app up to a point where we have many of the pieces we need to make a functioning implementation. In this post, we're going to finish it off.

Lucky for us, we did the markup and method definitions for the last piece of this implementation, the main Blazor component, in the previous part. Now all we need to do is write the C# code that will make it work.

Catching Up

You'll probably want to read the first three parts of this series before reading this one.

As always, there's a sample repository over on GitHub that has all of the code used in this series. Check it out!

A Quick Recap

Here's the markup for the main Blackjack Blazor component that we coded up in the previous post.

Also in the previous post, we defined a set of methods that needed to be implemented:

  • InitializeHand()
  • NewHand()
  • Stand()
  • Hit()
  • Insurance()
  • DoubleDown()
  • Bet(int amount)

Let's get started implementing those methods!

Useful Custom Method: Delay()

In order to force the application to take 'pauses' at certain times, we're going to make use of a special method called Delay(), which looks like this:

How to play blackjack step by step

The method StateHasChanged() in Blazor is a special method that tells the renderer to re-render the display. Therefore every time we delay, the display will be re-rendered.

From here on out, the methods we need to implement follow a rough order.

Step 1: Start a New Hand

The method InitializeHand() is invoked whenever a new hand is started.

In the real world, if the dealer is only playing with one deck of cards, they would need to check if the deck needs to be shuffled, and do so. So this method will also need to do that check, and set the game state to Shuffling while that is happening. If the deck does not need to be shuffled, then the player will be able to make a bet for the new hand, so the GameState will need to be set to Betting.

Here's the code for our method:

Step 2: Placing a Bet

Setting the GameState to Betting means the buttons to place a bet are available.

The player cannot bet more than they have funds for. After the bet is placed, the dealer can make the initial deal. Our Bet() method will take these into account, and be written like this:

Step 3: The Initial Deal

After the bet is placed, the Dealer makes the initial deal by dealing a card to the player first, then a face-down card to him/herself, then another card to the Player, then a face-up card to him/herself. After each card is dealt, we will call StateHasChanged() so the display will render the dealt card.

Once all the cards are dealt, the GameState will be set to InProgress.

Step 3.1: Player Has Blackjack

At this point, there's only one condition in which the player is not offered the choice to stand or hit: if they have a blackjack.

Let's modify the Deal() method to take this situation into account:

Step 4: Dealer's Turn

The Dealer does not take their turn until the Player completes theirs. Before we can build out the methods for the Player's actions, we need to have a method for the dealer to take their turn.

In Blackjack, the dealer will always hit if their total score is 16 or less, and will always stand on 17 or more. We can use a recursive method to make the Dealer take a turn until their score is 17 or greater:

Step 5: Player Actions

When the GameState is set to InProgress, the player is now able to choose which action to take. During each hand, they can either hit or stand; in some circumstances, they can use the double down or insurance special plays.

The implementation of the player action methods assumes the existence of a method EndHand(), which we will implement later.

Let's deal with each of the player actions one at a time.

Step 5.1: Hit

If the player chooses to hit, the Dealer will give them a new card. At that point, if the player is busted, the hand is over. Otherwise, the GameState does not change; the player may still choose to hit or stand.

Step 5.2: Stand

If the player chooses to stand, the Dealer reveals their hidden card and takes their turn. Afterward, the hand is over.

Step 5.3: Double Down

If the player has a 9, 10, or 11 score and exactly two cards, the Double Down special play is available. On this play, the following things happen:

How to play blackjack step by stepStep
  • The player's bet is doubled
  • The player gets one additional card, and is forced to stand after that.

This results in a DoubleDown() method that looks like this:

Step 5.4: Insurance

If the dealer's visible card is an Ace, the player make make a special Insurance bet. During this play, the GameState is set to Insurance.

If the player chooses to use this play, the insurance bet will be half of the original bet. The dealer will then look at their hidden card. If the hidden card is a ten-card, the player loses their original bet but wins the insurance bet at 2-1; in effect, the player loses no money.

However, if the dealer's hidden card is NOT a ten-card, the player immediately loses their insurance bet, and play continues normally.

Step 6: End Hand and Payouts

We now need a method to handle the end of a hand and payouts or collections. During this time, the GameState is Payout.

At the end of a hand of Blackjack, the following things can happen:

Blackjack Step By Step
  • If the player has Blackjack, they get paid 1.5 times their original bet.
  • If the player has a higher score and neither is bust, OR the dealer has gone bust, the player wins the bet.
  • If the player is bust, OR the dealer has a higher score and neither is bust, the player loses their bet.
  • If there's a push, no money changes hands.

In order to determine how to write up this method, let's consider what the 'normal' outcome is for the player. To put it simply, the player's normal outcome is a loss. After all, casinos don't really want you to beat them.

So, let's create conditions for a player win or a push, and leave the loss condition as the default.

Note that all of these conditions are modifying the Change property of the Player object; the player does not actually collect their winnings or payout their losses here.

Because the GameState is set to Payout, the button for 'Keep Going' will be visible to the player, and will invoke our last method: NewHand().

Step 7: New Hand

This last method needs to do the following things:

  • Payout (or collect) the player's bets.
  • Reset the player's bets and change values.
  • Reset the GameState
  • Call InitializeHand() to start a new hand.

Here's the code:

Try It Out!

You can play this implementation of Blackjack for yourself right now, over at my sister project site BlazorGames.net.

Or, you can just watch this GIF of me playing Blackjack in Blazor:

Summary

In this final part of our Blackjack in Blazor series, we put all of our C# and Blazor pieces together to make the final, working game.

Got ideas on how to improve this implementation? Can you figure out how to model a split play? I want to hear your suggestions! Leave them in the comments below.

Thanks for playing, and Happy Coding!

BLACKJACK 3D
Amazing 3d interface, action packed battles and tons of free chips Test your nerve as you take on the dealer - with the Ultimate Blackjack Experience!

Why Blackjack 3D?

  • SINGLE USER ACCOUNT
    Your account is linked to Pokerist Texas Poker and Roulettist, allowing you to use contacts, achievements and chips in all three applications.
  • FREE CHIPS
  • LEARN TO PLAY
    Are you a new player and have you always wanted to test your mettle in Blackjack? We can help you take the first step.
  • FAIR HAND DEALING GUARANTEED
    We have a fully compliant algorithm to ensure cards are dealt fairly at all times!

Poker Step By Step How To

  • TALK TO OTHER PLAYERS
    Use our convenient chat and messaging system to discuss the games you've won and share your experiences with friends and rivals.
  • TOURNAMENTS
    Participate in fun, engaging tournaments with special rules.
  • NO REGISTRATION
  • CONVENIENT INTERFACE
    Dive right into the action with the game’s fun and intuitive design!
Tweets from KamaCasino
You must agree to accept cookies to view the Twitter feed

Craps Step By Step

  • Play now
    TEXAS HOLD'EM POKER
  • Play now
    THREE KINDS OF GAME
  • Play now
    PUNTO BANCO