-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.cs
More file actions
80 lines (68 loc) · 1.69 KB
/
Deck.cs
File metadata and controls
80 lines (68 loc) · 1.69 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
using System;
using System.Linq;
using System.Collections.Generic;
namespace GoFish {
public class Deck {
/// <summary>
/// The list of cards currently in the deck
/// </summary>
List<Card> cards;
/// <summary>
/// A private RNG for shuffling the deck
/// </summary>
Random rand;
/// <summary>
/// The count of cards remaining in the deck
/// </summary>
public int Count {
get {
return this.cards.Count;
}
}
public Deck(Random rand) {
this.rand = rand;
this.cards = new List<Card>();
for (int i = 0; i < Card.SuitCount; i++) {
for (int j = 1; j <= Card.CardNames.Length; j++) {
cards.Add(new Card(j));
}
}
}
/// <summary>
/// Swap two cards in the deck.
/// </summary>
/// <param name="index1">The first position to swap</param>
/// <param name="index2">The second position to swap</param>
private void SwapCards(int index1, int index2) {
Card tmp = this.cards[index1];
this.cards[index1] = this.cards[index2];
this.cards[index2] = tmp;
}
/// <summary>
/// Shuffles the deck using the Fisher-Yates shuffle algorithm.
/// </summary>
public void Shuffle() {
for (int i = 0; i < this.Count - 2; i++) {
int swapIndex = rand.Next(i, this.Count);
SwapCards(i, swapIndex);
}
}
/// <summary>
/// Gets a card from the top of the deck.
/// </summary>
/// <returns>The card on top of the deck, or null if none left.</returns>
public Card GetCard() {
if (this.Count < 1) return null;
Card ret = cards[0];
cards.RemoveAt(0);
return ret;
}
public override string ToString() {
string str = "";
this.cards.ForEach((card) => {
str += card.ToString() + " ";
});
return str;
}
}
}