• Home

Blackjack C Sharp

 
Sharp

Mar 28, 2009 Let's hope your teacher doesn't know how to play blackjack! You're initially dealt 2 cards, before you hit or stay, not one. If you go over 21, and the dealer goes over 21, you do not win. This application uses one instance of the Random class in the object rnd.It also allocates enough space to hold the totals for scores 3.18 in the array Rolls.Member functions OneDice returns a value between 1 and 6 - rnd.Next(n) returns values in the range 0.n-1, while ThreeDice calls OneDice three times.

Simple project for practicing C# by making BlackJack! Card with value; Deck gives cards; Hand gets cards; Hand has a total; Player has a hand; Player can get new cards; Player can hit or stay; Player can bust; Implement simple player run dealer; Override dealer's Deal functionality to automate; Dealer must draw on all 16s. Let's hope your teacher doesn't know how to play blackjack! You're initially dealt 2 cards, before you hit or stay, not one. If you go over 21, and the dealer goes over 21, you do not win. C# (CSharp) Blackjack Card - 30 examples found. These are the top rated real world C# (CSharp) examples of Blackjack.Card extracted from open source projects. You can rate examples to help us improve the quality of examples.

Blackjack C Sharp Scale

blackjack.cs
usingSystem.IO;
usingSystem;
usingSystem.Linq;
usingSystem.Collections.Generic;
publicclassCard
{
publicstringsuit { get; privateset; }
publicintrank { get; privateset; }
publicCard(stringsuit, intrank)
{
this.suit=suit;
this.rank=rank;
}
}
publicclassHand
{
publicCard [] hand=newCard [2];
publicHand(Cardcard1, Cardcard2)
{
this.hand[0] =card1;
this.hand[1] =card2;
}
}
publicclassDeck
{
publicIList<Card> deck=newList<Card>();
publicIEnumerable<int> ranks=Enumerable.Range(1, 10);
publicstring[] suits=newstring[]
{'H','S','C','D'};
publicDeck()
{
foreach (stringsuitinsuits)
{
foreach (intrankinranks) {
deck.Add(newCard(suit, rank));
}
}
}
}
publicclassBlackjack
{
staticpublicvoidMain ()
{
Deckdeck1=newDeck();
foreach (Cardcardindeck1.deck)
{
Console.WriteLine(card.suit+card.rank);
}
}
}
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

The following tutorial is only some kind of “thought provoking” one: shows you some logic and class structure using enums and dynamically created images with Winform application.

Maybe I skipped some of the official rules. If so, I’m sorry I’m not a big gambler. This tutorial is not for teaching you Blackjack, but showing you some C# practices.

Here are some of the rules I have implemented:

Blackjack
  • 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:

I have 3 main classes: Card, Deck and Hand. Of course the form has its own Form1 class.

I also have 2 enums: CardValue and CardSuit. 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:

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:

With the constructor let’s create a dummy card with no color and value:

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).

Blackjack C SharpBlackjack

Blackjack C Sharp Vs

Cash

And the overridden ToString method, just for fun (not used later in the code):

C Sharp Programming

In the next chapter I’ll show you how to create the class for the Hand and the Deck.