-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.h
78 lines (63 loc) · 1.77 KB
/
Deck.h
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
//
// Created by Kai Blahnik on 7/6/22.
//
#ifndef POKER_DECK_H
#define POKER_DECK_H
#include <vector>
#include <random>
#include <string>
#include "Card.h"
using std::vector, std::string;
class Deck {
public:
explicit Deck(string n) {
name = std::move(n);
}
void fill() {
for(int s = 0; s <= 3; s++) {
for(int r = 2; r <= 14; ++r) {
cards.emplace_back(r, s);
}
}
}
void shuffle() {
std::random_device device;
std::mt19937 rng(device());
std::uniform_int_distribution<std::mt19937::result_type> card_dist(0, cards.size() - 1);
for(int card1 = 0; card1 < cards.size(); ++card1) {
unsigned int card2 = card_dist(rng);
while(card2 == card1) {
card2 = card_dist(rng);
}
Card temp = cards[card1];
cards[card1] = cards[card2];
cards[card2] = temp;
}
}
Card dealCard(Deck & discard) {
if(cards.empty()) {
if(discard.cards.empty())
throw std::runtime_error("Ran out of cards");
addCards(discard.cards);
discard.cards.clear();
}
Card card = cards.back();
cards.pop_back();
return card;
}
void addCards(vector<Card> newCards) {
cards.insert(cards.end(), newCards.begin(), newCards.end());
}
friend std::ostream & operator<<(std::ostream & strm, const Deck & deck) {
strm << deck.name << " has the following cards: ";
for(Card card : deck.cards) {
strm << "\n\t" << card;
}
return strm;
}
const vector<Card> & getCards() const {return cards;}
private:
string name;
vector<Card> cards;
};
#endif //POKER_DECK_H