forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.1.h
81 lines (78 loc) · 1.56 KB
/
7.1.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
79
80
81
#include <vector>
#include <cstdlib>
using namespace std;
enum Suit {
Spade, Heart, Club, Diamond
};
class Card {
private:
int v;
Suit s;
bool available;
public:
Card(){
available = true;
}
Card(int val, Suit su){
v = val;
s = su;
available = true;
}
int value(){
return v;
}
Suit suit(){
return s;
}
bool is_available(){
return available;
}
void mark_unavailable(){
available = false;
}
void mark_available(){
available = true;
}
};
class Deck {
private:
vector<Card> deck;
int used;
public:
Deck(){
for(int i=1; i<=13; ++i)
for(int j=0; j<4; ++j)
deck.push_back(Card(i, (Suit)j));
used = 0;
}
Deck(vector<Card> c){
deck.assign(c.begin(), c.end());
used = 0;
}
void shuffle(){
srand((unsigned)time(NULL));
for(int i=0; i<deck.size(); ++i){
int j = rand()%(deck.size()-i) + i;
Card t = deck[i];
deck[i] = deck[j];
deck[j] = t;
}
}
int remain_cards(){
return deck.size() - used;
}
Card deal_card(){
//if(remain_cards() == 0) return null;
Card c = deck[used];
deck[used].mark_unavailable();
++used;
return c;
}
vector<Card> deal_hand(int num){
vector<Card> hand;
if(remain_cards() < num) return hand;
while(num-- > 0)
hand.push_back(deal_card());
return hand;
}
};