-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.cpp
94 lines (72 loc) · 1.75 KB
/
Deck.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
/*
Deck.cpp
Definitions for Deck class.
*/
#include <cstdlib>
#include "Deck.h"
using namespace std;
// stores required constants
namespace {
const int CARD_COUNT = 52;
const int HAND_SIZE = 13;
}
// Deck constructor
Deck::Deck() : cards_left_(CARD_COUNT) {
reset();
}
// Deck destructor
Deck::~Deck() {
// get rid of all playing cards
for (int i = 0; i < cards_.size(); i++) {
delete cards_[i];
}
}
// repopulate deck
void Deck::repopulate() {
cards_left_ = CARD_COUNT;
}
// copied and pasted from given shuffle.cpp
/* To ensure that the your cards are ordered the same way as
the given program, use this shuffling algorithm.
CARD_COUNT is the constant 52
cards_ is an array of pointers to cards
*/
void Deck::shuffle(){
int n = CARD_COUNT;
while ( n > 1 ) {
int k = (int) (lrand48() % n);
--n;
cards_.swap(n,k);
}
}
// resets deck to sorted state
void Deck::reset() {
// easiest to wipe out deck and recreate it
// unallocates all cards
for (int i = 0; i < cards_.size(); i++) {
delete cards_[i];
}
// empty list of cards
cards_.clear();
// reinitalizes all cards
for (int s = 0; s < SUIT_COUNT; s++) {
for (int r = 0; r < RANK_COUNT; r++) {
Card *c = new Card((Suit)s, (Rank)r);
cards_.add(c);
}
}
}
// returns a vector of 13 cards for a player to use
CardList Deck::dealPlayerHand() {
CardList hand;
// grab next 13 cards and copy them to hand vector
for (int i = 0; i < HAND_SIZE; i++) {
int index = CARD_COUNT - cards_left_ + i;
if (index >= 0) {
hand.add(cards_[index]);
}
}
// mark those 13 cards as used
cards_left_ -= HAND_SIZE;
return hand;
}