BlackJack Game in C#

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn how to develop the Black Jack card Game in C#

Requirements
Visual Studio 2010 and above
Cards image
Difficulty

  • Intermediate

Black Jack Game
This tutorial is a somehow “thought provocative” and it uses enums to show logic and class structure and dynamically created images with Winform application.
Rules Implemented for the Game
• we have a shuffled deck of 52 cards
• the player starting hand has 2 cards
• the computer hand has also 2 cards: one of them is visible the other one is not
• the Jack, Queen and King has the value of 10
• the Ace has the value of 11 or 1 if 11 would be too much in hand
• the player starts to draw (official phrase is HIT)
• the main goal is to stop at 21 (or close to 21)
• the player can skip the drawing phase (STAY) if the total score is 16 at least
• if player stays, the computer turn comes
• first we can see the computer’s second (hidden) card
• the computer starts to draw using the same rules as ours mentioned before
To show you how to use System.Drawing.Graphics class, I create the images of the cards by cutting 1 big image into pieces. Click on the following image to download it:
cards.png

There are main classes: Card, Deck and Hand, in addition with the Form1 class.

Two enums: CardValue and CardSuit were also created. Both enum indexing starts from 1. The CardValue contains the type of the cards (Ace, King, 9, 7, 2, etc.). The CardSuit contains the colors (hearts, spades, etc.).

Create a new class: Card.cs

Under the class place the 2 enums:
Screenshot (4).png

The Card class will have 3 properties, 1 constructor, 1 new method and 1 overridden method. What properties does a card have? Of course its value, its color and its picture. So create the private properties with their public encapsulated fields. The Image property won’t have setter, the value and color setter will have the image loader method. And let’s create a dummy card with no color and value with the constructor

Screenshot (17).png!
Screenshot (19).png

The attached image has the following properties: it’s 392 pixel high and 950 wide. So 1 card is about 97 high and 73 wide. The method has to slice the image depending on the following color from the deck (one color in each row on the image) and depending on the value (the image has the values in ascending order).
Screenshot (9).png

we create the class for the deck. The deck contains the cards, 1 from each. Of course we have to shuffle the deck before every new game and we have to draw a card from the shuffled deck. Now we now what properties and methods should have this class:

card list
shuffle method
draw method
Screenshot (10).png

The Shuffle method walks through on every card, add them to a list (deck). The OrderBy Linq will randomize (shuffle) the deck.
we have to be able to draw a card, so we create a DrawCard method
Screenshot (11).png
The hand where the drawn card goes to public Card DrawCard(Hand hand) { Card drawn = cards[cards.Count – 1]; cards.Remove(drawn); hand.Cards.Add(drawn); return drawn; }

Every time you draw a card, you draw it from the top of your deck (from the end of your list). It’s simple.
I have also created an ’empty’ exception class for the deck, called DeckException. You can also write this either in this Deck.cs file or in a new file:

Screenshot (12).png

This custom exception only shows you how to create the basics of your exception and nothing more. From now you can throw (then catch) your own exception.

Now we need a ‘hand’ that holds the drawn cards, so let’s create its class. It will have 1 property (the list of the cards), 1 constructor with our new custom exception and 1 method that counts our and computer’s total points.

Screenshot (13).pngScreenshot (14).png
As you can see, the constructor waits for a startinghand number that is the starting number of the cards in hand (in this case it is 2) and a Deck type parameter that is the deck that we are going to draw from.
The AddValue method has no return value (void) that’s why we use the currentSum input parameter as ref. We pass this parameter here as a reference, so any changes on this parameter here will be visible from outside of this class (for example on our main form). You’ll see later. The Ace could have 2 possible values: 1 or 11. If our score is not more than 10 it is 11. If our score is at least 11 the Ace is going to be only 1. All the figure cards have the value of 10.
In the final part we create the form and use our classes to be able to play.
Now in this final part let’s create our form using all the classes we’ve done before.

Create a new form with the size of 1000*400. Its FormBorderStyle should be FixedSingle. Place a MenuStrip on it (only for further developments, for example: new game, score table, etc.) and a SplitContainer with 2 horizontal panels on it. You also need 2 buttons (hit and stay) and 3 labels to show scores and computer action if it STAYs.

The form should look like something like this:
blackjack01.png
Let’s create some class-level variables (so you can reach them from every method):

    private readonly int STARTING_HAND = 2;
    private readonly int MAX_CARDS_ON_TABLE = 11;//worst case: you could have maximum 11 cards in hand (four 2s + four 3s + four 1s = 21)
    PictureBox p;
    PictureBox q;
    Deck deck;
    Hand player;
    Hand computer;
    int computerSum;
    int playerSum;

The first 2 variables are readonly one. It could be const, if you prefer that one, doesn’t matter. PictureBox p and q are for the 11-11 pictureboxes that will be placed onto the table for each gamblers: p for computer, q for player. We also have a deck and 2 hands and of course the 2 total points in computerSum and playerSum.
We will have only 6 methods on our form:

NewGame – resets the form, clears the table, initializes the card places, resets the scores
ClearTable
InitializeCardPlacesOnTable
btnHit_Click
btnStay_Click
computerHit



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.

  • No not just use the screenshot to hide your plagiarising face .This is your first time plagiarising .If you want to contribute more in Utopian , pls spend your time doing your own technical work.Next time pls put the code in the post instead of the screenshot
  • The same link https://www.csharp-tutorial.hu/csharp/blackjack-game-part-1/

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.26
TRX 0.20
JST 0.038
BTC 95832.71
ETH 3561.59
USDT 1.00
SBD 3.85