forked from ibenes/freecell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card-storage.cc
181 lines (143 loc) · 3.97 KB
/
card-storage.cc
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
#include "card-storage.h"
bool operator== (const std::optional<Card> &lhs, const std::optional<Card> rhs) {
if (lhs.has_value() && rhs.has_value()) {
return *lhs == *rhs;
} else if (!lhs.has_value() && !rhs.has_value()) {
return true;
} else {
return false;
}
}
bool HomeDestination::canSitOn(const Card &base, const Card &candidate) {
return candidate.color == base.color && candidate.value == base.value + 1;
}
bool HomeDestination::canAccept(const Card & card) const {
if (storage_.size() == 0)
return card.value == 1;
else
return canSitOn(*topCard(), card);
}
bool HomeDestination::acceptCard(const Card & card) {
auto move_ok = canAccept(card);
if (move_ok)
storage_.push_back(card);
return move_ok;
}
const std::optional<Card> HomeDestination::topCard() const {
if (storage_.size() > 0)
return storage_.back();
else
return std::nullopt;
}
bool operator< (const HomeDestination &lhs, const HomeDestination &rhs) {
return lhs.topCard() < rhs.topCard();
}
bool operator== (const HomeDestination &lhs, const HomeDestination &rhs) {
return lhs.topCard() == rhs.topCard();
}
bool operator< (const WorkStack &lhs, const WorkStack &rhs) {
return lhs.storage_ < rhs.storage_;
}
bool operator== (const WorkStack &lhs, const WorkStack &rhs) {
return lhs.storage_ == rhs.storage_;
}
std::optional<Card> HomeDestination::getCard() {
auto card = storage_.back();
storage_.pop_back();
return card;
}
std::ostream& operator<< (std::ostream& os, const HomeDestination & hd) {
if (hd.storage_.size() == 0)
os << "_";
else
os << *hd.topCard();
return os;
}
bool FreeCell::canAccept([[maybe_unused]] const Card & card) const {
return !cell_.has_value();
}
bool FreeCell::acceptCard(const Card & card) {
auto move_ok = canAccept(card);
if (move_ok)
cell_.emplace(card);
return move_ok;
}
FreeCell & FreeCell::operator=(FreeCell &other) {
if (other.cell_.has_value())
cell_.emplace(*other.cell_);
else
cell_ = std::nullopt;
return *this;
}
const std::optional<Card> FreeCell::topCard() const {
return cell_;
}
std::optional<Card> FreeCell::getCard() {
auto card = std::move(cell_);
cell_.reset();
return card;
}
bool operator< (const FreeCell &lhs, const FreeCell &rhs) {
return lhs.topCard() < rhs.topCard();
}
bool operator== (const FreeCell &lhs, const FreeCell &rhs) {
return lhs.topCard() == rhs.topCard();
}
std::ostream& operator<< (std::ostream& os, const FreeCell & fc) {
auto card = fc.topCard();
if (card.has_value()) {
os << *card;
} else {
os << "_";
}
return os;
}
bool WorkStack::canSitOn(const Card &base, const Card &candidate) {
bool oppposing_render_color = render_color_map.at(candidate.color) != render_color_map.at(base.color);
bool one_less = candidate.value == base.value - 1;
return oppposing_render_color && one_less;
}
bool WorkStack::canAccept(const Card & card) const {
if (storage_.size() == 0)
return true;
else
return canSitOn(*topCard(), card);
}
bool WorkStack::acceptCard(const Card & card) {
auto move_ok = canAccept(card);
if (move_ok)
storage_.push_back(card);
return move_ok;
}
const std::optional<Card> WorkStack::topCard() const {
if (storage_.size() > 0)
return storage_.back();
else
return std::nullopt;
}
std::optional<Card> WorkStack::getCard() {
if (storage_.size() > 0) {
auto card = storage_.back();
storage_.pop_back();
return card;
} else {
return std::nullopt;
}
}
void WorkStack::forceCard(const Card & card) {
storage_.push_back(card);
}
std::ostream& operator<< (std::ostream& os, const WorkStack & stack) {
if (stack.storage_.size() == 0) {
os << "_";
} else {
os << stack.storage_[0];
for (auto card_it = stack.storage_.begin() + 1; card_it != stack.storage_.end(); ++card_it) {
os << " " << *card_it;
}
}
return os;
}
size_t WorkStack::nbCards() const {
return storage_.size();
}