-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.cs
153 lines (140 loc) · 3.86 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
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
144
145
146
147
148
149
150
151
152
153
/*
CSC 350H Spring 2022
Program: Card Game Elevens
Student: Thamires Andrade
Professor: Hao Tang
Description: can generate and manage a deck of 52 playing cards.
*/
using System;
using System.Collections.Generic;
namespace CardGame_ProjectTwo
{
public class Deck
{
List<Card> deck = new List<Card>(); // 52 cards
int count = 0;
public List<Card> GetDeck() { return deck; }
public int Count { get { return count; } }
public int GetSize() { return count; }
/*
Generates 52 cards for the deck
*/
public void Generate()
{
for (int i = 0; i < 52; i++)
{
Suit s = (Suit)Math.Floor(Convert.ToDecimal(i / 13));
int rank = i % 13 + 1;
Card c = new Card(rank, s);
deck.Add(c);
count++;
}
}
/*
Handles regular and special card names
*/
private string SettingCardsRankAndName(Card card)
{
// note: I might add a rank name to replace card.rank for all
switch (card.rank)
{
case 1:
return "Ace of " + card.name.ToString();
case 11:
return "Jack of " + card.name.ToString();
case 12:
return "Queen of " + card.name.ToString();
case 13:
return "King of " + card.name.ToString();
default:
return card.rank + " of " + card.name.ToString();
}
}
/*
Prints deck
*/
public void Print()
{
int i = -1;
foreach (Card c in deck)
{
i++;
Console.WriteLine(" " + i + " - " + SettingCardsRankAndName(c));
}
}
/*
Shuffles deck
*/
public void Shuffle()
{
Random rand = new Random();
for (int i = deck.Count - 1; i > 0; i--)
{
int randomIndex = rand.Next(i + 1);
Card tempCard = deck[i]; // temp that holds the last element
deck[i] = deck[randomIndex];
deck[randomIndex] = tempCard;
}
}
/*
Returns the top card of a deck
*/
public Card TopCard()
{
if (deck.Count > 0)
{
Card TopCard = deck[deck.Count - 1];
deck.RemoveAt(deck.Count - 1);
count--;
return TopCard;
}
return null;
} // end of TopCard()
/*
Adds a card to deck
*/
public void Add(Card c)
{
deck.Add(c);
count++;
}
/*
Returns a card from a given index
*/
public Card getByIndex(int index)
{
for (int i = 0; i < deck.Count; i++)
{
Card card = deck[i];
if (i == index)
{
return card;
}
}
return null;
}
/*
Access a card from a deck by index
*/
public Card this[int index]
{
get => (Card) deck[index];
set => deck.Insert(index, value);
}
/*
Removes a card from deck using a given index
*/
public void RemoveAt(int index)
{
for (int i = deck.Count - 1; i >= 0; i--)
{
Card card = deck[i];
if (i == index)
{
deck.RemoveAt(index);
count--;
}
}
}
}
}