-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeckOfCards.cpp
172 lines (157 loc) · 4.65 KB
/
DeckOfCards.cpp
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
171
172
#include "DeckOfCards.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <random>
#include <algorithm>
#include <queue>
using namespace std;
// Constructor
DeckOfCards::DeckOfCards(){
buildDeck();
}
// Destructor
DeckOfCards::~DeckOfCards(){};
// Print deck
void DeckOfCards::printDeck() {
for (int i = 0; i < 52; i++) {
cout << deck[i]->suit << " " << deck[i]->name << endl;
}
}
// Pick card from specified position
Card* DeckOfCards::pickCardFromPosition(int pos) {
Card *tmp = deck[pos];
shiftDeckFromPosition(pos);
return tmp;
}
// Pick random card from the deck
Card* DeckOfCards::pickRandomCard() {
srand(time(NULL)); // Seeding rand()
int randNum = rand()%(deckSize + 1); // Getting random number between 0 and 51
Card *tmp = deck[randNum];
shiftDeckFromPosition(randNum);
return tmp;
}
// Find card with specified name and suit
Card* DeckOfCards::getCard(string n, string s) {
Card *tmp;
bool found = false;
int i;
for (i = 0; i < deckSize; i++) {
if (deck[i]->name == n && deck[i]->suit == s) {
found = true;
tmp = deck[i];
break;
}
}
if (!found)
return NULL;
else {
shiftDeckFromPosition(i);
return tmp;
}
}
// Shuffle deck perfectly (non-realistic)
void DeckOfCards::perfectShuffle() {
random_shuffle(deck, deck+52);
}
// First element of deck becomes last element
// Deck shifts forward by 1
void DeckOfCards::shiftDeck() {
Card *tmp[deckSize];
for (int i = 0; i < deckSize - 1; i++) {
tmp[i] = deck[i+1];
}
tmp[deckSize - 1] = deck[0];
for (int i = 0; i < deckSize; i++)
deck[i] = tmp[i];
}
// Same as shiftDeck(), except deck[0] is replaced by deck[pos]
void DeckOfCards::shiftDeckFromPosition(int pos) {
Card *tmp[deckSize];
// Copying deck up to position
for (int i = 0; i < pos; i++) {
tmp[i] = deck[i];
}
// Copying deck except deck[pos]
for (int i = pos; i < deckSize - 1; i++) {
tmp[i] = deck[i+1];
}
tmp[deckSize - 1] = deck[pos];
// Copying tmp to deck
for (int i = 0; i < deckSize; i++)
deck[i] = tmp[i];
}
// Deal specified number of cards from deck
// Returns queue of cards
queue<Card*> DeckOfCards::dealCards(int numCards) {
queue<Card*> tmp;
for (int i = 0; i < numCards; i++) {
tmp.push(deck[0]);
shiftDeck();
}
return tmp;
}
// Return deck to original state
void DeckOfCards::rebuildDeck() {
// Delete everything in deck
for (int i = 0; i < deckSize; i++)
delete deck[i];
buildDeck(); // Rebuild
}
// Build ordered Deck
void DeckOfCards::buildDeck() {
int currentIndex = 0;
for (int i = 1; i < 14; i++) {
if (i == 1)
deck[currentIndex] = new Card(11, "ace", "spade");
else if (i > 1 && i < 11)
deck[currentIndex] = new Card(i, i, "spade");
else if (i == 11)
deck[currentIndex] = new Card(10, "jack", "spade");
else if (i == 12)
deck[currentIndex] = new Card(10, "queen", "spade");
else
deck[currentIndex] = new Card(10, "king", "spade");
currentIndex++;
}
for (int i = 1; i < 14; i++) {
if (i == 1)
deck[currentIndex] = new Card(11, "ace", "club");
else if (i > 1 && i < 11)
deck[currentIndex] = new Card(i, i, "club");
else if (i == 11)
deck[currentIndex] = new Card(10, "jack", "club");
else if (i == 12)
deck[currentIndex] = new Card(10, "queen", "club");
else
deck[currentIndex] = new Card(10, "king", "club");
currentIndex++;
}
for (int i = 1; i < 14; i++) {
if (i == 1)
deck[currentIndex] = new Card(11, "ace", "diamond");
else if (i > 1 && i < 11)
deck[currentIndex] = new Card(i, i, "diamond");
else if (i == 11)
deck[currentIndex] = new Card(10, "jack", "diamond");
else if (i == 12)
deck[currentIndex] = new Card(10, "queen", "diamond");
else
deck[currentIndex] = new Card(10, "king", "diamond");
currentIndex++;
}
for (int i = 1; i < 14; i++) {
if (i == 1)
deck[currentIndex] = new Card(11, "ace", "heart");
else if (i > 1 && i < 11)
deck[currentIndex] = new Card(i, i, "heart");
else if (i == 11)
deck[currentIndex] = new Card(10, "jack", "heart");
else if (i == 12)
deck[currentIndex] = new Card(10, "queen", "heart");
else
deck[currentIndex] = new Card(10, "king", "heart");
currentIndex++;
}
}