-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.cpp
More file actions
91 lines (79 loc) · 2.02 KB
/
deck.cpp
File metadata and controls
91 lines (79 loc) · 2.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "blackjack.h"
#include <vector>
#include <random>
#include <algorithm>
#include <string>
std::random_device rd;
std::mt19937 gen(rd());
const int MAX_CARD_NUM = 13;
std::vector<int> available_suits = { 0,1,2,3 };
//Can't convert string to enum in c++ so returning corresponding enum integer given a string
int return_suit_index(std::string suit_str) {
if (suit_str == "spades") {
return suits::spades;
}
else if (suit_str == "clubs") {
return suits::clubs;
}
else if (suit_str == "hearts") {
return suits::hearts;
}
else if (suit_str == "diamonds") {
return suits::diamonds;
}
return 0;
}
//Deck constructor
deck::deck() {
for (int i = 0; i < 4; i++) {
std::vector<card*> tmp;
for (int j = 0; j < MAX_CARD_NUM; j++) {
card* pcard = new card();
if (j + 1 == 11 || j + 1 == 12 || j + 1 == 13) {
pcard->value = 10;
}
else {
pcard->value = j + 1;
}
switch (i) {
case suits::spades:
pcard->suit = "spades";
case suits::clubs:
pcard->suit = "clubs";
case suits::hearts:
pcard->suit = "hearts";
case suits::diamonds:
pcard->suit = "diamonds";
}
tmp.push_back(pcard);
}
cards.push_back(tmp);
}
}
//Return random card
card* deck::pop_random() {
std::uniform_int_distribution<> dist_suit(0, cards.size() - 1);
int suit_idx = dist_suit(gen);
std::uniform_int_distribution<> dist_num(0, cards[suit_idx].size() - 1);
int num_idx = dist_num(gen);
card* pcard = cards[suit_idx][num_idx];
int idx = return_suit_index(pcard->suit);
cards[idx].erase(
std::remove_if(
cards[idx].begin(),
cards[idx].end(),
[pcard](const card* c) {
return c->value == pcard->value;
}
),
cards[idx].end()
);
return pcard;
}
//Reset deck by appending erased cards to the right suit vector in cards
void deck::reset_deck() {
for (int i = 0; i < erased_cards.size(); i++) {
int idx = return_suit_index(erased_cards[i]->suit);
cards[idx].push_back(erased_cards[i]);
}
}