-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathround_state.cpp
2864 lines (2650 loc) · 87.2 KB
/
round_state.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
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
#define PY_SSIZE_T_CLEAN
#include "simulation/round_state.hpp"
#include "simulation/shoupai.hpp"
#include "simulation/paishan.hpp"
#include "simulation/game_log.hpp"
#include "simulation/round_result.hpp"
#include "simulation/game_state.hpp"
#include "common/assert.hpp"
#include "common/throw.hpp"
#include <Python.h>
#include <algorithm>
#include <vector>
#include <array>
#include <functional>
#include <utility>
#include <stdexcept>
#include <limits>
#include <cstdint>
#include <cstddef>
namespace{
using std::placeholders::_1;
} // namespace `anonymous`
namespace Kanachan{
RoundState::RoundState(
std::vector<std::uint_least32_t> const &seed, Kanachan::GameState &game_state,
Kanachan::Paishan const *p_test_paishan)
: game_state_(game_state),
paishan_(p_test_paishan == nullptr ? Kanachan::Paishan(seed) : *p_test_paishan),
shoupai_list_({
Kanachan::Shoupai((4u - getJu()) % 4u, paishan_),
Kanachan::Shoupai((5u - getJu()) % 4u, paishan_),
Kanachan::Shoupai((6u - getJu()) % 4u, paishan_),
Kanachan::Shoupai((7u - getJu()) % 4u, paishan_)
}),
seat_(getJu()),
progression_()
{
for (std::uint_fast8_t i = 0u; i < 4u; ++i) {
initial_scores_[i] = game_state_.getPlayerScore(i);
}
progression_.push_back(0u);
}
std::uint_fast8_t RoundState::getPlayerGrade(std::uint_fast8_t const seat) const
{
KANACHAN_ASSERT((seat < 4u));
return game_state_.getPlayerGrade(seat);
}
std::uint_fast8_t RoundState::getChang() const
{
return game_state_.getChang();
}
std::uint_fast8_t RoundState::getJu() const
{
return game_state_.getJu();
}
std::uint_fast8_t RoundState::getBenChang() const
{
return game_state_.getBenChang();
}
std::uint_fast8_t RoundState::getNumLizhiDeposits() const
{
return game_state_.getNumLizhiDeposits();
}
std::int_fast32_t RoundState::getPlayerScore(std::uint_fast8_t const seat) const
{
KANACHAN_ASSERT((seat < 4u));
return game_state_.getPlayerScore(seat);
}
std::uint_fast8_t RoundState::getPlayerRanking(std::uint_fast8_t const seat) const
{
KANACHAN_ASSERT((seat < 4u));
return game_state_.getPlayerRanking(seat);
}
std::uint_fast8_t RoundState::getDoraIndicator(std::uint_fast8_t const index) const
{
KANACHAN_ASSERT((index < 5u));
if (index > gang_dora_count_) {
return std::numeric_limits<std::uint_fast8_t>::max();
}
// 牌山の王牌および嶺上牌は各幢の上下が逆になっている.
return paishan_[131u - 2u * index];
}
std::uint_fast8_t RoundState::getNumLeftTiles() const
{
KANACHAN_ASSERT((zimo_index_ >= 52u));
KANACHAN_ASSERT((122u >= zimo_index_ + lingshang_zimo_count_));
return 122u - zimo_index_ - lingshang_zimo_count_;
}
std::int_fast32_t RoundState::getPlayerDeltaScore(std::uint_fast8_t const seat) const
{
return game_state_.getPlayerScore(seat) - initial_scores_[seat];
}
bool RoundState::isPlayerMenqian(std::uint_fast8_t const seat) const
{
KANACHAN_ASSERT((seat < 4u));
Kanachan::Shoupai const &shoupai = shoupai_list_[seat];
return shoupai.isMenqian();
}
bool RoundState::isPlayerTingpai(std::uint_fast8_t const seat) const
{
KANACHAN_ASSERT((seat < 4u));
Kanachan::Shoupai const &shoupai = shoupai_list_[seat];
return shoupai.isTingpai();
}
bool RoundState::checkPlayerLiujuManguan(std::uint_fast8_t const seat) const
{
for (std::uint_fast16_t const encode : progression_) {
KANACHAN_ASSERT((encode <= 2164u));
if (encode == 0) {
continue;
}
if (5 <= encode && encode <= 596) {
std::uint_fast8_t const seat_ = (encode - 5) / 148;
if (seat_ != seat) {
continue;
}
std::uint_fast8_t const tile = (encode - 5 - seat_ * 148) / 4;
if (tile == 0 || 2 <= tile && tile <= 8) {
return false;
}
if (tile == 10 || 12 <= tile && tile <= 18) {
return false;
}
if (tile == 20 || 22 <= tile && tile <= 28) {
return false;
}
continue;
}
if (597 <= encode && encode <= 956) {
std::uint_fast8_t const seat_ = (encode - 597) / 90;
if (seat_ == (seat + 1) % 4) {
return false;
}
continue;
}
if (957 <= encode && encode <= 1436) {
std::uint_fast8_t const seat_ = (encode - 957) / 120;
std::uint_fast8_t const relseat = (encode - 957 - seat_ * 120) / 40;
if ((seat_ + relseat + 1) % 4 == seat) {
return false;
}
continue;
}
if (1437 <= encode && encode <= 1880) {
std::uint_fast8_t const seat_ = (encode - 1437) / 111;
std::uint_fast8_t const relseat = (encode - 1437 - seat_ * 111) / 37;
if ((seat_ + relseat + 1) % 4 == seat) {
return false;
}
continue;
}
}
return true;
}
bool RoundState::checkSifengLianda() const
{
if (progression_.size() != 5u) {
return false;
}
KANACHAN_ASSERT((progression_[0] == 0u));
std::array<std::uint_fast8_t, 4u> tiles = {
std::numeric_limits<std::uint_fast8_t>::max(),
std::numeric_limits<std::uint_fast8_t>::max(),
std::numeric_limits<std::uint_fast8_t>::max(),
std::numeric_limits<std::uint_fast8_t>::max()
};
for (std::uint_fast8_t i = 1u; i <= 4u; ++i) {
std::uint_fast16_t const encode = progression_[i];
KANACHAN_ASSERT((encode <= 2164u));
if (encode < 5u || 596u < encode) {
return false;
}
long const p = encode - 5u;
std::uint_fast8_t const seat = p / 148;
std::uint_fast8_t const tile = (p - seat * 148) / 4;
tiles[i - 1u] = tile;
}
for (std::uint_fast8_t i = 30u; i < 34u; ++i) {
bool const result = std::all_of(
tiles.cbegin(), tiles.cend(),
[i](std::uint_fast8_t const tile){ return tile == i; });
if (result) {
return true;
}
}
return false;
}
bool RoundState::checkSigangSanle() const
{
std::uint_fast8_t num_ganzi = 0u;
bool flag = false;
for (Kanachan::Shoupai const &s : shoupai_list_) {
std::uint_fast8_t const n = s.getNumGangzi();
if (num_ganzi >= 1u && n >= 1u) {
// 複数のプレイヤが槓をしている.
flag = true;
}
num_ganzi += n;
}
KANACHAN_ASSERT((num_ganzi <= 4u));
return num_ganzi == 4u && flag;
}
bool RoundState::checkSijiaLizhi() const
{
std::uint_fast8_t n = 0u;
for (std::uint_fast8_t lizhi : lizhi_list_) {
if (lizhi >= 1u) {
++n;
}
}
return n == 4u;
}
std::pair<std::uint_fast8_t, std::uint_fast8_t> RoundState::getLastDapai_() const
{
if (progression_.size() == 0u) {
KANACHAN_THROW<std::logic_error>("There is no discarded tile.");
}
std::uint_fast16_t const encode = progression_.back();
KANACHAN_ASSERT((encode >= 5u));
if (/*5u <= encode && */encode <= 596u) {
std::uint_fast8_t const seat = (encode - 5u) / 148u;
std::uint_fast8_t const tile = (encode - 5u - seat * 148u) / 4u;
return { seat, tile };
}
if (/*597u <= encode && */encode <= 1880u) {
KANACHAN_THROW<std::logic_error>(_1) << encode;
}
if (/*1881u <= encode && */encode <= 2016u) {
// 暗槓.槍槓の際には打牌とみなす.
std::uint_fast8_t const seat = (encode - 1881u) / 34u;
std::uint_fast8_t const tile = (encode - 1881u - seat * 34u);
if (0u <= tile && tile <= 8u) {
return { seat, tile + 1u };
}
if (9u <= tile && tile <= 17u) {
return { seat, tile + 2u };
}
if (18u <= tile && tile < 34u) {
return { seat, tile + 3u };
}
KANACHAN_THROW<std::logic_error>(_1) << tile;
}
if (/*2017u <= encode && */encode <= 2164u) {
// 加槓.槍槓の際には打牌とみなす.
std::uint_fast8_t const seat = (encode - 2017u) / 37u;
std::uint_fast8_t const tile = (encode - 2017u - seat * 37u);
return { seat, tile };
}
KANACHAN_THROW<std::logic_error>(_1) << encode;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
}
#pragma GCC diagnostic pop
std::uint_fast8_t RoundState::drawLingshangPai_()
{
KANACHAN_ASSERT((getNumLeftTiles() >= 1u));
KANACHAN_ASSERT((lingshang_zimo_count_ < 4u));
KANACHAN_ASSERT((gang_dora_count_ <= 4u));
KANACHAN_ASSERT((lizhi_delayed_ == 0u));
KANACHAN_ASSERT((!angang_dora_delayed_));
KANACHAN_ASSERT((!first_zimo_[0u]));
KANACHAN_ASSERT((!first_zimo_[1u]));
KANACHAN_ASSERT((!first_zimo_[2u]));
KANACHAN_ASSERT((!first_zimo_[3u]));
KANACHAN_ASSERT((!yifa_[0u]));
KANACHAN_ASSERT((!yifa_[1u]));
KANACHAN_ASSERT((!yifa_[2u]));
KANACHAN_ASSERT((!yifa_[3u]));
KANACHAN_ASSERT((lingshang_kaihua_delayed_));
KANACHAN_ASSERT((!qianggang_delayed_));
// 牌山の王牌および嶺上牌は各幢の上下が逆になっている.
std::uint_fast8_t const tile = paishan_[135 - lingshang_zimo_count_];
++lingshang_zimo_count_;
return tile;
}
std::pair<std::vector<std::uint_fast16_t>, std::vector<std::uint_fast32_t>>
RoundState::constructFeatures_(std::uint_fast8_t seat, std::uint_fast8_t const zimo_tile) const
{
std::vector<std::uint_fast16_t> sparse_features;
std::uint_fast16_t const room = game_state_.getRoom();
sparse_features.push_back(room);
std::uint_fast16_t const game_style = game_state_.isDongfengZhan() ? 5 : 6;
sparse_features.push_back(game_style);
sparse_features.push_back(7 + seat);
std::uint_fast16_t const chang = 11 + getChang();
sparse_features.push_back(chang);
std::uint_fast16_t const ju = 14 + getJu();
sparse_features.push_back(ju);
std::uint_fast16_t const dora_indicator = getDoraIndicator(0u);
KANACHAN_ASSERT((dora_indicator != std::numeric_limits<std::uint_fast8_t>::max()));
sparse_features.push_back(18 + dora_indicator);
for (std::uint_fast8_t i = 1u; i <= 4u; ++i) {
std::uint_fast16_t const gang_dora_indicator = getDoraIndicator(i);
if (gang_dora_indicator == std::numeric_limits<std::uint_fast8_t>::max()) {
break;
}
sparse_features.push_back(18 + i * 37 + gang_dora_indicator);
}
std::uint_fast16_t const num_left_tiles = getNumLeftTiles();
KANACHAN_ASSERT((0 <= num_left_tiles && num_left_tiles < 70));
sparse_features.push_back(203 + num_left_tiles);
for (std::uint_fast8_t i = 0u; i < 4u; ++i) {
std::uint_fast16_t const player_grade = getPlayerGrade((seat + i) % 4u);
sparse_features.push_back(273 + i * 20 + player_grade);
std::uint_fast16_t const player_ranking = getPlayerRanking((seat + i) % 4u);
sparse_features.push_back(289 + i * 20 + player_ranking);
}
Kanachan::Shoupai const &shoupai = shoupai_list_[seat];
shoupai.appendToFeatures(sparse_features);
if (zimo_tile != std::numeric_limits<std::uint_fast8_t>::max()) {
sparse_features.push_back(489 + zimo_tile);
}
std::vector<std::uint_fast32_t> numeric_features;
std::uint_fast32_t const ben_chang = getBenChang();
numeric_features.push_back(ben_chang);
std::uint_fast32_t const lizhi_deposits = getNumLizhiDeposits();
numeric_features.push_back(lizhi_deposits);
for (std::uint_fast8_t i = 0u; i < 4u; ++i) {
std::uint_fast32_t const player_score = getPlayerScore((seat + i) % 4u);
numeric_features.push_back(player_score);
}
return std::make_pair(std::move(sparse_features), std::move(numeric_features));
}
std::uint_fast16_t RoundState::selectAction_(
std::uint_fast8_t const seat, std::vector<std::uint_fast16_t> &&sparse,
std::vector<std::uint_fast32_t> &&numeric, std::vector<std::uint_fast16_t> &&progression,
std::vector<std::uint_fast16_t> &&candidates, Kanachan::GameLog &game_log) const
{
std::uint_fast16_t const action = game_state_.selectAction(
seat, std::vector(sparse), std::vector(numeric), std::vector(progression),
std::vector(candidates));
game_log.onDecision(
seat, std::move(sparse), std::move(numeric), std::move(progression),
std::move(candidates), action);
return action;
}
long RoundState::encodeToolConfig_(std::uint_fast8_t const seat, bool const rong) const
{
KANACHAN_ASSERT((seat < 4u));
long tool_config = 0u;
if (!rong) {
tool_config |= 1u << 0u; // Zi Mo (自摸)
}
if (lizhi_list_[seat] == 1u) {
tool_config |= 1u << 1u; // Li Zhi (立直)
}
if (yifa_[seat]) {
tool_config |= 1u << 2u; // Yi Fa (一発)
}
if (lingshang_kaihua_delayed_) {
KANACHAN_ASSERT((!rong));
tool_config |= 1u << 3u; // Ling Shang Kai Hua (嶺上開花)
}
if (qianggang_delayed_) {
KANACHAN_ASSERT((rong));
tool_config |= 1u << 4u; // Qiang Gang (槍槓)
}
if (getNumLeftTiles() == 0u && !rong && !lingshang_kaihua_delayed_) {
// 最終自摸牌が嶺上牌である場合は海底撈月は成立しない.
tool_config |= 1u << 5u; // Hai Di Lao Yue (海底撈月)
}
if (getNumLeftTiles() == 0u && rong) {
tool_config |= 1u << 6u; // He Di Lao Yu (河底撈魚)
}
if (lizhi_list_[seat] == 2u) {
tool_config |= 1u << 7u; // Double Li Zhi (ダブル立直)
}
if (!rong && first_zimo_[seat]) {
if (seat == getJu()) {
tool_config |= 1u << 8u; // Tian Hu (天和)
}
else {
tool_config |= 1u << 9u; // Di Hu (地和)
}
}
tool_config |= 1u << (10u + (seat + 4u - getJu()) % 4u); // Men Feng Pai (門風牌, 自風)
tool_config |= 1u << (14u + getChang()); // Quan Feng Pai (圏風牌, 場風)
return tool_config;
}
std::vector<std::uint_fast8_t>
RoundState::constructDoraIndicators_(std::uint_fast8_t const seat) const
{
KANACHAN_ASSERT((seat < 4u));
bool const lizhi = lizhi_list_[seat];
std::vector<std::uint_fast8_t> dora_indicators;
auto append = [&](std::uint_fast8_t const tile) -> void {
KANACHAN_ASSERT((tile < 37u));
if (tile == 0u) {
dora_indicators.push_back(16);
}
else if (1u <= tile && tile <= 9u) {
for (std::uint_fast8_t i = (tile == 5u ? 1u : 0u); i < 4u; ++i) {
std::uint_fast8_t const encode = (tile - 1u) * 4u + i;
std::size_t const count = std::count(
dora_indicators.cbegin(), dora_indicators.cend(), encode);
if (count >= 1u) {
KANACHAN_ASSERT((count == 1u));
KANACHAN_ASSERT((i != 3u));
continue;
}
dora_indicators.push_back(encode);
break;
}
}
else if (tile == 10u) {
dora_indicators.push_back(52);
}
else if (11u <= tile && tile <= 19u) {
for (std::uint_fast8_t i = (tile == 15u ? 1u : 0u); i < 4u; ++i) {
std::uint_fast8_t const encode = (tile - 2u) * 4u + i;
std::size_t const count = std::count(
dora_indicators.cbegin(), dora_indicators.cend(), encode);
if (count >= 1u) {
KANACHAN_ASSERT((count == 1u));
KANACHAN_ASSERT((i != 3u));
continue;
}
dora_indicators.push_back(encode);
break;
}
}
else if (tile == 20u) {
dora_indicators.push_back(88);
}
else if (21u <= tile && tile < 37u) {
for (std::uint_fast8_t i = (tile == 25u ? 1u : 0u); i < 4u; ++i) {
std::uint_fast8_t const encode = (tile - 3u) * 4u + i;
std::size_t const count = std::count(
dora_indicators.cbegin(), dora_indicators.cend(), encode);
if (count >= 1) {
KANACHAN_ASSERT((count == 1));
KANACHAN_ASSERT((i != 3u));
continue;
}
dora_indicators.push_back(encode);
break;
}
}
};
// 牌山の王牌および嶺上牌は各幢の上下が逆になっている.
for (std::uint_fast8_t i = 0u; i <= gang_dora_count_; ++i) {
std::uint_fast8_t const dora_indicator = paishan_[131u - i * 2];
append(dora_indicator);
if (lizhi) {
std::uint_fast8_t const lidora_indicator = paishan_[130u - i * 2];
append(lidora_indicator);
}
}
std::sort(dora_indicators.begin(), dora_indicators.end());
return dora_indicators;
}
std::pair<std::uint_fast8_t, std::uint_fast8_t> RoundState::checkDaSanyuanPao_() const
{
std::uint_fast8_t to = std::numeric_limits<std::uint_fast8_t>::max();
std::uint_fast8_t count = 0u;
for (std::uint_fast16_t const encode : progression_) {
if (/*0u <= encode && */encode <= 956u) {
continue;
}
if (/*957u <= encode && */encode <= 1436u) {
// ポン
std::uint_fast8_t const seat = (encode - 957u) / 120u;
std::uint_fast8_t const relseat = (encode - 957u - seat * 120u) / 40u;
std::uint_fast8_t const peng
= (encode - 957u - seat * 120u - relseat * 40u);
if (peng <= 36u) {
continue;
}
KANACHAN_ASSERT((peng <= 39u));
if (to == std::numeric_limits<std::uint_fast8_t>::max()) {
to = seat;
}
else if (to != seat) {
return {
std::numeric_limits<std::uint_fast8_t>::max(),
std::numeric_limits<std::uint_fast8_t>::max()
};
}
++count;
if (count == 3u) {
return { (seat + relseat + 1u) % 4u, to };
}
continue;
}
if (/*1437u <= encode && */encode <= 1880u) {
// 大明槓
std::uint_fast8_t const seat = (encode - 1437u) / 111u;
std::uint_fast8_t const relseat = (encode - 1437u - seat * 111u) / 37u;
std::uint_fast8_t const tile = (encode - 1437u - seat * 111u - relseat * 37u);
if (tile < 34u) {
continue;
}
KANACHAN_ASSERT((tile < 37u));
if (to == std::numeric_limits<std::uint_fast8_t>::max()) {
to = seat;
}
else if (to != seat) {
return {
std::numeric_limits<std::uint_fast8_t>::max(),
std::numeric_limits<std::uint_fast8_t>::max()
};
}
++count;
if (count == 3u) {
return { (seat + relseat + 1u) % 4u, to };
}
continue;
}
if (/*1881u <= encode && */encode <= 2016u) {
// 暗槓
std::uint_fast8_t const seat = (encode - 1881u) / 34u;
std::uint_fast8_t const tile = (encode - 1881u - seat * 34u);
if (tile < 31u) {
continue;
}
KANACHAN_ASSERT((tile < 34u));
if (to == std::numeric_limits<std::uint_fast8_t>::max()) {
to = seat;
}
else if (to != seat) {
return {
std::numeric_limits<std::uint_fast8_t>::max(),
std::numeric_limits<std::uint_fast8_t>::max()
};
}
++count;
continue;
}
if (2017u <= encode && encode <= 2164u) {
// 加槓
continue;
}
KANACHAN_THROW<std::logic_error>(_1) << encode << ": A logic error.";
}
KANACHAN_ASSERT((count < 3u));
return {
std::numeric_limits<std::uint_fast8_t>::max(),
std::numeric_limits<std::uint_fast8_t>::max()
};
}
std::pair<std::uint_fast8_t, std::uint_fast8_t> RoundState::checkDaSixiPao_() const
{
std::uint_fast8_t to = -1;
std::uint_fast8_t count = 0u;
for (std::uint_fast16_t const encode : progression_) {
if (/*0u <= encode && */encode <= 956u) {
continue;
}
if (/*957u <= encode && */encode <= 1436u) {
// ポン
std::uint_fast8_t const seat = (encode - 957u) / 120u;
std::uint_fast8_t const relseat = (encode - 957u - seat * 120u) / 40u;
std::uint_fast8_t const peng
= (encode - 957u - seat * 120u - relseat * 40u);
if (peng < 33u || 36 < peng) {
// 風牌以外のポン
continue;
}
if (to == static_cast<std::uint_fast8_t>(-1)) {
to = seat;
}
else if (to != seat) {
return {
static_cast<std::uint_fast8_t>(-1), static_cast<std::uint_fast8_t>(-1)
};
}
++count;
if (count == 4u) {
return { (seat + relseat + 1u) % 4u, to };
}
continue;
}
if (/*1437u <= encode && */encode <= 1880u) {
// 大明槓
std::uint_fast8_t const seat = (encode - 1437u) / 111u;
std::uint_fast8_t const relseat = (encode - 1437u - seat * 111u) / 37u;
std::uint_fast8_t const tile = (encode - 1437u - seat * 111u - relseat * 37u);
if (tile < 30u || 33u < tile) {
// 風牌以外の大明槓.
continue;
}
if (to == static_cast<std::uint_fast8_t>(-1)) {
to = seat;
}
else if (to != seat) {
return {
static_cast<std::uint_fast8_t>(-1), static_cast<std::uint_fast8_t>(-1)
};
}
++count;
if (count == 4u) {
return { (seat + relseat + 1u) % 4u, to };
}
continue;
}
if (/*1881u <= encode && */encode <= 2016u) {
// 暗槓
std::uint_fast8_t const seat = (encode - 1881u) / 34u;
std::uint_fast8_t const tile = (encode - 1881u - seat * 34u);
if (tile < 27u || 30u < tile) {
// 風牌以外の暗槓.
continue;
}
if (to == static_cast<std::uint_fast8_t>(-1)) {
to = seat;
}
else if (to != seat) {
return {
static_cast<std::uint_fast8_t>(-1), static_cast<std::uint_fast8_t>(-1)
};
}
++count;
continue;
}
if (2017u <= encode && encode <= 2164u) {
// 加槓
continue;
}
KANACHAN_THROW<std::logic_error>(_1) << encode << ": A logic error.";
}
KANACHAN_ASSERT((count < 3u));
return {
static_cast<std::uint_fast8_t>(-1), static_cast<std::uint_fast8_t>(-1)
};
}
std::pair<std::uint_fast8_t, std::uint_fast8_t> RoundState::calculateHand_(
std::uint_fast8_t const seat, std::uint_fast8_t const hupai, long const config) const
{
KANACHAN_ASSERT((seat < 4u));
KANACHAN_ASSERT((config >= 0));
Kanachan::Shoupai const &shoupai = shoupai_list_[seat];
std::vector<std::uint_fast8_t> dora_indicators = constructDoraIndicators_(seat);
return shoupai.calculateHand(hupai, dora_indicators, config);
}
void RoundState::settleLizhiDeposits_()
{
std::uint_fast8_t seat = std::numeric_limits<std::uint_fast8_t>::max();
for (std::uint_fast8_t i = 0u; i < 4u; ++i) {
if (getPlayerRanking(i) == 0u) {
seat = i;
break;
}
}
KANACHAN_ASSERT((seat < 4u));
game_state_.addPlayerScore(seat, 1000 * getNumLizhiDeposits());
}
std::pair<std::uint_fast16_t, std::uint_fast8_t> RoundState::onZimo(Kanachan::GameLog &game_log)
{
KANACHAN_ASSERT((getNumLeftTiles() >= 1u));
KANACHAN_ASSERT((lingshang_zimo_count_ <= 4u));
KANACHAN_ASSERT((gang_dora_count_ <= 4u));
KANACHAN_ASSERT((seat_ < 4u));
KANACHAN_ASSERT((lizhi_delayed_ == 0u));
KANACHAN_ASSERT((!qianggang_delayed_));
for (std::uint_fast8_t i = 0u; i < 4u; ++i) {
KANACHAN_ASSERT((!rong_delayed_[i]));
}
std::uint_fast8_t zimo_tile = std::numeric_limits<std::uint_fast8_t>::max();
if (angang_dora_delayed_) {
// 暗槓直後の嶺上自摸
KANACHAN_ASSERT((lingshang_zimo_count_ < 4u));
KANACHAN_ASSERT((gang_dora_count_ < 4u));
KANACHAN_ASSERT((!minggang_dora_delayed_));
for (std::uint_fast8_t i = 0u; i < 4u; ++i) {
KANACHAN_ASSERT((!first_zimo_[i]));
KANACHAN_ASSERT((!yifa_[i]));
}
KANACHAN_ASSERT((lingshang_kaihua_delayed_));
++gang_dora_count_;
angang_dora_delayed_ = false;
zimo_tile = drawLingshangPai_();
}
else if (minggang_dora_delayed_) {
// 明槓直後の嶺上自摸
KANACHAN_ASSERT((lingshang_zimo_count_ < 4u));
KANACHAN_ASSERT((gang_dora_count_ < 4u));
KANACHAN_ASSERT((!angang_dora_delayed_));
for (std::uint_fast8_t i = 0u; i < 4u; ++i) {
KANACHAN_ASSERT((!first_zimo_[i]));
KANACHAN_ASSERT((!yifa_[i]));
}
KANACHAN_ASSERT((lingshang_kaihua_delayed_));
zimo_tile = drawLingshangPai_();
}
else {
KANACHAN_ASSERT((!angang_dora_delayed_));
KANACHAN_ASSERT((!minggang_dora_delayed_));
zimo_tile = paishan_[zimo_index_++];
}
KANACHAN_ASSERT((zimo_tile != UINT_FAST8_MAX));
Kanachan::Shoupai &shoupai = shoupai_list_[seat_];
std::uint_fast16_t const action = [&]() {
auto [sparse, numeric] = constructFeatures_(seat_, zimo_tile);
bool const first_zimo = first_zimo_[seat_];
bool const haidi = (getNumLeftTiles() == 0u);
bool const lizhi_prohibited = (getPlayerScore(seat_) < 1000 || getNumLeftTiles() < 4u);
bool const gang_prohibited = (lingshang_zimo_count_ == 4u || haidi);
long const tool_config = encodeToolConfig_(seat_, /*rong = */false);
std::vector<std::uint_fast16_t> candidates = shoupai.getCandidatesOnZimo(
zimo_tile, first_zimo, lizhi_prohibited, gang_prohibited, tool_config);
if (candidates.size() >= 1) {
KANACHAN_ASSERT((candidates.size() >= 2));
return selectAction_(
seat_, std::move(sparse), std::move(numeric), std::vector(progression_),
std::move(candidates), game_log);
}
return std::numeric_limits<std::uint_fast16_t>::max();
}();
std::uint_fast8_t tile = UINT_FAST8_MAX;
if (action <= 147u) {
// Da Pai (打牌)
tile = action / 4u;
bool const moqi = ((action - tile * 4u) / 2u == 1u);
bool const lizhi = (action - tile * 4u - moqi * 2u == 1u);
KANACHAN_ASSERT((!moqi || tile == zimo_tile));
if (lizhi) {
if (first_zimo_[seat_]) {
lizhi_delayed_ = 2u;
}
else {
lizhi_delayed_ = 1u;
}
yifa_[seat_] = true;
}
else {
yifa_[seat_] = false;
}
first_zimo_[seat_] = false;
lingshang_kaihua_delayed_ = false;
shoupai.onPostZimo(zimo_tile, tile, lizhi_list_[seat_] >= 1u);
tile = zimo_tile;
}
if (action == std::numeric_limits<std::uint_fast16_t>::max()) {
// Da Pai after Li Zhi (立直後の打牌)
KANACHAN_ASSERT((lizhi_list_[seat_] >= 1u));
KANACHAN_ASSERT((!first_zimo_[seat_]));
yifa_[seat_] = false;
lingshang_kaihua_delayed_ = false;
tile = zimo_tile;
}
if (148u <= action && action <= 181u) {
// An Gang (暗槓)
KANACHAN_ASSERT((lizhi_delayed_ == 0u));
// 一発の消失は槓の成立後.つまり槍槓により槓が不成立の場合,
// 一発は消失せず,槍槓と複合しうる.ただし,暗槓に対する槍槓は
// 国士無双に限られるため,一発の消失のタイミングが問題になることは無い.
// 第一自摸の判定も同様だが,こちらもタイミングが問題になることは無い.
lingshang_kaihua_delayed_ = false;
tile = zimo_tile;
}
if (182u <= action && action <= 218u) {
// Jiang Gang (加槓)
KANACHAN_ASSERT((lizhi_delayed_ == 0u));
// 一発の消失は槓の成立後.つまり槍槓により槓が不成立の場合,
// 一発は消失せず,槍槓と複合しうる.第一自摸の判定も同様だが,
// こちらはタイミングが問題になることは無い.
lingshang_kaihua_delayed_ = false;
tile = zimo_tile;
}
if (action == 219u) {
// Zi Mo Hu (自摸和)
KANACHAN_ASSERT((lizhi_delayed_ == 0u));
KANACHAN_ASSERT((!angang_dora_delayed_));
tile = zimo_tile;
}
if (action == 220u) {
// Jiu Zhong Jiu Pai (九種九牌)
KANACHAN_ASSERT((lizhi_delayed_ == 0u));
KANACHAN_ASSERT((!angang_dora_delayed_));
KANACHAN_ASSERT((!minggang_dora_delayed_));
KANACHAN_ASSERT((first_zimo_[seat_]));
KANACHAN_ASSERT((!yifa_[seat_]));
KANACHAN_ASSERT((!lingshang_kaihua_delayed_));
}
if (221u <= action && action != UINT_FAST16_MAX) {
KANACHAN_THROW<std::runtime_error>(_1) << action << ": An invalid action on zimo.";
}
return { action, tile };
}
std::pair<std::uint_fast8_t, std::uint_fast16_t> RoundState::onDapai(
std::uint_fast8_t const tile, bool const moqi, bool const lizhi, Kanachan::GameLog &game_log)
{
KANACHAN_ASSERT((zimo_index_ <= 122u - lingshang_zimo_count_));
KANACHAN_ASSERT((lingshang_zimo_count_ <= 4u));
KANACHAN_ASSERT((gang_dora_count_ <= 4u));
KANACHAN_ASSERT((seat_ < 4u));
KANACHAN_ASSERT((!angang_dora_delayed_));
KANACHAN_ASSERT((!lingshang_kaihua_delayed_));
KANACHAN_ASSERT((!qianggang_delayed_));
KANACHAN_ASSERT((!rong_delayed_[0u]));
KANACHAN_ASSERT((!rong_delayed_[1u]));
KANACHAN_ASSERT((!rong_delayed_[2u]));
KANACHAN_ASSERT((!rong_delayed_[3u]));
if (minggang_dora_delayed_) {
KANACHAN_ASSERT((lingshang_zimo_count_ == gang_dora_count_ + 1u));
KANACHAN_ASSERT((lizhi_delayed_ == 0u));
KANACHAN_ASSERT((!first_zimo_[0u]));
KANACHAN_ASSERT((!first_zimo_[1u]));
KANACHAN_ASSERT((!first_zimo_[2u]));
KANACHAN_ASSERT((!first_zimo_[3u]));
KANACHAN_ASSERT((!yifa_[0u]));
KANACHAN_ASSERT((!yifa_[1u]));
KANACHAN_ASSERT((!yifa_[2u]));
KANACHAN_ASSERT((!yifa_[3u]));
++gang_dora_count_;
}
minggang_dora_delayed_ = false;
std::uint_fast8_t const moqi_ = moqi ? 1u : 0u;
std::uint_fast8_t const lizhi_ = lizhi ? 1u : 0u;
if (lizhi) {
KANACHAN_ASSERT((lizhi_delayed_ >= 1u));
}
progression_.push_back(5 + seat_ * 148 + tile * 4u + moqi_ * 2u + lizhi_);
std::array<std::uint_fast16_t, 3u> actions = {
std::numeric_limits<std::uint_fast16_t>::max(),
std::numeric_limits<std::uint_fast16_t>::max(),
std::numeric_limits<std::uint_fast16_t>::max()
};
for (std::uint_fast8_t i = 0; i < 3u; ++i) {
std::uint_fast8_t const calling_seat = (seat_ + i + 1) % 4u;
std::uint_fast8_t const zimo_tile = -1;
auto [sparse, numeric] = constructFeatures_(calling_seat, zimo_tile);
Kanachan::Shoupai const &shoupai = shoupai_list_[calling_seat];
long const tool_config = encodeToolConfig_(calling_seat, /*rong = */true);
std::vector<std::uint_fast16_t> candidates = shoupai.getCandidatesOnDapai(
2u - i, tile, lingshang_zimo_count_ == 4u, tool_config);
KANACHAN_ASSERT((candidates.size() != 1));
if (checkSigangSanle()) {
// 4つ目の槓に対する打牌の場合,その打牌に対する他家の選択肢から
// スキップと栄和以外を除外しなければならない.
std::vector<std::uint_fast16_t> new_candidates;
for (std::uint_fast16_t const candidate : candidates) {
if (candidate == 221u || (543u <= candidate && candidate <= 545u)) {
new_candidates.push_back(candidate);
}
}
candidates.swap(new_candidates);
}
if (candidates.size() == 1u) {
KANACHAN_ASSERT((candidates[0u] == 221u));
continue;
}
if (candidates.size() == 0u) {
continue;
}
try {
std::uint_fast16_t const action = selectAction_(
calling_seat, std::move(sparse), std::move(numeric), std::vector(progression_),
std::move(candidates), game_log);
actions[i] = action;
}
catch (std::runtime_error const &) {
if (checkSigangSanle()) {
// TestModel を使用したテストを行っている場合で,四槓散了が成立する時,
// 直前の `selectAction_` で次局の親の打牌の選択肢を読んでしまう.
// これにより `selectAction_` から例外が送出されるが,テストとしては
// 正常な動作であるため,実行を続けなければならない.
break;
}
throw;
}
}
for (;;) {
// 栄和が選択されたかどうかをチェックする.
std::uint_fast8_t i = 0u;
for (; i < 3u; ++i) {
if (543u <= actions[i] && actions[i] <= 545u) {
break;
}
}
if (i == 3u) {
break;
}
// 栄和が選択された.
for (std::uint_fast8_t j = 0u; j < 3u; ++j) {
if (actions[j] == 221) {
continue;
}
if (222u <= actions[j] && actions[j] <= 311u) {
// Chi that is being canceled by rong.
continue;
}
if (312u <= actions[j] && actions[j] <= 431u) {
// Peng that is being canceled by rong.
continue;
}
if (432u <= actions[j] && actions[j] <= 542u) {
// Da ming gang that is being canceled by rong.
continue;
}
if (543u <= actions[j] && actions[j] <= 545u) {
std::uint_fast8_t const calling_seat = (seat_ + j + 1u) % 4u;
rong_delayed_[calling_seat] = true;
continue;
}
if (actions[j] == std::numeric_limits<std::uint_fast16_t>::max()) {
continue;
}
KANACHAN_THROW<std::runtime_error>(_1)
<< static_cast<unsigned>(j) << ": " << actions[j]
<< ": An invalid rong action.";
}
seat_ = std::numeric_limits<std::uint_fast8_t>::max();
return { /*seat = */std::numeric_limits<std::uint_fast8_t>::max(), 543u };
}
if (checkSigangSanle()) {