-
Notifications
You must be signed in to change notification settings - Fork 4
/
Trick.cpp
57 lines (48 loc) · 1.21 KB
/
Trick.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
#include "Trick.h"
#include "Game.h"
void Trick::addCard(Card *c, int p) {
_cards.emplace_back(c, p);
}
Color::Type Trick::getStartingColor() {
if (_cards.empty()) {
return Color::NOTRUMP;
} else {
return _cards.front().first->getColor();
}
}
int Trick::getWinner(Contract *c) {
std::list<pair<Card *, int>>::iterator it = _cards.begin();
pair<Card *, int> biggest = *it;
for (++it; it != _cards.end(); ++it) {
if (it->first->isBigger(biggest.first, c)) {
biggest = *it;
}
}
return biggest.second;
}
vector<Card *> Trick::getCards() {
vector<Card *> cardsOnTable;
if (_cards.empty()) {
return cardsOnTable;
} else {
std::list<pair<Card *, int>>::iterator it = _cards.begin();
for (++it; it != _cards.end(); ++it) {
cardsOnTable.push_back(it->first);
}
return cardsOnTable;
}
}
json Trick::serialize() {
json cards;
for (auto c: _cards) {
cards += {c.second, c.first->serialize()};
}
return cards;
}
Trick *Trick::unserialize(json data) {
Trick *t = new Trick;
for (const auto &c: data) {
t->addCard(Card::get(c[1]), c[0]);
}
return t;
}