-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.cs
171 lines (162 loc) · 6.69 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Linq; // currently only needed if we use alternate shuffle method
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
namespace RaceTo21_Blazor
{
public class Deck
{
//replaced string with Card class to store short and long name
//List<string> cards = new List<string>();
//save cards privately
private readonly List<Card> cards = new List<Card>();
public Deck()
{
Console.WriteLine("Building deck...");
//string[] suits = { "S", "H", "C", "D" };
//since we need longname and shortname
//we can only store longnames of the suits and get its first character for shortname
string[] suits = { "Spades", "Hearts", "Clubs", "Diamonds" };
//use icon to show cards in the webpage
//create a dictionary by key is suits and value is icon
Dictionary<string, string> cardIcon = new Dictionary<string, string>()
{
{"Spades", "♠"},
{"Hearts", "♥"},
{"Clubs", "♣"},
{"Diamonds", "♦"},
};
//run for loop with card number, and add into the deck
for (int cardNum = 1; cardNum <= 13; cardNum++)
{
//run loop in suits array to create cards for each number
foreach (string cardSuit in suits)
{
string shortName;
string longName;
//check card num in different condition
switch (cardNum)
{
//if card num = 1
case 1:
shortName = "A";
longName = "Ace";
break;
case 11:
shortName = "J";
longName = "Jack";
break;
case 12:
shortName = "Q";
longName = "Queen";
break;
case 13:
shortName = "K";
longName = "King";
break;
//if not above
default:
//change int to string
shortName = cardNum.ToString();
//get long name by cardNum and index of numToWords
string[] numToWords = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
//the index start from 0, so we need to - 1
longName = numToWords[cardNum-1];
break;
}
//add the card to the card list with id(inculding icon) and displayName
//cards.Add(new Card(shortName + cardSuit.First<char>(), longName + " of " + cardSuit));
cards.Add(new Card(shortName + cardIcon[cardSuit], longName + " of " + cardSuit));
//-----the other way to add the card image without a method or using Regex
/*if (shortName == "A" || shortName == "J" || shortName == "Q" || shortName == "K")
{
cardImg.Add(shortName + cardSuit.First<char>(), "card_" + cardSuit.ToLower() + "_" + shortName + ".png");
}
else
{
cardImg.Add(shortName + cardSuit.First<char>(), "card_" + cardSuit.ToLower() + "_" + shortName.PadLeft(2, '0') + ".png");
}*/
}
}
}
/*
* shuffle the card in the deck
* call: SetCardImg()
* called by: game
* parameter: no
* return: no (void)
*/
public void ShuffleDeck()
{
Console.WriteLine("Shuffling Cards...");
//create a Random as rng
Random rng = new Random();
//run for loop by cards' count to shuffle cards list
for (int i=0; i<cards.Count; i++)
{
//replaced string with Card class
//string tmp = cards[i];
//create a new card by current index to exchange to the new index, save in tmp
Card tmp = cards[i];
//get a random num in cards.Count
int swapindex = rng.Next(cards.Count);
//change current card to the new index's card
cards[i] = cards[swapindex];
//change the new index's card to current card (tmp)
cards[swapindex] = tmp;
}
}
/*
* show all cards on the console
* call: no
* called by: game if needed
* parameter: no
* return: no (void)
*/
public void ShowAllCards()
{
//run for loop by cards.Count to write cards in a line
for (int i=0; i<cards.Count; i++)
{
//Console.Write(i+":"+cards[i]); // a list property can look like an Array!
//Console.Write(i + ":" + cards[i].id);
//show the long name in console
//shows like 1:Ace of Club
Console.Write(i + ":" + cards[i].displayName);
if (i < cards.Count -1)
{
//separate names
Console.Write(" ");
}
else
{
//the last one write above names on console
Console.WriteLine("");
}
}
}
/*
* get the last card in deck
* There are two args in the Card.Card field
* so we need to give the two args back when we need to add the card into player's hand
* when (nextTask == Task.PlayerTurn)
* call: no
* called by: game
* parameter: no
* return: string, string - card id, card long name
*/
public (string, string) DealTopCard()
{
//string card = cards[cards.Count - 1];
//the index start from 0, so we need to -1
//get the last card's id
string card = cards[cards.Count - 1].id;
//get the last card's long name
string cardLong = cards[cards.Count - 1].displayName;
//remove the last card from the deck
cards.RemoveAt(cards.Count - 1);
return (card, cardLong);
}
}
}