-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.cpp
73 lines (60 loc) · 1.17 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
#ifndef _DECK_CPP_
#define _DECK_CPP_
#include "./card.cpp"
#include <random>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define DECK_SIZE 52
class Deck{
std::vector<Card> cards;
int index;
public:
Deck(): cards(){
init();
}
~Deck(){}
void init(){
cards.resize(DECK_SIZE);
for(int i = 0; i < DECK_SIZE; ++i){
Card &c = cards[i];
c.setNumber(i / SUIT_SIZE + 1);
switch(i % SUIT_SIZE){
case 0:
c.setSuit(Suit::SPADE);
break;
case 1:
c.setSuit(Suit::CLOVER);
break;
case 2:
c.setSuit(Suit::HEART);
break;
case 3:
c.setSuit(Suit::DIAMOND);
break;
default:
std::cerr << "error: unknown Suit\n";
exit(1);
}
}
index = 0;
shuffle();
}
void shuffle(){
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(cards.begin(), cards.end(), g);
}
bool hasNext(){
return index < DECK_SIZE;
}
Card dealNext(){
if(!hasNext()){
std::cerr << "error: no card remains\n";
exit(1);
}
return cards[index++];
}
};
#endif