Blackjack in C#
Some of you from Quora know that I am very pro-C#. But what has C# got that Java, C and C++ haven’t?
I guess the simplest possible answer is that C# is programmer-friendlier than the above three combined, carries most of their advantages and none of their disadvantages.
More importantly, C#, now in version 12 (as of November 2023) has many new goodies that will make development in C# pure joy.
If you know some Python, I invite you to take a look at this simple game of Blackjack I wrote in C#:
(If you want to download and try the game on your computer, head on to my github.)
1using System;
2using System.Collections.Generic;
3
4public class Program
5{
6 static void Main()
7 {
8 var deck = new Deck();
9 var playerHand = new Hand();
10 var dealerHand = new Hand();
11
12 playerHand.AddCard(deck.DrawCard());
13 playerHand.AddCard(deck.DrawCard());
14
15 dealerHand.AddCard(deck.DrawCard());
16
17 while (true)
18 {
19 Console.WriteLine("Your hand: " + playerHand);
20 Console.WriteLine("Dealer's hand: " + dealerHand);
21
22 if (playerHand.Value > 21)
23 {
24 Console.WriteLine("You busted! Dealer wins.");
25 break;
26 }
27
28 Console.Write("Do you want to hit or stand? ");
29 var input = Console.ReadLine().ToLower();
30
31 if (input == "hit")
32 {
33 playerHand.AddCard(deck.DrawCard());
34 }
35 else if (input == "stand")
36 {
37 while (dealerHand.Value < 17)
38 {
39 dealerHand.AddCard(deck.DrawCard());
40 }
41
42 Console.WriteLine("Dealer's final hand: " + dealerHand);
43
44 if (dealerHand.Value > 21)
45 {
46 Console.WriteLine("Dealer busted! You win!");
47 }
48 else if (dealerHand.Value > playerHand.Value)
49 {
50 Console.WriteLine("Dealer wins!");
51 }
52 else if (dealerHand.Value < playerHand.Value)
53 {
54 Console.WriteLine("You win!");
55 }
56 else
57 {
58 Console.WriteLine("It's a tie!");
59 }
60
61 break;
62 }
63 }
64 }
65}
66
67public class Card
68{
69 public int Value { get; set; }
70 public string Name { get; set; }
71
72 public override string ToString()
73 {
74 return Name;
75 }
76}
77
78public class Deck
79{
80 private List<Card> cards;
81
82 public Deck()
83 {
84 cards = new List<Card>();
85
86 for (int i = 2; i <= 11; i++)
87 {
88 for (int j = 0; j < 4; j++)
89 {
90 var card = new Card { Value = i };
91
92 if (i == 11)
93 {
94 card.Name = "Ace";
95 card.Value = 11;
96 }
97 else if (i == 10)
98 {
99 card.Name = j == 0 ? "10" : j == 1 ? "Jack" : j == 2 ? "Queen" : "King";
100 }
101 else
102 {
103 card.Name = i.ToString();
104 }
105
106 cards.Add(card);
107 }
108 }
109
110 var random = new Random();
111 for (int i = 0; i < cards.Count; i++)
112 {
113 var temp = cards[i];
114 var index = random.Next(cards.Count);
115 cards[i] = cards[index];
116 cards[index] = temp;
117 }
118 }
119
120 public Card DrawCard()
121 {
122 var card = cards[0];
123 cards.RemoveAt(0);
124 return card;
125 }
126}
127
128public class Hand
129{
130 private List<Card> cards = new List<Card>();
131
132 public int Value
133 {
134 get
135 {
136 var value = 0;
137 var aces = 0;
138
139 foreach (var card in cards)
140 {
141 value += card.Value;
142 if (card.Name == "Ace")
143 {
144 aces++;
145 }
146 }
147
148 while (value > 21 && aces > 0)
149 {
150 value -= 10;
151 aces--;
152 }
153
154 return value;
155 }
156 }
157
158 public void AddCard(Card card)
159 {
160 cards.Add(card);
161 }
162
163 public override string ToString()
164 {
165 return string.Join(", ", cards) + " (" + Value + ")";
166 }
167}