-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.cpp
193 lines (164 loc) · 4.84 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "Deck.h"
#include <stdexcept>
#include <iostream> // for test
#include <cstdlib>
#include <algorithm> // find
#include <vector>
// whether ace is high is defined here (1-13 or 2-14)
const int Deck::LOW = 2;
const int Deck::HIGH = 14;
const int Deck::VALUE_COUNT = HIGH + 1 - LOW;
const int Deck::LAST_SUIT = SUIT_COUNT - 1;
Deck::iterator Deck::begin() const
{
iterator itr_to_return(this);
itr_to_return.current_set = 0;
itr_to_return.inside_iterator = cards[sort_order[itr_to_return.current_set]].begin();
itr_to_return.check_between_sets();
return itr_to_return;
}
Deck::iterator Deck::end() const
{
iterator itr_to_return(this);
itr_to_return.current_set = LAST_SUIT;
itr_to_return.inside_iterator = cards[sort_order[itr_to_return.current_set]].end();
return itr_to_return;
}
Deck::Deck(const bool& create_full /*= false*/)
{
card_count = 0;
if (create_full)
fill();
for (int suit = 0; suit < SUIT_COUNT; ++suit)
{
sort_order[suit] = Suit(suit); // default sort order
}
}
void Deck::change_sort(const std::vector<Suit>& suits_in_order)
{
// make sure all suits are there
bool all_found = false;
if (suits_in_order.size() == SUIT_COUNT)
{
all_found = true;
bool found[SUIT_COUNT];
for (int i = 0; i < SUIT_COUNT; ++i)
found[i] = false;
for (int i = 0; i < SUIT_COUNT; ++i)
found[suits_in_order[i]] = true;
for (int i = 0; i < SUIT_COUNT; ++i)
if (! found[i])
{
all_found = false;
break;
}
}
if (! all_found)
throw std::invalid_argument("invalid suit order");
for (int i = 0; i < SUIT_COUNT; ++i)
{
sort_order[i] = suits_in_order[i];
}
}
void Deck::erase(const Card& card_to_remove)
{
if (cards[card_to_remove.get_suit()].erase(card_to_remove)) // TODO: does this remove the right card (after order change)?
{
--card_count;
//test
/*
if (card_count < 13)
std::cout << "successfully erased: " << card_to_remove.str() << std::endl; */
}
// test
/*
else
std::cout << "tried to erase a card not there: " << card_to_remove.str() << std::endl; */
}
Card Deck::deal_one()
{
auto deck_itr = begin();
int choice = rand() % card_count; // used as the iterating variable to advance iterator
while (choice > 0)
{
++deck_itr;
--choice;
}
Card to_return = *deck_itr;
erase(to_return);
return to_return;
}
std::vector<Card> Deck::pick_random(int n) const
{
// std::cout << "entered pick_random to pick " << n << std::endl;
std::vector<Card> to_return;
iterator deck_itr(this);
int choice;
for (; n > 0; --n)
{
// std::cout << "enter loop needing " << n << " picks\n";
deck_itr = begin();
choice = rand() % (card_count - to_return.size());
while (true) // exit condition in the middle, top is (choice >= 0), bottom is (choice > 0)
{
bool this_might_be_in_vector = true;
while (this_might_be_in_vector)
{
// std::cout << "haven't found it not in vector\n";
std::vector<Card>::iterator to_ret_itr = std::find(to_return.begin(), to_return.end(), *deck_itr);
if (to_ret_itr == to_return.end()) // got through the vector without finding it
this_might_be_in_vector = false;
else // *deck_itr is already in vector to_return
++deck_itr;
}
if (choice == 0)
break;
// std::cout << "need to advance deck_itr " << choice << std::endl;
++deck_itr;
--choice;
}
to_return.push_back(*deck_itr);
// test
// std::cout << deck_itr->str() << " added to random 3\n";
}
return to_return;
}
void Deck::insert(const Card& card)
{
if (cards[card.get_suit()].insert(card).second) // second is bool whether inserted
++card_count;
}
void Deck::clear()
{
for (int i = 0; i < SUIT_COUNT; ++i)
cards[i].clear();
card_count = 0;
}
void Deck::fill()
{
for (int suit = 0; suit < SUIT_COUNT; ++suit)
{
for (int value = LOW; value <= HIGH; ++value)
{
if (cards[suit].insert(Card(value, Suit(suit))).second) // sets might be not empty
++card_count;
}
}
}
const Card& Deck::at(int index) const
{
auto itr = begin();
while (index > 0)
{
++itr;
--index;
}
return *itr;
}
bool Deck::contains_non_points() const
{
return (cards[CLUBS].size() ||
cards[DIAMONDS].size() ||
(cards[SPADES].size() > 1) ||
(cards[SPADES].size() /* exactly 1 spade */ && (cards[SPADES].begin()->get_value() != 12)));
}