-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.cpp
More file actions
70 lines (58 loc) · 1.62 KB
/
deck.cpp
File metadata and controls
70 lines (58 loc) · 1.62 KB
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
#include "deck.h"
#include <chrono>
#include <algorithm>
// Seed randomness for later shuffle using system time
Deck::Deck() : seed(std::chrono::system_clock::now().time_since_epoch().count()), e(seed) {
}
/*
Fill the deck with the default 52-card deck,
shorten the deck from the low side (positive value)
or from the high side (negative value),
add jokers
*/
void Deck::populate(int shorten, int jokers){
std::vector<std::string> ranks = {
"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"
};
std::string suits[] = {
"Diamonds", "Hearts", "Clubs", "Spades"
};
if (shorten > 0){
// Shorten the deck from lowest rank ascending
for(int i = shorten; i > 0; i--)
ranks.erase(ranks.begin());
} else if (shorten < 0){
// Shorten the deck from highest rank descending
for(int i = shorten; i < 0; i++)
ranks.pop_back();
}
// Populate deck
for (const auto& rank : ranks) {
for (const auto& suit : suits) {
deck.emplace_back(rank, suit);
}
}
// Add Jokers
for (int i = jokers; i > 0; i--){
Card* newJokerCard = new Card("Joker", "Joker");
deck.push_back(*newJokerCard);
}
}
// Take card from the back of the vector
Card Deck::drawCard(){
Card card = deck.back();
deck.pop_back();
return card;
}
// Add card to the back of the vector
void Deck::addCard(Card card){
deck.push_back(card);
}
int Deck::getNumCards() const {
return deck.size();
}
void Deck::shuffle(){
std::shuffle(deck.begin(), deck.end(), e);
}
Deck::~Deck() {
}