-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.cpp
More file actions
70 lines (56 loc) · 1.66 KB
/
Copy pathdeck.cpp
File metadata and controls
70 lines (56 loc) · 1.66 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
#include "deck.h"
#include <algorithm>
#include <cassert>
Deck::Deck() {
for (int ii = 0; ii < 52; ++ii)
residueDeck.push_back(ii);
}
void Deck::checkFullAss() const {
vector<IdxCard> test(residueDeck);
std::sort(test.begin(), test.end());
assert( test.size() == 52 );
for (int ii = 0; ii < test.size(); ++ii)
assert( test.at(ii) == ii );
}
IdxCard Deck::takeByIndex(int idx) {
IdxCard res = residueDeck.at(idx);
residueDeck.erase(residueDeck.begin() + idx);
return res;
}
int Deck::findCard(IdxCard card) const {
auto itr = std::find(residueDeck.begin(), residueDeck.end(), card);
if (itr == residueDeck.end())
return -1;
return itr - residueDeck.begin();
}
IdxCard Deck::takeOne(IdxCard card) {
// изъятие одной указанной карты
int idx = findCard(card);
assert( idx >= 0 );
return takeByIndex(idx);
}
void Deck::takeList(const ListCards& cards) {
// изъятие списка указанных карт
for (const auto& card: cards)
takeOne(card);
}
IdxCard Deck::takeRandom() {
// изъятие одной случайной карты
int idx = rand() % residueCount();
return takeByIndex(idx);
}
ListCards Deck::takeRandomCount(int cnt) {
ListCards res;
res.reserve(cnt);
for (int ii = 0; ii < cnt; ++ii)
res.push_back(takeRandom());
return res;
}
void Deck::recall(IdxCard card) {
// возврат карты обратно в пачку неизвестных
residueDeck.push_back(card);
}
void Deck::recallList(const ListCards& cards) {
for (const IdxCard& card: cards)
recall(card);
}