-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSimpleBot.cc
281 lines (243 loc) · 8.87 KB
/
SimpleBot.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <cassert>
#include "Hanabi.h"
#include "SimpleBot.h"
using namespace Hanabi;
CardKnowledge::CardKnowledge()
{
for (Color color = RED; color <= BLUE; ++color) {
colors_[color] = MAYBE;
}
for (int value = 1; value <= 5; ++value) {
values_[value] = MAYBE;
}
isPlayable = false;
}
bool CardKnowledge::mustBe(Hanabi::Color color) const { return (colors_[color] == YES); }
bool CardKnowledge::mustBe(Hanabi::Value value) const { return (values_[value] == YES); }
bool CardKnowledge::cannotBe(Hanabi::Color color) const { return (colors_[color] == NO); }
bool CardKnowledge::cannotBe(Hanabi::Value value) const { return (values_[value] == NO); }
int CardKnowledge::value() const
{
for (int i=1; i <= 5; ++i) {
if (this->mustBe(Value(i))) return i;
}
assert(false);
return -1;
}
void CardKnowledge::setMustBe(Hanabi::Color color)
{
assert(colors_[color] != NO);
for (Color k = RED; k <= BLUE; ++k) {
colors_[k] = ((k == color) ? YES : NO);
}
}
void CardKnowledge::setMustBe(Hanabi::Value value)
{
assert(values_[value] != NO);
for (int v = 1; v <= 5; ++v) {
values_[v] = ((v == value) ? YES : NO);
}
}
SimpleBot::SimpleBot(int index, int numPlayers, int handSize)
{
me_ = index;
handKnowledge_.resize(numPlayers);
for (int i=0; i < numPlayers; ++i) {
handKnowledge_[i].resize(handSize);
}
}
void SimpleBot::invalidateKnol(int player_index, int card_index)
{
/* The other cards are shifted down and a new one drawn at the end. */
std::vector<CardKnowledge> &vec = handKnowledge_[player_index];
for (int i = card_index; i+1 < vec.size(); ++i) {
vec[i] = vec[i+1];
}
vec.back() = CardKnowledge();
}
void SimpleBot::pleaseObserveBeforeMove(const Server &server)
{
assert(server.whoAmI() == me_);
}
void SimpleBot::pleaseObserveBeforeDiscard(const Hanabi::Server &server, int from, int card_index)
{
assert(server.whoAmI() == me_);
this->invalidateKnol(from, card_index);
}
void SimpleBot::pleaseObserveBeforePlay(const Hanabi::Server &server, int from, int card_index)
{
assert(server.whoAmI() == me_);
this->invalidateKnol(from, card_index);
this->wipeOutPlayables(server.activeCard());
}
void SimpleBot::pleaseObserveColorHint(const Hanabi::Server &server, int /*from*/, int to, Color color, Hanabi::CardIndices card_indices)
{
assert(server.whoAmI() == me_);
/* Someone has given P a color hint. Using SimpleBot's strategy,
* this means that all the named cards are playable. */
Pile pile = server.pileOf(color);
int value = pile.size() + 1;
assert(1 <= value && value <= 5);
for (int i=0; i < card_indices.size(); ++i) {
CardKnowledge &knol = handKnowledge_[to][card_indices[i]];
knol.setMustBe(color);
knol.setMustBe(Value(value));
knol.isPlayable = true;
}
}
void SimpleBot::pleaseObserveValueHint(const Hanabi::Server &server, int from, int to, Value value, Hanabi::CardIndices card_indices)
{
assert(server.whoAmI() == me_);
/* Someone has given P a value hint. Using SimpleBot's strategy,
* this means that all the named cards are playable...
* unless the hint looks like it was just an attempt to free up
* a hint stone for discarding. */
const bool isHintStoneReclaim =
(!server.discardingIsAllowed()) &&
(from == (to+1) % server.numPlayers()) &&
card_indices.contains(0);
if (isHintStoneReclaim) {
return;
}
for (int i=0; i < card_indices.size(); ++i) {
CardKnowledge &knol = handKnowledge_[to][card_indices[i]];
knol.setMustBe(value);
knol.isPlayable = true;
}
}
void SimpleBot::pleaseObserveAfterMove(const Hanabi::Server &server)
{
assert(server.whoAmI() == me_);
}
void SimpleBot::wipeOutPlayables(const Card &played_card)
{
const int numPlayers = handKnowledge_.size();
for (int player = 0; player < numPlayers; ++player) {
for (int c = 0; c < handKnowledge_[player].size(); ++c) {
CardKnowledge &knol = handKnowledge_[player][c];
if (!knol.isPlayable) continue;
if (knol.mustBe(Value(5))) continue;
if (knol.cannotBe(played_card.color)) continue;
if (knol.cannotBe(played_card.value)) continue;
/* This card might or might not be playable, anymore. */
knol.isPlayable = false;
}
}
}
bool SimpleBot::maybePlayLowestPlayableCard(Server &server)
{
/* Find the lowest-valued playable card in my hand. */
int best_index = -1;
int best_value = 6;
for (int i=0; i < handKnowledge_[me_].size(); ++i) {
const CardKnowledge &knol = handKnowledge_[me_][i];
if (knol.isPlayable && knol.value() < best_value) {
best_index = i;
best_value = knol.value();
}
}
/* If I found a card to play, play it. */
if (best_index != -1) {
assert(1 <= best_value && best_value <= 5);
server.pleasePlay(best_index);
return true;
}
return false;
}
bool SimpleBot::maybeGiveHelpfulHint(Server &server)
{
if (server.hintStonesRemaining() == 0) return false;
const int numPlayers = handKnowledge_.size();
int best_so_far = 0;
int player_to_hint = -1;
int color_to_hint = -1;
int value_to_hint = -1;
for (int i = 1; i < numPlayers; ++i) {
const int partner = (me_ + i) % numPlayers;
assert(partner != me_);
const std::vector<Card> partners_hand = server.handOfPlayer(partner);
bool is_really_playable[5];
for (int c=0; c < partners_hand.size(); ++c) {
is_really_playable[c] =
server.pileOf(partners_hand[c].color).nextValueIs(partners_hand[c].value);
}
/* Can we construct a color hint that gives our partner information
* about unknown-playable cards, without also including any
* unplayable cards? */
for (Color color = RED; color <= BLUE; ++color) {
int information_content = 0;
bool misinformative = false;
for (int c=0; c < partners_hand.size(); ++c) {
if (partners_hand[c].color != color) continue;
if (is_really_playable[c] &&
!handKnowledge_[partner][c].isPlayable)
{
information_content += 1;
} else if (!is_really_playable[c]) {
misinformative = true;
break;
}
}
if (misinformative) continue;
if (information_content > best_so_far) {
best_so_far = information_content;
color_to_hint = color;
value_to_hint = -1;
player_to_hint = partner;
}
}
for (int value = 1; value <= 5; ++value) {
int information_content = 0;
bool misinformative = false;
for (int c=0; c < partners_hand.size(); ++c) {
if (partners_hand[c].value != value) continue;
if (is_really_playable[c] &&
!handKnowledge_[partner][c].isPlayable)
{
information_content += 1;
} else if (!is_really_playable[c]) {
misinformative = true;
break;
}
}
if (misinformative) continue;
if (information_content > best_so_far) {
best_so_far = information_content;
color_to_hint = -1;
value_to_hint = value;
player_to_hint = partner;
}
}
}
if (best_so_far == 0) return false;
/* Give the hint. */
if (color_to_hint != -1) {
server.pleaseGiveColorHint(player_to_hint, Color(color_to_hint));
} else if (value_to_hint != -1) {
server.pleaseGiveValueHint(player_to_hint, Value(value_to_hint));
} else {
assert(false);
}
return true;
}
void SimpleBot::pleaseMakeMove(Server &server)
{
assert(server.whoAmI() == me_);
assert(server.activePlayer() == me_);
/* If I have a playable card, play it.
* Otherwise, if someone else has an unknown-playable card, hint it.
* Otherwise, just discard my oldest (index-0) card. */
if (maybePlayLowestPlayableCard(server)) return;
if (maybeGiveHelpfulHint(server)) return;
/* We couldn't find a good hint to give, or else we're out of hint-stones.
* Discard a card. However, discarding is not allowed when we have all
* the hint stones, so in that case, just hint to the player on our right
* about his oldest card. */
if (!server.discardingIsAllowed()) {
const int numPlayers = server.numPlayers();
const int right_partner = (me_ + numPlayers - 1) % numPlayers;
server.pleaseGiveValueHint(right_partner, server.handOfPlayer(right_partner)[0].value);
} else {
server.pleaseDiscard(0);
}
}