-
Notifications
You must be signed in to change notification settings - Fork 1
/
Deck.cs
106 lines (89 loc) · 3.42 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Monopoly.Properties;
using System.IO;
using System.Diagnostics;
namespace Monopoly
{
public class Deck
{
private int mID;
private DeckType mDecktype;
private Card[] mCards;
public Deck(int id, DeckType type)
{
this.ID = id;
this.DeckType = type;
ShuffleDeck();
}//Deck()
//PROPERTIES
public int ID { get { return mID; } private set { mID = value; } }
public DeckType DeckType { get { return mDecktype; } set { mDecktype = value; } }
public Card[] Cards { get { return mCards; } set { mCards = value; } }
private int TopCardIndex; //represents the top card on the deck, ie the first non-discarded card
//METHODS
//called when deck is initialized
public void ShuffleDeck()
{
String allDescriptions = Resources.CardDescriptions;
int cardIDMod = 0; //used to assign correct cardIDs
UI ui = new UI();
if (this.DeckType == DeckType.CommunityChest)
{
Cards = new Card[17]; //wipe deck
allDescriptions = allDescriptions.Substring(16, allDescriptions.IndexOf("Chance")); //+14 for 'communitychest', +2 for cr and lf
ui.UIDebug("Community Chest Deck initializing...");
} else {
Cards = new Card[16];
cardIDMod = 17;
allDescriptions = allDescriptions.Substring(allDescriptions.IndexOf("Chance") + 8); //+6 for 'chance', + 2 for cr and lf
ui.UIDebug("Chance Deck initializing...");
}
String[] cardDescriptions = new String[Cards.Length];
Card[] orderedCards = new Card[Cards.Length];
StringReader reader = new StringReader(allDescriptions);
for(int i = 0; i <= (Cards.Length - 1); i++)
{
String line = reader.ReadLine();
orderedCards[i] = new Card(i + cardIDMod, line);
}//foreach
reader.Close();
ui.UIDebug("Deck is now in proper order. Shuffling...");
//Begin Fisher-Yates shuffling algorithm
Random rand = new Random();
int n = orderedCards.Length;
for(int i = 0; i <= n - 1; i++)
{
int j = rand.Next(i, n);
Card tempCard = orderedCards[i];
orderedCards[i] = orderedCards[j];
orderedCards[j] = tempCard;
}//for
this.Cards = orderedCards;
this.TopCardIndex = 0;
ui.UIDebug("Deck has been shuffled.");
}//ShuffleDeck
public Card DrawCard()
{
Card topCard = Cards[this.TopCardIndex];
this.TopCardIndex++;
if (TopCardIndex > Cards.Length - 1)
{
this.ShuffleDeck();
topCard = Cards[this.TopCardIndex];
}//if
return topCard;
}//DrawCard
override public string ToString(){
String output = "";
foreach (Card c in Cards)
{
output = output + c.ToString() + System.Environment.NewLine;
}//foreach
return output;
}//ToString()
}//Deck
}