Blackjack C Sharp
We're going to build a Blackjack app using the Console the quickest way that we can and then after we will talk a little bit about some better ways to build this app. Designing a Blackjack App will be a good way for us to talk a little bit about some of the different variable types, do some basic math functions, create and use methods, and even.
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); |
} |
} |
} |
- In future, I will try to make some step-by-step video and techniques needed to develop this BlackJack game from ground. Give a comment below if this solution works for you or if you have a better.
- Blackjack skill and card counting training A simple Casino BlackJack card game written in C # as part of my learning assignment a few years ago and it is not intent to be a full feature game. This project is to demostrate the way to build a simple card game and only cover the very basic of blackjack rule with standard card counting method.
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[]. MemberfunctionsOneDice() returns a value between 1 and 6 - rnd.Next(n) returns values in the range 0..n-1, while ThreeDice() calls OneDice() three times. The constructor for the RollDice() clears the Rolls array then calls ThreeDice() however many times (10 million in this case) and increments the appropriate Rolls[] element.
The last part is to print out the generated totals to see that it generates throws in accordance with the probabilities. A 6 sided dice has an average score of 3.5, so three dice should average about 10.5. The totals for 10 and 11 are roughly the same and occur about 12.5% of the time.
Here is the output of a typical run. It takes no more than a second. Because it's a console application, I included a
To wait until you hit a key before closing.