-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModBot.cc
1187 lines (1037 loc) · 36.3 KB
/
ModBot.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 <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include "Hanabi.h"
#include "ModBot.h"
// Based on AwwBot, this bot only plays the multicolor (very difficult) version of the game
#define NULLCOLOR ORANGE
using namespace Hanabi;
static const bool UseMulligans = true;
template<typename T>
static bool vector_contains(const std::vector<T> &vec, T value)
{
for (int i=0; i < vec.size(); ++i) {
if (vec[i] == value) return true;
}
return false;
}
CardKnowledge::CardKnowledge()
{
color_ = -1;
value_ = -1;
std::memset(cantBe_, '\0', sizeof cantBe_);
playable_ = valuable_ = worthless_ = MAYBE;
clued_ = false;
}
bool CardKnowledge::mustBe(Hanabi::Color color) const { return (this->color_ == color); }
bool CardKnowledge::mustBe(Hanabi::Value value) const { return (this->value_ == value); }
bool CardKnowledge::cannotBe(Hanabi::Card card) const { return cantBe_[card.color][card.value]; }
bool CardKnowledge::cannotBe(Hanabi::Color color) const
{
if (this->color_ != -1) return (this->color_ != color);
for (int v = 1; v <= 5; ++v) {
if (!cantBe_[color][v]) return false;
}
return true;
}
bool CardKnowledge::cannotBe(Hanabi::Value value) const
{
if (this->value_ != -1) return (this->value_ != value);
for (Color k = RED; k <= MULTI; ++k) {
if (!cantBe_[k][value]) return false;
}
return true;
}
int CardKnowledge::color() const { return this->color_; }
int CardKnowledge::value() const { return this->value_; }
bool CardKnowledge::clued() const { return this->clued_; }
void CardKnowledge::setMode(Hanabi::GameMode mode)
{
mode_ = mode;
}
void CardKnowledge::setClued(bool gotClued)
{
clued_ = gotClued;
}
int CardKnowledge::setMustBeMultiOr(Hanabi::Color color)
{
int tot = 0;
for (Color k = RED; k <= BLUE; ++k) {
if (k != color) tot += setCannotBe(k);
}
//color_ = color;
return tot;
}
int CardKnowledge::setMustBe(Hanabi::Color color)
{
int tot = 0;
for (Color k = RED; k <= MULTI; ++k) {
if (k != color) tot += setCannotBe(k);
}
color_ = color;
return tot;
}
int CardKnowledge::setMustBe(Hanabi::Value value)
{
int tot = 0;
for (int v = 1; v <= 5; ++v) {
if (v != value) tot += setCannotBe(Value(v));
}
value_ = value;
return tot;
}
int CardKnowledge::setCannotBeMulti()
{
int tot = 0;
for (int v = 1; v <= 5; ++v) {
if (!cantBe_[MULTI][v]) {
tot++;
cantBe_[MULTI][v] = true;
}
}
return tot;
}
int CardKnowledge::setCannotBe(Hanabi::Color color)
{
int tot = 0;
for (int v = 1; v <= 5; ++v) {
if (!cantBe_[color][v]) {
tot++;
cantBe_[color][v] = true;
}
}
return tot;
}
int CardKnowledge::setCannotBe(Hanabi::Value value)
{
int tot = 0;
for (Color k = RED; k <= MULTI; ++k) {
if (!cantBe_[k][value]) {
tot++;
cantBe_[k][value] = true;
}
}
return tot;
}
void CardKnowledge::setIsPlayable(const Server& server, bool knownPlayable)
{
for (Color k = RED; k <= MULTI; ++k) {
int playableValue = server.pileOf(k).size() + 1;
for (int v = 1; v <= 5; ++v) {
if (this->cantBe_[k][v]) continue;
if ((v == playableValue) != knownPlayable) {
this->cantBe_[k][v] = true;
}
}
}
this->playable_ = (knownPlayable ? YES : NO);
}
void CardKnowledge::setIsValuable(const ModBot &bot, const Server& server, bool knownValuable)
{
for (Color k = RED; k <= MULTI; ++k) {
for (int v = 1; v <= 5; ++v) {
if (this->cantBe_[k][v]) continue;
if (bot.isValuable(server, Card(k,v)) != knownValuable) {
this->cantBe_[k][v] = true;
}
}
}
this->valuable_ = (knownValuable ? YES : NO);
}
void CardKnowledge::setIsWorthless(const ModBot &bot, const Server& server, bool knownWorthless)
{
for (Color k = RED; k <= MULTI; ++k) {
for (int v = 1; v <= 5; ++v) {
if (this->cantBe_[k][v]) continue;
if (bot.isWorthless(server, Card(k,v)) != knownWorthless) {
this->cantBe_[k][v] = true;
}
}
}
this->worthless_ = (knownWorthless ? YES : NO);
}
void CardKnowledge::update(const Server &server, const ModBot &bot, bool useMyEyesight)
{
int color = this->color_;
int value = this->value_;
if (useMyEyesight) goto complicated_part;
repeat_loop:
// if card can't be 5 colors, it must be 6th color
if (color == -1) {
for (Color k = RED; k <= MULTI; ++k) {
if (this->cannotBe(k)) continue;
else if (color == -1) color = k;
else { color = -1; break; }
}
if (color != -1) this->setMustBe(Color(color));
}
// if card can't be 4 number, it must be 5th number
if (value == -1) {
for (int v = 1; v <= 5; ++v) {
if (this->cannotBe(Value(v))) continue;
else if (value == -1) value = v;
else { value = -1; break; }
}
if (value != -1) this->setMustBe(Value(value));
}
complicated_part:
assert(color == this->color_);
assert(value == this->value_);
/* Rule out any cards that have been completely played and/or discarded. */
if (value == -1 || color == -1) {
bool restart = false;
for (Color k = RED; k <= MULTI; ++k) {
for (int v = 1; v <= 5; ++v) {
if (this->cantBe_[k][v]) continue;
const int total = (v == 1 ? 3 : (v == 5 ? 1 : 2));
const int played = bot.playedCount_[k][v];
const int held = (useMyEyesight ? bot.eyesightCount_[k][v] : bot.locatedCount_[k][v]);
assert(played+held <= total);
if (played+held == total) {
this->cantBe_[k][v] = true;
restart = true;
}
}
}
if (restart) goto repeat_loop;
}
if (true) {
bool yesP = false, noP = false;
bool yesV = false, noV = false;
bool yesW = false, noW = false;
for (Color k = RED; k <= MULTI; ++k) {
int playableValue = server.pileOf(k).size() + 1;
for (int v = 1; v <= 5; ++v) {
if (this->cantBe_[k][v]) continue;
if (v < playableValue) {
noP = true;
noV = true;
yesW = true;
} else if (v == playableValue) {
yesP = true;
if (!yesV || !noV) {
const int count = Card(k,v).count();
if (bot.playedCount_[k][v] == count-1) {
yesV = true;
} else {
noV = true;
}
}
noW = true;
} else {
noP = true;
if (!yesV || !noV) {
if (bot.isValuable(server, Card(k,v))) {
yesV = true;
} else {
noV = true;
}
}
if (!yesW || !noW) {
if (bot.isWorthless(server, Card(k,v))) {
yesW = true;
} else {
noW = true;
}
}
}
}
if (yesP && yesV && yesW) break;
}
assert(yesP || noP);
assert(yesV || noV);
assert(yesW || noW);
this->playable_ = (yesP ? (noP ? MAYBE : YES) : NO);
this->valuable_ = (yesV ? (noV ? MAYBE : YES) : NO);
this->worthless_ = (yesW ? (noW ? MAYBE : YES) : NO);
}
if (worthless_ == YES) assert(valuable_ == NO);
if (worthless_ == YES) assert(playable_ == NO);
}
Hint::Hint()
{
fitness = -1;
color = -1;
value = -1;
to = -1;
}
void Hint::give(Server &server)
{
assert(to != -1);
if (color != -1) {
server.pleaseGiveColorHint(to, Color(color));
} else if (value != -1) {
server.pleaseGiveValueHint(to, Value(value));
} else {
assert(false);
}
}
ModBot::ModBot(int index, int numPlayers, int handSize, Hanabi::GameMode mode)
{
me_ = index;
mode_ = mode;
//TODONEXT
handKnowledge_.resize(numPlayers);
for (int i=0; i < numPlayers; ++i) {
handKnowledge_[i].resize(handSize);
}
maxEval_ = 20;
if (numPlayers >= 4) {
maxEval_ = 30;
}
for (int i=0; i < 5; ++i) {
playClued_[i] = false;
discardClued_[i] = false;
}
ignoreCluesIfDiscardValuable_ = false;
priorPlayerDiscarded_ = false;
std::memset(playedCount_, '\0', sizeof playedCount_);
}
bool ModBot::isPlayable(const Server &server, Card card) const
{
const int playableValue = server.pileOf(card.color).size() + 1;
return (card.value == playableValue);
}
bool ModBot::isValuable(const Server &server, Card card) const
{
/* A card which has not yet been played, and which is the
* last of its kind, is valuable. */
if (playedCount_[card.color][card.value] != card.count()-1) return false;
return !this->isWorthless(server, card);
}
bool ModBot::isWorthless(const Server &server, Card card) const
{
const int playableValue = server.pileOf(card.color).size() + 1;
if (card.value < playableValue) return true;
if (true) {
/* If all the red 4s are in the discard pile, then the red 5 is worthless.
* But doing this check all the time apparently lowers ModBot's average score! */
while (card.value > playableValue) {
--card.value;
if (playedCount_[card.color][card.value] == card.count()) return true;
}
}
return false;
}
/* Could "knol" be playable, if it were known to be of value "value"? */
bool ModBot::couldBePlayableWithValue(const Server &server, const CardKnowledge &knol, int value) const
{
if (value < 1 || 5 < value) return false;
if (knol.playable() != MAYBE) return (knol.playable() == YES);
for (Color k = RED; k <= MULTI; ++k) {
Card card(k, value);
if (knol.cannotBe(card)) continue;
if (this->isPlayable(server, card))
return true;
}
return false;
}
/* Could "knol" be valuable, if it were known to be of value "value"? */
bool ModBot::couldBeValuableWithValue(const Server &server, const CardKnowledge &knol, int value) const
{
if (value < 1 || 5 < value) return false;
if (knol.valuable() != MAYBE) return false;
for (Color k = RED; k <= MULTI; ++k) {
Card card(k, value);
if (knol.cannotBe(card)) continue;
if (this->isValuable(server, card))
return true;
}
return false;
}
void ModBot::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 ModBot::seePublicCard(const Card &card)
{
int &entry = this->playedCount_[card.color][card.value];
entry += 1;
assert(1 <= entry && entry <= card.count());
}
void ModBot::updateEyesightCount(const Server &server)
{
std::memset(this->eyesightCount_, '\0', sizeof this->eyesightCount_);
const int numPlayers = handKnowledge_.size();
for (int p=0; p < numPlayers; ++p) {
if (p == me_) {
for (int i=0; i < myHandSize_; ++i) {
CardKnowledge &knol = handKnowledge_[p][i];
if (knol.color() != -1 && knol.value() != -1) {
this->eyesightCount_[knol.color()][knol.value()] += 1;
}
}
} else {
const std::vector<Card> hand = server.handOfPlayer(p);
for (int i=0; i < hand.size(); ++i) {
const Card &card = hand[i];
this->eyesightCount_[card.color][card.value] += 1;
}
}
}
}
bool ModBot::updateLocatedCount(const Hanabi::Server &server)
{
int newCount[Hanabi::NUMCOLORS][5+1] = {};
for (int p=0; p < handKnowledge_.size(); ++p) {
for (int i=0; i < handKnowledge_[p].size(); ++i) {
CardKnowledge &knol = handKnowledge_[p][i];
int k = knol.color();
if (k != -1) {
int v = knol.value();
if (v != -1) {
newCount[k][v] += 1;
}
}
}
}
if (std::memcmp(this->locatedCount_, newCount, sizeof newCount) != 0) {
std::memcpy(this->locatedCount_, newCount, sizeof newCount);
return true; /* there was a change */
}
return false;
}
void ModBot::noValuableWarningWasGiven(const Hanabi::Server &server, int from)
{
/* Something just happened that wasn't a warning. If what happened
* wasn't a hint to the guy expecting a warning, then he can safely
* deduce that his card isn't valuable enough to warn about. */
/* The rules are different when there are no cards left to draw,
* or when valuable-warning hints can't be given. */
if (server.cardsRemainingInDeck() == 0) return;
if (server.hintStonesRemaining() == 0) return;
const int playerExpectingWarning = (from + 1) % handKnowledge_.size();
//const int discardIndex = this->nextDiscardIndex(server, playerExpectingWarning);
//if (discardIndex != -1) {
// handKnowledge_[playerExpectingWarning][discardIndex].setIsValuable(*this, server, false);
//}
}
void ModBot::pleaseObserveBeforeMove(const Server &server)
{
assert(server.whoAmI() == me_);
myHandSize_ = server.sizeOfHandOfPlayer(me_);
for (int p=0; p < handKnowledge_.size(); ++p) {
const int numCards = server.sizeOfHandOfPlayer(p);
assert(handKnowledge_[p].size() >= numCards);
handKnowledge_[p].resize(numCards);
}
std::memset(this->locatedCount_, '\0', sizeof this->locatedCount_);
this->updateLocatedCount(server);
do {
for (int p=0; p < handKnowledge_.size(); ++p) {
const int numCards = handKnowledge_[p].size();
for (int i=0; i < numCards; ++i) {
CardKnowledge &knol = handKnowledge_[p][i];
knol.setMode(mode_);
knol.update(server, *this, false);
}
}
} while (this->updateLocatedCount(server));
this->updateEyesightCount(server);
lowestPlayableValue_ = 6;
for (Color color = RED; color <= MULTI; ++color) {
lowestPlayableValue_ = std::min(lowestPlayableValue_, server.pileOf(color).size()+1);
}
for (Color k = RED; k <= MULTI; ++k) {
for (int v = 1; v <= 5; ++v) {
assert(this->locatedCount_[k][v] <= this->eyesightCount_[k][v]);
}
}
}
void ModBot::pleaseObserveBeforeDiscard(const Hanabi::Server &server, int from, int card_index)
{
//this->noValuableWarningWasGiven(server, from);
// If we didn't need to discard then next player(s) have no playable cards that are not
// already known playable
/*
if ((server.hintStonesRemaining() != 0) &&
(!handLocked_[from])) {
int nextPlayer = (from + 1) % server.numPlayers();
for (int c=0; c < server.sizeOfHandOfPlayer(nextPlayer); c++) {
CardKnowledge &knol = handKnowledge_[nextPlayer][c];
if (//knol.clued() &&
(knol.playable() != YES)) {
knol.setIsPlayable(server, false);
}
}
}
*/
assert(server.whoAmI() == me_);
Card card = server.activeCard();
if (isValuable(server, card)) {
// if we are discarding something valuable, ignore all future clues
for (int i=0; i < 5; ++i) {
playClued_[i] = false;
discardClued_[i] = false;
}
}
this->seePublicCard(card);
if (((from + 1) % server.numPlayers() == me_) &&
(isValuable(server, card))) {
// if prior player discarded and that card is now valuable then we should not discard.
// Here we set flag that will ensure we play or clue.
priorPlayerDiscarded_ = true;
priorDiscardColor_ = card.color;
priorDiscardValue_ = card.value;
}
this->invalidateKnol(from, card_index);
}
void ModBot::pleaseObserveBeforePlay(const Hanabi::Server &server, int from, int card_index)
{
assert(server.whoAmI() == me_);
priorPlayerDiscarded_ = false;
Card card = server.activeCard();
assert(handKnowledge_[from][card_index].worthless() != YES);
if (handKnowledge_[from][card_index].valuable() == YES) {
/* We weren't wrong about this card being valuable, were we? */
assert(this->isValuable(server, card));
}
this->seePublicCard(card);
this->invalidateKnol(from, card_index);
}
void ModBot::pleaseObserveColorHint(const Hanabi::Server &server, int from, int to, Color color, const std::vector<int> &card_indices)
{
assert(server.whoAmI() == me_);
priorPlayerDiscarded_ = false;
// a color clue to LH1 corresponds to an eval = value + 5
// a color clue to LH2 corresponds to an eval = value + 15
int numPlayers = server.numPlayers();
if ((from + 1) % numPlayers == to) {
observeEval(server, from, color + 5);
} else if ((from + 2) % numPlayers == to) {
observeEval(server, from, color + 15);
} else {
observeEval(server, from, color + 25);
}
const int toHandSize = server.sizeOfHandOfPlayer(to);
// set each card as mustBe/clued or cantBe
for (int i=0; i < toHandSize; ++i) {
CardKnowledge &knol = handKnowledge_[to][i];
if (vector_contains(card_indices, i)) {
knol.setClued(true);
knol.setMustBeMultiOr(color);
knol.update(server, *this, false);
} else {
knol.setCannotBe(color);
knol.setCannotBeMulti();
}
}
}
void ModBot::pleaseObserveValueHint(const Hanabi::Server &server, int from, int to, Value value, const std::vector<int> &card_indices)
{
assert(server.whoAmI() == me_);
priorPlayerDiscarded_ = false;
// a value clue to LH1 corresponds to an eval = value - 1
// a value clue to LH2 corresponds to an eval = value + 9
// a value clue to LH2 corresponds to an eval = value + 14
int numPlayers = server.numPlayers();
if ((from + 1) % numPlayers == to) {
observeEval(server, from, value - 1);
} else if ((from + 2) % numPlayers == to) {
observeEval(server, from, value + 9);
} else {
observeEval(server, from, value + 19);
}
const int toHandSize = server.sizeOfHandOfPlayer(to);
// set each card as mustBe/clued or cantBe
for (int i=0; i < toHandSize; ++i) {
CardKnowledge &knol = handKnowledge_[to][i];
if (vector_contains(card_indices, i)) {
knol.setClued(true);
knol.setMustBe(value);
knol.update(server, *this, false);
} else {
knol.setCannotBe(value);
}
}
}
void ModBot::observeEval(const Hanabi::Server &server, int from, int evalInput)
{
int numPlayers = server.numPlayers();
int eval[5];
int totalEval = evaluateAllHands(server, from, me_, eval);
totalEval = totalEval % maxEval_;
// Except for the clue giver (from) we need to calculate the eval for
// each player's own hand (since we can't see our own hand).
if (from != me_) {
// everyone except "from player" is effectively receiving this clue
eval[me_] = evalInput - totalEval;
if (eval[me_] < 0) eval[me_] += maxEval_;
}
// We can now determine what the players have been clued (play or discard).
// Order of precedence dictates a player with a "play" clue will play or
// sometimes clue to save a problem. However, LH1 cannot possibly clue after this
// to save something in LH2's hand (because they'd get the same clue again).
// If LH1 discards something valuable when we think they should play,
// ignore the rest of this clue. (Actually, for now, if anyone ever discards
// something valuable, we'll ignore all future clues.)
if (server.hintStonesRemaining() == 1) {
//ignoreCluesIfDiscardValuable_ = true;
}
for (int i=1; i<numPlayers; i++) {
int p = (from + i) % numPlayers;
playClued_[p] = false;
discardClued_[p] = false;
// If our own eval is >=15, make sure it is for us. If not, set it correctly.
if (eval[p] > 14) {
int otherp = (int((eval[p] - 5) / 5) + from) % numPlayers;
if (otherp != p) {
// need to swap evals of p and otherp
int tmp = eval[p] - (eval[p] % 5);
eval[p] = eval[p] % 5;
eval[otherp] += tmp;
}
}
cluedIndex_[p] = eval[p] % 5;
if ((eval[p] > 9) && (eval[p] < 15)) {
// do not discard anything unless forced
} else if (eval[p] < 5) {
playClued_[p] = true;
if (server.hintStonesRemaining() != 1) {
// this is definitely a playable card
CardKnowledge &knol = handKnowledge_[p][eval[p] % 5];
knol.setIsPlayable(server, true);
knol.update(server, *this, false);
playClued_[p] = true;
}
} else {
discardClued_[p] = true;
}
}
}
void ModBot::pleaseObserveAfterMove(const Hanabi::Server &server)
{
assert(server.whoAmI() == me_);
int player = server.activePlayer();
playClued_[player] = false;
discardClued_[player] = false;
}
bool ModBot::botCanPlay(Hanabi::GameMode mode)
{
if (mode==VERYDIFFICULT) return true;
return false;
}
int ModBot::bestCardToPlay(Server &server)
{
/* Try to find a card that nobody else knows I know is playable
* (because they don't see what I see). Let's try to get that card
* out of my hand before someone "helpfully" wastes a hint on it.
*/
CardKnowledge eyeKnol[5];
for (int i=0; i < myHandSize_; ++i) {
eyeKnol[i] = handKnowledge_[me_][i];
eyeKnol[i].update(server, *this, /*useMyEyesight=*/true);
}
int best_index = -1;
int best_fitness = 0;
for (int i=0; i < myHandSize_; ++i) {
if (eyeKnol[i].playable() != YES) continue;
/* How many further plays are enabled by this play?
* Rough heuristic: 5 minus its value. Notice that this
* gives an extra-high fitness to cards that are "known playable"
* but whose color/value is unknown (value() == -1).
* TODO: If both red 4s were discarded, then the red 3 doesn't open up any plays.
* TODO: Give higher value to cards if the card that follows it is known playable
* in someone else's hand
*/
int fitness = (6 - eyeKnol[i].value());
// If there are no clues, maybe better to play 5 to get a clue back
// TODO don't do this if it's the last round of the game and playing something else
// helps more.
if (fitness >= best_fitness) {
best_index = i;
best_fitness = fitness;
}
}
return best_index;
}
bool ModBot::maybePlayLowestPlayableCard(Server &server)
{
if (playClued_[me_]) {
server.pleasePlay(cluedIndex_[me_]);
return true;
}
// Don't play something I might find below if I've been forced to discard.
if (discardClued_[me_]) {
return false;
}
// If I find a card to play, play it
// This can happen when a player clues and a subsequent player plays making a card
// in this players hand playable, and this player knows enough about the card to play it.
int best_index = bestCardToPlay(server);
if (best_index != -1) {
server.pleasePlay(best_index);
return true;
}
return false;
}
bool ModBot::maybeDontDoubleDiscard(Server &server)
{
// check if the prior player discarded something that now might mean
// my discard (if any) is valuable. Don't discard if that is the case.
if (!priorPlayerDiscarded_) return false;
Card priorDiscard = Card(Color(priorDiscardColor_), priorDiscardValue_);
// is that card now valuable?
if (!isValuable(server, priorDiscard)) return false;
// Do I even have something marked for discard?
if (!discardClued_[me_]) return false;
// Do I know the card I would discard is NOT the same card as prior discard?
CardKnowledge eyeKnol;
eyeKnol = handKnowledge_[me_][cluedIndex_[me_]];
eyeKnol.update(server, *this, /*useMyEyesight=*/true);
if (eyeKnol.cannotBe(priorDiscard)) return false;
// At this point we know prior player discarded something valuable and
// I may have the same card marked for discard, so I will clue! (if possible)
return maybeGiveHelpfulHint(server, false);
}
int ModBot::handEval(const Server &server, int partner) const
{
if ((server.hintStonesRemaining() == 1) &&
(server.cardsRemainingInDeck() > 1)) {
//return discardPriorityHandEval(server, partner, false);
}
const std::vector<Card> partners_hand = server.handOfPlayer(partner);
int numPlayers = server.numPlayers();
// Find oldest unclued playable, if any.
for (int c=0; c < partners_hand.size() ; ++c) {
Card card = partners_hand[c];
if (!handKnowledge_[partner][c].clued()) {
if (server.pileOf(card.color).nextValueIs(card.value)) {
return c;
}
}
}
// Find oldest playable, if any.
for (int c=0; c < partners_hand.size() ; ++c) {
Card card = partners_hand[c];
if (server.pileOf(card.color).nextValueIs(card.value)) {
return c;
}
}
// No playables. :( Find oldest worthless card, if any
for (int c=0; c < partners_hand.size() ; ++c) {
if (isWorthless(server, partners_hand[c])) {
return (5 + c);
} else {
// Also check for duplicate cards in same hand
for (int cc=c + 1; cc < partners_hand.size(); ++cc) {
if (partners_hand[c] == partners_hand[cc]) {
return (5 + c);
}
}
}
}
Hanabi::Value bestValuableValue = ONE;
int bestValuableIndex = -1;
Hanabi::Value bestNonvaluableValue = ONE;
int bestNonvaluableIndex = -1;
// No playables. No worthless.
// Find oldest, nonvaluable 4
// else nonvaluable 3
// else nonvaluable 2
// else (getting ugly now) oldest 5
// else oldest 4
// else oldest 3
// else oldest 2 (Yikes! a hand full of non-playable, nonreplacable 2's!)
for (int c=0; c < partners_hand.size() ; ++c) {
if (isValuable(server, partners_hand[c])) {
if (partners_hand[c].value > bestValuableValue) {
bestValuableValue = partners_hand[c].value;
bestValuableIndex = c;
}
} else {
if (partners_hand[c].value > bestNonvaluableValue) {
bestNonvaluableValue = partners_hand[c].value;
bestNonvaluableIndex = c;
}
}
}
if (bestNonvaluableValue > ONE) return (5 + bestNonvaluableIndex);
return (10 + bestValuableIndex);
}
int ModBot::discardPriorityHandEval(const Server &server, int partner, bool excludePlay) const
{
const std::vector<Card> partners_hand = server.handOfPlayer(partner);
int numPlayers = server.numPlayers();
// Worthless is top priority for discard
for (int c=0; c < partners_hand.size() ; ++c) {
if (isWorthless(server, partners_hand[c])) {
return (5 + c);
} else {
// Also check for duplicate cards in same hand
for (int cc=c + 1; cc < partners_hand.size(); ++cc) {
if (partners_hand[c] == partners_hand[cc]) {
return (5 + c);
}
}
}
}
// No worthless.
// Find oldest, nonvaluable 4
// else nonvaluable 3
// else nonvaluable 2
// else playable (see below)
// else (getting ugly now) oldest 5
// else oldest 4
// else oldest 3
// else oldest 2 (Yikes! a hand full of non-playable, nonreplacable 2's!)
Hanabi::Value bestValuableValue = ONE;
int bestValuableIndex = -1;
Hanabi::Value bestNonvaluableValue = ONE;
int bestNonvaluableIndex = -1;
for (int c=0; c < partners_hand.size() ; ++c) {
if (isValuable(server, partners_hand[c])) {
if (partners_hand[c].value > bestValuableValue) {
bestValuableValue = partners_hand[c].value;
bestValuableIndex = c;
}
} else {
if (partners_hand[c].value > bestNonvaluableValue) {
bestNonvaluableValue = partners_hand[c].value;
bestNonvaluableIndex = c;
}
}
}
int playIndex = -1;
if (!excludePlay) {
// If already have card clued play, if it is still playable, return its index
if (playClued_[partner]) {
// already have a playable clued. Check if it is still playable
Card card = partners_hand[cluedIndex_[partner]];
if (server.pileOf(card.color).nextValueIs(card.value)) {
// Note: if this same card is/will be marked playable by previous player
// that will be handled in the calling routine
playIndex = cluedIndex_[partner];
} // else card is no longer playable!
}
// No clued play or it's not playable. Find oldest playable, if any.
for (int c=0; c < partners_hand.size() ; ++c) {
Card card = partners_hand[c];
if (server.pileOf(card.color).nextValueIs(card.value)) {
// Note: if this same card is/will be marked playable by previous player
// that will be handled in the calling routine
playIndex = c;
}
}
}
if (playIndex >= 0) return playIndex;
if (bestNonvaluableValue > ONE) return (5 + bestNonvaluableIndex);
return (10 + bestValuableIndex);
}
int ModBot::evaluateAllHands(const Server &server, int skipMe, int skipAlso, int *eval) const
{
int numPlayers = server.numPlayers();
int totalEval = 0;
//int eval[numPlayers];
bool haveSeenDiscard = false;
bool forceDiscard = false;
// evaluate each of my partners hands according to the eval rules (see eval section)
// Note that eval does not check for 2 players (or more) getting the same "play" clue.
// That is checked in the inner loop.
for (int i=1; i < numPlayers; i++) {
// We don't evaluate my own (clue giver's) hand
int p = (skipMe + i) % numPlayers;
if (p == skipAlso) continue;
if ((server.hintStonesRemaining() == 1) &&
(server.cardsRemainingInDeck() >= numPlayers) &&
(i==1)) {
eval[p] = discardPriorityHandEval(server, (skipMe + 1) % numPlayers, true);
} else {
eval[p] = handEval(server, p);
}
if (eval[p] < 5) {
// this player is getting a "play" clue
// check if card is same as a previous player
for (int ii=1; ii < i; ii++) {
int priorp = (skipMe + ii) % numPlayers;
if (priorp == skipAlso) continue;
if (eval[priorp] < 5) {
// if player p (who playes after priorp) would play same card as p
// increase eval. If dup is in hand of:
// LH2, increase by 15
// LH3, increase by 20
// LH4, increase by 25
if (server.handOfPlayer(priorp)[eval[priorp]] == server.handOfPlayer(p)[eval[p]]) {
eval[p] += (i*5) + 5;
break;
}
}
}
} else if (eval[p] >= 10) {
// This is a bad thing to discard. If an earlier player hasn't discarded yet, we may
// have to force LH1 to discard :(
//if (!haveSeenDiscard) forceDiscard = true;
} else {
haveSeenDiscard = true;
}
totalEval += eval[p];