-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.cs
113 lines (99 loc) · 2.96 KB
/
Deck.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlackJack
{
/// <summary>
/// A deck of cards
/// </summary>
public class Deck
{
#region Fields
List<Card> cards = new List<Card>();
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public Deck()
{
// fill the deck with cards
foreach (Suit suit in Enum.GetValues(typeof(Suit)))
{
foreach (Rank rank in Enum.GetValues(typeof(Rank)))
{
cards.Add(new Card(rank.ToString(), suit.ToString()));
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets whether the deck is empty
/// </summary>
public bool Empty
{
get { return cards.Count == 0; }
}
#endregion
#region Public methods
/// <summary>
/// Cuts the deck of cards at the given location
/// </summary>
/// <param name="location">the location at which to cut the deck</param>
public void Cut(int location)
{
int cutIndex = cards.Count - location;
Card[] newCards = new Card[cards.Count];
cards.CopyTo(cutIndex, newCards, 0, location);
cards.CopyTo(0, newCards, location, cutIndex);
cards.Clear();
cards.InsertRange(0, newCards);
}
/// <summary>
/// Shuffles the deck
///
/// Reference: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#shuffle%28java.util.List%29
/// </summary>
public void Shuffle()
{
Random rand = new Random();
for (int i = cards.Count - 1; i > 0; i--)
{
int randomIndex = rand.Next(i + 1);
Card tempCard = cards[i];
cards[i] = cards[randomIndex];
cards[randomIndex] = tempCard;
}
}
/// <summary>
/// Takes the top card from the deck. If the deck is empty, returns null
/// </summary>
/// <returns>the top card</returns>
public Card TakeTopCard()
{
if (!Empty)
{
Card topCard = cards[cards.Count - 1];
cards.RemoveAt(cards.Count - 1);
return topCard;
}
else
{
return null;
}
}
/// <summary>
/// Prints the contents of the deck
/// </summary>
public void Print()
{
foreach (Card card in cards)
{
Console.WriteLine(card.Rank + " of " + card.Suit);
}
}
#endregion
}
}