-
Notifications
You must be signed in to change notification settings - Fork 6
/
InfoBot.cc
1503 lines (1375 loc) · 52.8 KB
/
InfoBot.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Hanabi.h"
#include "InfoBot.h"
#include <algorithm>
#include <cassert>
#include <memory>
#include <vector>
#define MAXPLAYERS 10 // use statically sized arrays instead of vector in some places
#define MAXHANDSIZE 5 // use statically sized arrays instead of vector in some places
using Hanabi::Card;
using Hanabi::CardIndices;
using Hanabi::Color;
using Hanabi::Value;
using Hanabi::RED;
using Hanabi::BLUE;
template<class T, int Cap>
class fixed_capacity_vector {
int size_ = 0;
alignas(T) char buffer_[Cap][sizeof(T)];
public:
fixed_capacity_vector() = default;
explicit fixed_capacity_vector(int initial_size) noexcept {
for (int i=0; i < initial_size; ++i) {
this->emplace_back();
}
}
fixed_capacity_vector(const fixed_capacity_vector& rhs) noexcept {
for (const auto& elt : rhs) {
this->emplace_back(elt);
}
}
fixed_capacity_vector(fixed_capacity_vector&& rhs) noexcept {
for (auto& elt : rhs) {
this->emplace_back(std::move(elt));
}
}
fixed_capacity_vector& operator=(const fixed_capacity_vector& rhs) = delete;
~fixed_capacity_vector() {
for (int i=0; i < size_; ++i) {
(*this)[i].~T();
}
}
template<class... Args>
void emplace_back(Args&&... args) {
assert(size_ < Cap);
new ((void*)buffer_[size_]) T(std::forward<Args>(args)...);
size_ += 1;
}
void erase(T *it) {
assert(this->begin() <= it && it < this->end());
size_ -= 1;
for (int i = (it - begin()); i < size_; ++i) {
(*this)[i] = std::move((*this)[i+1]);
}
(*this)[size_].~T();
}
const T& operator[](int i) const { return *(const T*)buffer_[i]; }
T& operator[](int i) { return *(T*)buffer_[i]; }
const T& back() const { return *(const T*)buffer_[size_-1]; }
T& back() { return *(T*)buffer_[size_-1]; }
const T *begin() const { return (const T*)buffer_[0]; }
const T *end() const { return (const T*)buffer_[size_]; }
T *begin() { return (T*)buffer_[0]; }
T *end() { return (T*)buffer_[size_]; }
int size() const { return size_; }
bool empty() const { return size() == 0; }
};
template<class T, int Cap>
class fixed_capacity_set {
fixed_capacity_vector<T, Cap> elements_;
public:
void insert(T t) {
for (auto&& elt : elements_) {
if (elt == t) return;
}
elements_.emplace_back(std::move(t));
}
const T *begin() const { return elements_.begin(); }
const T *end() const { return elements_.end(); }
T *begin() { return elements_.begin(); }
T *end() { return elements_.end(); }
int size() const { return elements_.size(); }
bool empty() const { return elements_.empty(); }
};
struct Hinted {
enum Kind { COLOR, VALUE };
explicit Hinted(Kind kind, int k) : kind(kind), color(Color(-1)), value(Value(-1)) {
if (kind == Hinted::COLOR) color = Color(k);
if (kind == Hinted::VALUE) value = Value(k);
}
static Hinted WithColor(Color k) { return Hinted(COLOR, int(k)); }
static Hinted WithValue(Value v) { return Hinted(VALUE, int(v)); }
static Hinted Dummy() { return Hinted(COLOR, 0); }
Kind kind;
Color color;
Value value;
friend bool operator==(const Hinted& a, const Hinted& b) noexcept {
return (a.kind == b.kind) && (a.color == b.color) && (a.value == b.value);
}
};
struct Hint : public Hinted {
int to;
explicit Hint(Kind kind, int to, int k) : Hinted(kind, k), to(to) {}
};
class CardCounts {
int8_t counts_[Hanabi::NUMCOLORS][5+1] {};
public:
explicit CardCounts() = default;
void increment(Card card) {
counts_[card.color][card.value] += 1;
}
int remaining(Card card) const {
return card.count() - counts_[card.color][card.value];
}
};
class GameView {
public:
CardCounts discard;
int fireworks[Hanabi::NUMCOLORS];
int deck_size;
int total_cards;
int num_players;
int hand_size;
int discard_size;
int hints_remaining;
int lives_remaining;
int player; // whose turn is it?
private:
int hints_total;
int lives_total;
public:
explicit GameView(const Hanabi::Server& server) {
deck_size = server.cardsRemainingInDeck();
total_cards = Hanabi::NUMCOLORS * 10;
discard_size = server.discards().size();
for (Card card : server.discards()) {
discard.increment(card);
}
for (Color k = RED; k <= BLUE; ++k) {
fireworks[k] = server.pileOf(Color(k)).size();
}
num_players = server.numPlayers();
player = server.activePlayer();
hand_size = server.handSize();
hints_total = Hanabi::NUMHINTS;
hints_remaining = server.hintStonesRemaining();
lives_total = Hanabi::NUMMULLIGANS;
lives_remaining = server.mulligansRemaining();
}
explicit GameView(int numPlayers, int handSize) {
deck_size = Hanabi::NUMCOLORS * 10;
total_cards = Hanabi::NUMCOLORS * 10;
for (Color k = RED; k <= BLUE; ++k) {
fireworks[k] = 0;
}
num_players = numPlayers;
player = 0;
hand_size = handSize;
hints_total = Hanabi::NUMHINTS;
hints_remaining = Hanabi::NUMHINTS;
lives_total = Hanabi::NUMMULLIGANS;
lives_remaining = Hanabi::NUMMULLIGANS;
}
// returns whether a card would place on a firework
bool is_playable(Card card) const {
return card.value == this->fireworks[card.color] + 1;
}
// best possible value we can get for firework of that color,
// based on looking at discard + fireworks
bool is_higher_than_highest_attainable(Card card) const {
assert(card.value > this->fireworks[card.color]);
assert(this->discard.remaining(card) != 0); // called only on a card in someone's hand
for (int v = this->fireworks[card.color] + 1; v < card.value; ++v) {
Card needed_card(card.color, v);
if (this->discard.remaining(needed_card) == 0) {
// already discarded all of these
return true;
}
}
return false;
}
// is never going to play, based on discard + fireworks
bool is_dead(Card card) const {
return (
card.value <= this->fireworks[card.color] ||
this->is_higher_than_highest_attainable(card)
);
}
// can be discarded without necessarily sacrificing score, based on discard + fireworks
bool is_dispensable(Card card) const {
return (this->discard.remaining(card) != 1) || this->is_dead(card);
}
};
class OwnedGameView : public GameView {
const Hanabi::Server *server;
public:
explicit OwnedGameView(const GameView& view, const Hanabi::Server& server) : GameView(view), server(&server) {}
const std::vector<Card>& get_hand(int p) const {
static_assert(std::is_same<const std::vector<Card>&, decltype(server->handOfPlayer(p))>::value, "");
return server->handOfPlayer(p);
}
bool has_card(int player, Card card) const {
for (Card c : server->handOfPlayer(player)) {
if (c == card) return true;
}
return false;
}
bool can_see(Card card) const {
int me = server->whoAmI();
for (int p=0; p < num_players; ++p) {
if (p != me && this->has_card(p, card)) return true;
}
return false;
}
bool someone_else_can_play() const {
int me = server->whoAmI();
for (int p=0; p < server->numPlayers(); ++p) {
if (p == me) continue;
for (auto&& card : server->handOfPlayer(p)) {
if (this->is_playable(card)) {
return true;
}
}
}
return false;
}
};
class CardPossibilityTable {
int8_t counts_[Hanabi::NUMCOLORS][5+1] {};
public:
explicit CardPossibilityTable() {
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
counts_[k][v] = Hanabi::Card(k, v).count();
}
}
}
static CardPossibilityTable from(const CardCounts& counts) {
CardPossibilityTable result;
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
result.counts_[k][v] = counts.remaining(Hanabi::Card(k, v));
}
}
return result;
}
void mark_false(Card card) {
counts_[card.color][card.value] = 0;
}
void mark_color(Color color, bool yes) {
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
if ((k == color) != yes) {
mark_false(Card(k, v));
}
}
}
}
void mark_value(Value value, bool yes) {
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
if ((v == value) != yes) {
mark_false(Card(k, v));
}
}
}
}
bool is_possible(Card card) const {
return (counts_[card.color][card.value] != 0);
}
bool can_be_color(Color color) const {
for (int v = 1; v <= 5; ++v) {
if (counts_[color][v] != 0) return true;
}
return false;
}
bool can_be_value(Value value) const {
for (Color k = RED; k <= BLUE; ++k) {
if (counts_[k][value] != 0) return true;
}
return false;
}
void decrement_weight_if_possible(Card card) {
auto& count = counts_[card.color][card.value];
if (count) count -= 1;
}
template<class F>
double weighted_score(const F& score_fn) const {
double total_score = 0;
int total_weight = 0;
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
if (counts_[k][v] != 0) {
int weight = counts_[k][v];
double score = score_fn(Card(k, v));
total_weight += weight;
total_score += weight * score;
}
}
}
return total_score / total_weight;
}
double average_value() const {
return this->weighted_score([](Card card) { return int(card.value); });
}
int total_weight() const {
int result = 0;
this->for_each_possibility_by_count([&](Card, int count) { result += count; });
return result;
}
template<class F>
double probability_of_predicate(const F& predicate) const {
return this->weighted_score([&](Card card) { return predicate(card) ? 1 : 0; });
}
double probability_is_dead(const GameView& view) const {
int total_dead = 0;
int total_live = 0;
for (Color k = RED; k <= BLUE; ++k) {
int next = view.fireworks[k] + 1;
int v = 1;
for (; v < next; ++v) {
total_dead += counts_[k][v];
}
for (; v <= 5 && view.discard.remaining(Card(k, v)) != 0; ++v) {
total_live += counts_[k][v];
}
for (; v <= 5; ++v) {
total_dead += counts_[k][v];
}
}
return total_dead / double(total_dead + total_live);
}
double probability_is_playable(const GameView& view) const {
return this->probability_of_predicate([&](Card card) { return view.is_playable(card); });
}
double probability_is_dispensable(const GameView& view) const {
return this->probability_of_predicate([&](Card card) { return view.is_dispensable(card); });
}
template<class F>
void for_each_possibility(const F& fn) const {
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
if (counts_[k][v] != 0) {
fn(Card(k, v));
}
}
}
}
template<class F>
void for_each_possibility_by_count(const F& fn) const {
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
if (counts_[k][v] != 0) {
fn(Card(k, v), counts_[k][v]);
}
}
}
}
bool is_determined() const {
int count = 0;
this->for_each_possibility([&](Card) { ++count; });
return count == 1;
}
template<class F>
void if_is_determined(const F& fn) const {
int count = 0;
this->for_each_possibility([&](Card) { ++count; });
if (count == 1) {
this->for_each_possibility(fn);
}
}
bool color_determined() const {
bool found = false;
for (Color k = RED; k <= BLUE; ++k) {
for (int v = 1; v <= 5; ++v) {
if (counts_[k][v] != 0) {
if (found) return false;
found = true;
break;
}
}
}
assert(found); // otherwise we didn't find any possibilities at all
return false;
}
bool value_determined() const {
bool found = false;
for (int v = 1; v <= 5; ++v) {
for (Color k = RED; k <= BLUE; ++k) {
if (counts_[k][v] != 0) {
if (found) return false;
found = true;
break;
}
}
}
assert(found); // otherwise we didn't find any possibilities at all
return false;
}
};
using HandInfo = fixed_capacity_vector<CardPossibilityTable, MAXHANDSIZE>;
using SinglePlayerHintSet = fixed_capacity_set<Hinted, 2*MAXHANDSIZE>;
struct ModulusInformation {
int modulus;
int value;
explicit ModulusInformation(int m, int v) : modulus(m), value(v) {
assert(value < modulus);
}
static ModulusInformation none() {
return ModulusInformation(1, 0);
}
void combine(ModulusInformation other) {
value = value * other.modulus + other.value;
modulus *= other.modulus;
}
ModulusInformation split(int m) {
assert(modulus >= m);
assert(modulus % m == 0);
int original_modulus = modulus;
int original_value = value;
modulus /= m;
int v = value / modulus;
value -= (v * modulus);
assert(original_modulus == m * modulus);
assert(original_value == v * modulus + value);
return ModulusInformation(m, v);
}
void cast_up(int m) {
assert(modulus <= m);
assert(value < m);
modulus = m;
}
void cast_down(int m) {
assert(modulus >= m);
assert(value < m);
modulus = m;
}
void add(ModulusInformation other) {
assert(modulus == other.modulus);
value = (value + other.value) % modulus;
}
void subtract(ModulusInformation other) {
assert(modulus == other.modulus);
value = (modulus + value - other.value) % modulus;
}
};
class IsPlayable {
int index;
public:
IsPlayable() = default;
explicit IsPlayable(int i) : index(i) {}
int info_amount() const { return 2; }
int answer(const std::vector<Card>& hand, const GameView& view) const {
const Card& card = hand[this->index];
if (view.is_playable(card)) {
return 1;
} else {
return 0;
}
}
void acknowledge_answer(int answer, HandInfo& hand_info, const GameView& view) const {
auto& card_table = hand_info[this->index];
card_table.for_each_possibility([&](Card card) {
if (view.is_playable(card)) {
if (answer == 0) { card_table.mark_false(card); }
} else {
if (answer == 1) { card_table.mark_false(card); }
}
});
}
};
class CardToIntMap {
int8_t map_[Hanabi::NUMCOLORS][5+1] {};
public:
void emplace(Card key, int value) {
map_[key.color][key.value] = value;
}
int at(Card key) const { return map_[key.color][key.value]; }
};
class CardPossibilityPartition {
int index;
int n_partitions;
CardToIntMap partition;
public:
CardPossibilityPartition() = default;
explicit CardPossibilityPartition(
int index, int max_n_partitions, const CardPossibilityTable& card_table, const GameView& view)
{
int cur_block = 0;
CardToIntMap partition;
int n_partitions = 0;
bool has_dead = card_table.probability_is_dead(view) != 0.0;
// TODO: group things of different colors and values?
int effective_max = max_n_partitions;
if (has_dead) {
effective_max -= 1;
}
card_table.for_each_possibility([&](Card card) {
if (!view.is_dead(card)) {
partition.emplace(card, cur_block);
cur_block = (cur_block + 1) % effective_max;
if (n_partitions < effective_max) {
n_partitions += 1;
}
}
});
if (has_dead) {
card_table.for_each_possibility([&](Card card) {
if (view.is_dead(card)) {
partition.emplace(card, n_partitions);
}
});
n_partitions += 1;
}
this->index = index;
this->n_partitions = n_partitions;
this->partition = std::move(partition);
}
int info_amount() const { return this->n_partitions; }
int answer(const std::vector<Card>& hand, const GameView&) const {
const auto& card = hand[this->index];
return this->partition.at(card);
}
void acknowledge_answer(int answer, HandInfo& hand_info, const GameView&) const {
auto& card_table = hand_info[this->index];
card_table.for_each_possibility([&](Card card) {
if (this->partition.at(card) != answer) {
card_table.mark_false(card);
}
});
}
};
class Question {
enum Kind { Q_IS_PLAYABLE, Q_PARTITION };
Kind kind;
std::aligned_union_t<1, ::IsPlayable, ::CardPossibilityPartition> storage;
template<class F>
auto visit(const F& fn) const {
switch (kind) {
case Q_IS_PLAYABLE: return fn(*(::IsPlayable*)&this->storage);
case Q_PARTITION: return fn(*(::CardPossibilityPartition*)&this->storage);
default: assert(false); __builtin_unreachable();
}
}
public:
int info_amount() const {
return visit([&](auto&& impl){ return impl.info_amount(); });
}
int answer(const std::vector<Card>& hand, const GameView& view) const {
return visit([&](auto&& impl){ return impl.answer(hand, view); });
}
void acknowledge_answer(int answer, HandInfo& info, const GameView& view) const {
return visit([&](auto&& impl){ return impl.acknowledge_answer(answer, info, view); });
}
ModulusInformation answer_info(const std::vector<Card>& hand, const GameView& view) const {
return ModulusInformation(
this->info_amount(),
this->answer(hand, view)
);
}
void acknowledge_answer_info(ModulusInformation answer, HandInfo& hand_info, const GameView& view) const {
assert(this->info_amount() == answer.modulus);
this->acknowledge_answer(answer.value, hand_info, view);
}
static Question IsPlayable(int i) {
Question result;
result.kind = Q_IS_PLAYABLE;
new (&result.storage) ::IsPlayable(i);
return result;
}
static Question CardPossibilityPartition(
int index, int max_n_partitions,
const CardPossibilityTable& card_table, const GameView& view
) {
Question result;
result.kind = Q_PARTITION;
new (&result.storage) ::CardPossibilityPartition(index, max_n_partitions, card_table, view);
return result;
}
};
struct AugmentedCardPossibilities {
CardPossibilityTable card_table;
int i;
double p_play;
double p_dead;
bool is_determined;
explicit AugmentedCardPossibilities(CardPossibilityTable ct, int i, const GameView& view) :
card_table(ct), i(i)
{
int n_play = 0;
int n_dead = 0;
int n_unique = 0;
int n_total = 0;
ct.for_each_possibility_by_count([&](Card card, int count) {
n_unique += 1;
n_total += count;
if (view.is_playable(card)) {
n_play += count;
} else if (view.is_dead(card)) {
n_dead += count;
}
});
this->p_play = n_play / (double)n_total;
this->p_dead = n_dead / (double)n_total;
this->is_determined = (n_unique == 1);
}
};
class HintStrategyImpl {
protected:
static int get_hint_index_score(const CardPossibilityTable& card_table, const GameView& view) {
if (card_table.probability_is_dead(view) == 1.0) {
return 0;
}
if (card_table.is_determined()) {
return 0;
}
// Do something more intelligent?
unsigned score = 0;
card_table.for_each_possibility([&](Card card) {
score |= (1u << int(card.color));
score |= (1u << (int(card.value) + 5));
});
return __builtin_popcount(score);
}
static int get_index_for_hint(const HandInfo& info, const GameView& view) {
fixed_capacity_vector<int, MAXHANDSIZE> scores;
for (const auto& card_table : info) {
scores.emplace_back(get_hint_index_score(card_table, view));
}
return std::max_element(scores.begin(), scores.end()) - scores.begin();
}
};
class HintStrategy3 : public HintStrategyImpl {
int card_index;
public:
explicit HintStrategy3(const HandInfo& info, const GameView& view) {
card_index = get_index_for_hint(info, view);
}
int get_count() const { return 3; }
template<class F>
void encode_hint(const OwnedGameView& view, int to, int hint_type, const F& fn) const {
const auto& hand = view.get_hand(to);
const auto& hint_card = hand[card_index];
if (hint_type == 0) {
fn( Hinted::WithValue(hint_card.value) );
} else if (hint_type == 1) {
fn( Hinted::WithColor(hint_card.color) );
} else {
for (auto&& card : hand) {
if (card.color != hint_card.color) fn( Hinted::WithColor(card.color) );
if (card.value != hint_card.value) fn( Hinted::WithValue(card.value) );
}
}
}
int decode_hint(Hint hint, CardIndices card_indices) const {
if (card_indices.contains(card_index)) {
if (hint.kind == Hinted::VALUE) {
return 0;
} else {
return 1;
}
} else {
return 2;
}
}
};
class HintStrategy4 : public HintStrategyImpl {
int card_index;
public:
explicit HintStrategy4(const HandInfo& info, const GameView& view) {
card_index = get_index_for_hint(info, view);
}
int get_count() const { return 4; }
template<class F>
void encode_hint(const OwnedGameView& view, int to, int hint_type, const F& fn) const {
const auto& hand = view.get_hand(to);
const auto& hint_card = hand[card_index];
if (hint_type == 0) {
fn( Hinted::WithValue(hint_card.value) );
} else if (hint_type == 1) {
fn( Hinted::WithColor(hint_card.color) );
} else if (hint_type == 2) {
for (auto&& card : hand) {
if (card.value != hint_card.value) fn( Hinted::WithValue(card.value) );
}
} else {
for (auto&& card : hand) {
if (card.color != hint_card.color) fn( Hinted::WithColor(card.color) );
}
}
}
int decode_hint(Hint hint, CardIndices card_indices) const {
if (card_indices.contains(card_index)) {
if (hint.kind == Hinted::VALUE) {
return 0;
} else {
return 1;
}
} else {
if (hint.kind == Hinted::VALUE) {
return 2;
} else {
return 3;
}
}
}
};
class HintStrategySetPacking {
int count_;
int color_to_int_[Hanabi::NUMCOLORS];
int value_to_int_[5+1];
public:
explicit HintStrategySetPacking(const HandInfo& info) {
// Make a matrix with info.size() rows and NUMCOLORS columns.
// Set each cell (r,c) to 1 if the r'th card could be of color c.
// Find the max-cardinality set of disjoint rows; this is the
// NP-complete "set-cover" problem, so we just do brute force
// search over the at-most-32 possible row-sets.
// Each row in the max-cardinality set corresponds to a
// card in the hand where touching that card with a color hint
// cannot possibly be confused with any other hint generated
// by this algorithm.
//
int nrows = info.size();
unsigned rowsetmax = (1u << nrows);
unsigned largest_disjoint_color_rowset = 1;
int best_cardinality = 1;
for (unsigned rowset = 3; rowset < rowsetmax; ++rowset) {
int cardinality_of_rowset = __builtin_popcount(rowset);
if (cardinality_of_rowset <= best_cardinality) {
// Go on only if this rowset has a chance of being a new "best".
continue;
}
bool rowset_is_disjoint = true;
unsigned kmask = 0;
for (int i=0; i < nrows; ++i) {
if (rowset & (1u << i)) {
info[i].for_each_possibility([&](Card card) {
unsigned km = (1u << int(card.color));
if (kmask & km) {
rowset_is_disjoint = false;
}
kmask |= km;
});
}
}
if (rowset_is_disjoint) {
best_cardinality = cardinality_of_rowset;
largest_disjoint_color_rowset = rowset;
}
}
// Also perform the same operation, but for value hints.
//
unsigned largest_disjoint_value_rowset = 1;
best_cardinality = 1;
for (unsigned rowset = 3; rowset < rowsetmax; ++rowset) {
int cardinality_of_rowset = __builtin_popcount(rowset);
if (cardinality_of_rowset <= best_cardinality) {
// Go on only if this rowset has a chance of being a new "best".
continue;
}
bool rowset_is_disjoint = true;
unsigned vmask = 0;
for (int i=0; i < nrows; ++i) {
if (rowset & (1u << i)) {
info[i].for_each_possibility([&](Card card) {
unsigned vm = (1u << int(card.value));
if (vmask & vm) {
rowset_is_disjoint = false;
}
vmask |= vm;
});
}
}
if (rowset_is_disjoint) {
best_cardinality = cardinality_of_rowset;
largest_disjoint_value_rowset = rowset;
}
}
// Now generate our mapping from hints to ints.
std::fill(color_to_int_, std::end(color_to_int_), -1);
std::fill(value_to_int_, std::end(value_to_int_), -1);
count_ = 0;
for (int i = 0; i < nrows; ++i) {
if (largest_disjoint_color_rowset & (1u << i)) {
info[i].for_each_possibility([&](Card card) {
color_to_int_[card.color] = count_;
});
count_ += 1;
}
if (largest_disjoint_value_rowset & (1u << i)) {
info[i].for_each_possibility([&](Card card) {
value_to_int_[card.value] = count_;
});
count_ += 1;
}
}
// Some colors and values might not be touched by any of the rows
// in our largest disjoint rowset. Permit those hints to be given,
// since their "overt" information might be useful to the hintee.
int cur_block = 0;
for (Color k = RED; k <= BLUE; ++k) {
if (color_to_int_[k] == -1) {
color_to_int_[k] = cur_block;
cur_block = (cur_block + 1) % count_;
}
}
for (int v = 1; v <= 5; ++v) {
if (value_to_int_[v] == -1) {
value_to_int_[v] = cur_block;
cur_block = (cur_block + 1) % count_;
}
}
}
int get_count() const { return count_; }
template<class F>
void encode_hint(const OwnedGameView& view, int to, int hint_type, const F& fn) const {
assert(0 <= hint_type && hint_type < get_count());
for (auto&& card : view.get_hand(to)) {
if (color_to_int_[card.color] == hint_type) fn( Hinted::WithColor(card.color) );
if (value_to_int_[card.value] == hint_type) fn( Hinted::WithValue(card.value) );
}
}
int decode_hint(Hint hint, CardIndices) const {
if (hint.kind == Hinted::COLOR) {
return color_to_int_[hint.color];
} else {
return value_to_int_[hint.value];
}
}
};
class InfoBotImpl;
class HintStrategy {
enum Kind { HS_HINT3, HS_HINT4, HS_SETPACKING };
Kind kind;
std::aligned_union_t<1, ::HintStrategy3, ::HintStrategy4, ::HintStrategySetPacking> storage;
template<class F>
auto visit(const F& fn) const {
switch (kind) {
case HS_HINT3: return fn(*(::HintStrategy3*)&this->storage);
case HS_HINT4: return fn(*(::HintStrategy4*)&this->storage);
case HS_SETPACKING: return fn(*(::HintStrategySetPacking*)&this->storage);
default: assert(false); __builtin_unreachable();
}
}
public:
int get_count() const {
return visit([&](auto&& impl){ return impl.get_count(); });
}
template<class F>
void encode_hint(const OwnedGameView& view, int to, int hint_type, const F& fn) const {
return visit([&](auto&& impl){ return impl.encode_hint(view, to, hint_type, fn); });
}
int decode_hint(Hint hint, CardIndices card_indices) const {
return visit([&](auto&& impl) { return impl.decode_hint(hint, card_indices); });
}
static HintStrategy Make3(const HandInfo& info, const GameView& view) {
HintStrategy result;
result.kind = HS_HINT3;
new (&result.storage) ::HintStrategy3(info, view);
return result;
}
static HintStrategy Make4(const HandInfo& info, const GameView& view) {
HintStrategy result;
result.kind = HS_HINT4;
new (&result.storage) ::HintStrategy4(info, view);
return result;
}
static HintStrategy MakeSetPacking(const HandInfo& info) {
HintStrategy result;
result.kind = HS_SETPACKING;
new (&result.storage) ::HintStrategySetPacking(info);
return result;
}
};
class InfoBotImpl : public InfoBot {
int me;
int numPlayers;
std::vector<HandInfo> public_info;
CardCounts public_counts; // what any newly drawn card could be
GameView last_view; // the view on the previous turn
public:
InfoBotImpl(int index, int numPlayers, int handSize) :
me(index), numPlayers(numPlayers), last_view(numPlayers, handSize)
{
for (int i=0; i < numPlayers; ++i) {
this->public_info.emplace_back(HandInfo(handSize));
}
}
fixed_capacity_vector<Question, 2*MAXHANDSIZE> get_questions(int total_info, const GameView& view, const HandInfo& hand_info) const {
fixed_capacity_vector<Question, 2*MAXHANDSIZE> questions;
int info_remaining = total_info;
auto add_question = [&](Question question) {
info_remaining /= question.info_amount();
questions.emplace_back(std::move(question));
return info_remaining <= 1;
};
fixed_capacity_vector<AugmentedCardPossibilities, MAXHANDSIZE> augmented_hand_info;
bool any_known_playable = false;
for (int i=0; i < (int)hand_info.size(); ++i) {
augmented_hand_info.emplace_back(hand_info[i], i, view);
if (augmented_hand_info.back().p_play == 1.0) {
any_known_playable = true;
}
}
if (!any_known_playable) {
fixed_capacity_vector<AugmentedCardPossibilities, MAXHANDSIZE> ask_play;
for (auto&& knol : augmented_hand_info) {
if (knol.is_determined) continue;
if (knol.p_dead == 1.0) continue;
if (knol.p_play == 1.0 || knol.p_play < 0.2) continue;
ask_play.emplace_back(knol);
}
// sort by probability of play, then by index
std::sort(ask_play.begin(), ask_play.end(), [](auto&& a, auto&& b) {
// *higher* probabilities are better
if (a.p_play != b.p_play) return (a.p_play > b.p_play);
return (a.i < b.i);
});
for (const auto& knol : ask_play) {
auto q = Question::IsPlayable(knol.i);
if (add_question(std::move(q))) {
return questions;
}
}
}
fixed_capacity_vector<AugmentedCardPossibilities, MAXHANDSIZE> ask_partition;
for (auto&& knol : augmented_hand_info) {
if (knol.is_determined) continue;
// TODO: possibly still valuable to ask?