-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.cs
More file actions
143 lines (118 loc) · 3.21 KB
/
Deck.cs
File metadata and controls
143 lines (118 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlackjackSandbox
{
public enum SuitName
{
Heart = 0, Diamond = 1, Spade = 2, Club = 3
}
public enum CardName
{
Ace = 0, Two = 1, Three = 2, Four = 3, Five = 4, Six = 5, Seven = 6, Eight = 7, Nine = 8, Ten = 9, Jack = 10, Queen = 11, King = 12
}
/// <summary>
/// Represents a card
/// </summary>
public class Card
{
public SuitName Suit { get; private set; }
public CardName Value { get; private set; }
public Card(SuitName suit, CardName value)
{
Suit = suit;
Value = value;
}
/// <summary>
/// Gets this cards 2-character string representation
/// </summary>
/// <returns></returns>
public string ToShortString()
{
string name = "";
switch (Value)
{
case CardName.Ace:
name += "A";
break;
case CardName.Jack:
name += "J";
break;
case CardName.Queen:
name += "Q";
break;
case CardName.King:
name += "K";
break;
case CardName.Ten:
name += "T";
break;
default:
name += HandHelper.GetCardValue(Value);
break;
}
switch (Suit)
{
case SuitName.Club:
name += "♣";
break;
case SuitName.Diamond:
name += "♦";
break;
case SuitName.Heart:
name += "♥";
break;
case SuitName.Spade:
name += "♠";
break;
}
return name;
}
}
/// <summary>
/// Represents a standard deck of 52 cards
/// </summary>
public class Deck
{
Card[] m_cards = new Card[52];
public Card[] Cards
{
get { return m_cards; }
}
/// <summary>
/// Creates an unshuffled deck of cards
/// </summary>
public Deck()
{
int cardNumber = 0;
for (int suit = 0; suit < 4; suit++)
{
for (int card = 0; card < 13; card++)
{
m_cards[cardNumber++] = new Card((SuitName)suit, (CardName)card);
}
}
}
/// <summary>
/// Shuffle the deck
/// </summary>
public void Shuffle()
{
//Is this sufficient?
for (int i = 0; i < m_cards.Length; i++)
{
Swap(ref m_cards[i], ref m_cards[Program.RNG.Next(52)]);
}
}
/// <summary>
/// Swaps two cards
/// </summary>
static void Swap(ref Card a, ref Card b)
{
Card intermediate = a;
a = b;
b = intermediate;
}
}
}