-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmayhem.hpp
3127 lines (2699 loc) · 102 KB
/
mayhem.hpp
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
/*
Mayhem. Linux UCI Chess960 engine. Written in C++20 language
Copyright (C) 2020-2024 Toni Helminen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Header guard
#pragma once
// Headers
#include <bits/stdc++.h>
#include "nnue.hpp"
#include "polyglotbook.hpp"
#include "eucalyptus.hpp"
extern "C" {
#ifdef WINDOWS
#include <conio.h>
#endif
}
// Namespace
namespace mayhem {
// Constants
const std::string VERSION = "Mayhem 8.7"; // Version
const std::string STARTPOS = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; // UCI startpos
const std::string EVAL_FILE = "nn-cb80fb9393af.nnue"; // Default NNUE evaluation file
const std::string BOOK_FILE = "final-book.bin"; // Default Polyglot book file
constexpr int MAX_MOVES = 256; // Max chess moves
constexpr int MAX_SEARCH_DEPTH = 64; // Max search depth (Stack frame problems ...)
constexpr int MAX_Q_SEARCH_DEPTH = 16; // Max Qsearch depth
constexpr int INF = 1048576; // System max number
constexpr int DEF_HASH_MB = 256; // MiB
constexpr int NOISE = 2; // Noise for opening moves
constexpr int MOVEOVERHEAD = 100; // ms
constexpr int REPS_DRAW = 3; // 3rd repetition is a draw
constexpr int FIFTY = 100; // 100 moves w/o progress is a draw (256 max)
constexpr int R50_ARR = (FIFTY + 2); // Checkmate overrules 50 move rep so extra space here
constexpr int SHUFFLE = 30; // Allow shuffling then scale
constexpr int BOOK_MS = 100; // At least 100ms+ for the book lookup
constexpr int LEVEL = 100; // Level of engine. ( 0: Random, 1-99: Levels, 100: Full)
constexpr int NOISE_PAWNS = 5; // Add noise to eval for different playing levels ( -5 -> +5 pawns )
constexpr int PERFT_DEPTH = 6; // Perft at depth 6
constexpr int BENCH_DEPTH = 14; // Bench at depth 14
constexpr int BENCH_SPEED = 10000; // Bench for 10s
constexpr int WEEK = (7 * 24 * 60 * 60 * 1000); // ms
constexpr int MAX_PIECES = (2 * (8 * 1 + 2 * 3 + 2 * 3 + 2 * 5 + 1 * 9 + 1 * 0)); // Max pieces on board (Kings always exist)
constexpr int FRC_PENALTY = 100; // Penalty for bishop blocked on corner by own pawn
constexpr int TEMPO_BONUS = 25; // Bonus for the side to move
constexpr int BISHOP_PAIR_BONUS = 20; // Both colored bishops bonus
constexpr int CHECKS_BONUS = 17; // Bonus for checks
constexpr bool BOOK_BEST = false; // Nondeterministic opening play
constexpr std::uint64_t READ_CLOCK = 0x1FFULL; // Read clock every 512 ticks (white / 2 x both)
// Use NNUE evaluation (From make) ?
#ifdef MAYHEMNNUE
constexpr bool USE_NNUE = true;
#else
constexpr bool USE_NNUE = false;
#endif
// Use Polyglot book ?
#ifdef MAYHEMBOOK
constexpr bool USE_BOOK = true;
#else
constexpr bool USE_BOOK = false;
#endif
// Constants
// Tactical fens to pressure search
const std::vector<std::string> kBench = {
"r2q2k1/pQ2bppp/4p3/8/3r1B2/6P1/P3PP1P/1R3RK1 w - - 0 1 ; bm f4b8",
"q5k1/5pp1/8/1pb1P3/2p4p/2P2r1P/1P3PQ1/1N3R1K b - - 0 1 ; bm f3h3",
"3r2k1/1pp4p/p1n1q1p1/2Q5/1P2B3/P3P1Pb/3N1R1P/6K1 b - - 1 1 ; bm e6e4",
"8/8/8/4p3/4k3/8/8/4K3 w - - 0 1 ; bm e1e2",
"2n3k1/P5P1/6K1/8/8/8/8/8 w - - 0 1 ; bm a7a8b",
"7K/P1p1p1p1/2P1P1Pk/6pP/3p2P1/1P6/3P4/8 w - - 0 1 ; bm a7a8n",
"5K2/k7/4P1p1/8/8/8/4b3/8 w - - 0 1 ; bm f8e7",
"8/k7/P2b2P1/KP1Pn2P/4R3/8/6np/8 w - - 0 1 ; bm b5b6",
"7k/5K2/7P/6pP/8/8/8/8 w - - 0 1 ; bm f7f6",
"8/6PK/5k2/8/8/8/8/6r1 w - - 0 1 ; bm g7g8n",
"r7/2k1Pp1p/p1n2p2/P1b1r3/2p5/2P3P1/5P1P/1R1Q2K1 w - - 0 1 ; bm b1b7",
"3r2k1/pp5p/6p1/2Ppq3/4Nr2/4B2b/PP2P2K/R1Q1R2B b - - 0 1 ; bm f4f2",
"2r3k1/q5pp/4p3/2rp1p2/1p1B1P2/1P1QP3/P1R3PP/6K1 w - - 0 1 ; bm d3b5",
"8/8/8/8/8/k5R1/2rn4/K7 b - - 55 94 ; bm d2b3",
"8/k7/p1p5/2P1p3/1P2B3/P3P3/3K2p1/6n1 b - - 0 1 ; bm g1f3",
"1q3r1k/r4ppp/5n2/8/3Q1N2/p6R/PPP5/1K5R w - - 0 1 ; bm d4f6",
"8/r2n1p2/1r1N1Pk1/3pP1p1/1p4P1/qPp2K2/P1R4R/8 w - - 0 1 ; bm h2h6",
"rnb2rk1/pp1nqppp/4p3/3pP3/3p3P/2NB3N/PPP2PP1/R2QK2R w KQ - 0 1 ; bm d3h7",
"3r4/k1P5/P7/1K6/8/8/8/8 w - - 0 1 ; bm c7d8n",
"8/4R2n/4K1pk/6p1/7P/8/8/8 w - - 0 1 ; bm e7h7",
"R7/P4k2/8/8/8/8/r7/6K1 w - - 0 1 ; bm a8h8",
"2kr3r/pp1q1ppp/5n2/1Nb5/2Pp1B2/7Q/P4PPP/1R3RK1 w - - 0 1 ; bm b5a7",
"2R5/2R4p/5p1k/6n1/8/1P2QPPq/r7/6K1 w - - 0 1 ; bm c7h7",
"5r1k/1b4p1/p6p/4Pp1q/2pNnP2/7N/PPQ3PP/5R1K b - - 0 1 ; bm h5h3",
"6k1/3r4/2R5/P5P1/1P4p1/8/4rB2/6K1 b - - 0 1 ; bm g4g3",
"5n2/pRrk2p1/P4p1p/4p3/3N4/5P2/6PP/6K1 w - - 0 1 ; bm d4b5",
"8/6pp/4p3/1p1n4/1NbkN1P1/P4P1P/1PR3K1/r7 w - - 0 1 ; bm c2c4",
"2r5/2rk2pp/1pn1pb2/pN1p4/P2P4/1N2B3/nPR1KPPP/3R4 b - - 0 1 ; bm c6d4",
"nrq4r/2k1p3/1p1pPnp1/pRpP1p2/P1P2P2/2P1BB2/1R2Q1P1/6K1 w - - 0 1 ; bm e3c5",
"3r2k1/5p2/6p1/4b3/1P2P3/1R2P2p/P1K1N3/8 b - - 0 1 ; bm d8d1",
"1k1r4/pp1r1pp1/4n1p1/2R5/2Pp1qP1/3P2QP/P4PB1/1R4K1 w - - 0 1 ; bm g2b7",
"2r1k3/6pr/p1nBP3/1p3p1p/2q5/2P5/P1R4P/K2Q2R1 w - - 0 1 ; bm g1g7",
"2b4k/p1b2p2/2p2q2/3p1PNp/3P2R1/3B4/P1Q2PKP/4r3 w - - 0 1 ; bm c2c6",
"5bk1/1rQ4p/5pp1/2pP4/3n1PP1/7P/1q3BB1/4R1K1 w - - 0 1 ; bm d5d6",
"rnbqkb1r/pppp1ppp/8/4P3/6n1/7P/PPPNPPP1/R1BQKBNR b KQkq - 0 1 ; bm g4e3"
};
// [Attacker][Captured] / [PNBRQK][pnbrqk]
constexpr int kMvv[6][6] = {
{ 10, 15, 15, 20, 25, 99 }, { 9, 14, 14, 19, 24, 99 }, { 9, 14, 14, 19, 24, 99 },
{ 8, 13, 13, 18, 23, 99 }, { 7, 12, 12, 17, 22, 99 }, { 6, 11, 11, 16, 21, 99 }
};
// Evaluation phases ( P N B R Q K )
constexpr int kPiece[6] = { 1, 3, 3, 5, 9, 0 }; // Must match MAX_PIECES !
// ( MG EG ) -> ( P N B R Q K )
constexpr int kPestoMaterial[2][6] = {
{ 82, 337, 365, 477, 1025, 0 },
{ 94, 281, 297, 512, 936, 0 }
};
// [Piece][Phase][Square]
constexpr int kPestoPsqt[6][2][64] = {
{{ -55, -54, -53, -52, -52, -53, -54, -55, // Pawn (MG)
-35, -1, -20, -23, -15, 24, 38, -22,
-26, -4, -4, -10, 3, 3, 33, -12,
-27, -2, -5, 12, 17, 6, 10, -25,
-14, 13, 6, 21, 23, 12, 17, -23,
-6, 7, 26, 31, 65, 56, 25, -20,
98, 134, 61, 95, 68, 126, 34, -11,
0, 0, 0, 0, 0, 0, 0, 0 },
{ -55, -54, -53, -52, -52, -53, -54, -55, // Pawn (EG)
13, 8, 8, 10, 13, 0, 2, -7,
4, 7, -6, 1, 0, -5, -1, -8,
13, 9, -3, -7, -7, -8, 3, -1,
32, 24, 13, 5, -2, 4, 17, 17,
94, 100, 85, 67, 56, 53, 82, 84,
178, 173, 158, 134, 147, 132, 165, 187,
0, 0, 0, 0, 0, 0, 0, 0 }},
{{-105, -21, -58, -33, -17, -28, -19, -23, // Knight (MG)
-29, -53, -12, -3, -1, 18, -14, -19,
-23, -9, 12, 10, 19, 17, 25, -16,
-13, 4, 16, 13, 28, 19, 21, -8,
-9, 17, 19, 53, 37, 69, 18, 22,
-47, 60, 37, 65, 84, 129, 73, 44,
-73, -41, 72, 36, 23, 62, 7, -17,
-167, -89, -34, -49, 61, -97, -15,-107 },
{ -29, -51, -23, -15, -22, -18, -50, -64, // Knight (EG)
-42, -20, -10, -5, -2, -20, -23, -44,
-23, -3, -1, 15, 10, -3, -20, -22,
-18, -6, 16, 25, 16, 17, 4, -18,
-17, 3, 22, 22, 22, 11, 8, -18,
-24, -20, 10, 9, -1, -9, -19, -41,
-25, -8, -25, -2, -9, -25, -24, -52,
-58, -38, -13, -28, -31, -27, -63, -99 }},
{{ -33, -3, -14, -21, -13, -12, -39, -21, // Bishop (MG)
4, 15, 16, 0, 7, 21, 33, 1,
0, 15, 15, 15, 14, 27, 18, 10,
-6, 13, 13, 26, 34, 12, 10, 4,
-4, 5, 19, 50, 37, 37, 7, -2,
-16, 37, 43, 40, 35, 50, 37, -2,
-26, 16, -18, -13, 30, 59, 18, -47,
-29, 4, -82, -37, -25, -42, 7, -8 },
{ -23, -9, -23, -5, -9, -16, -5, -17, // Bishop (EG)
-14, -18, -7, -1, 4, -9, -15, -27,
-12, -3, 8, 10, 13, 3, -7, -15,
-6, 3, 13, 19, 7, 10, -3, -9,
-3, 9, 12, 9, 14, 10, 3, 2,
2, -8, 0, -1, -2, 6, 0, 4,
-8, -4, 7, -12, -3, -13, -4, -14,
-14, -21, -11, -8, -7, -9, -17, -24 }},
{{ -19, -13, 1, 17, 16, 7, -37, -26, // Rook (MG)
-44, -16, -20, -9, -1, 11, -6, -71,
-45, -25, -16, -17, 3, 0, -5, -33,
-36, -26, -12, -1, 9, -7, 6, -23,
-24, -11, 7, 26, 24, 35, -8, -20,
-5, 19, 26, 36, 17, 45, 61, 16,
27, 32, 58, 62, 80, 67, 26, 44,
32, 42, 32, 51, 63, 9, 31, 43 },
{ -9, 2, 3, -1, -5, -13, 4, -20, // Rook (EG)
-6, -6, 0, 2, -9, -9, -11, -3,
-4, 0, -5, -1, -7, -12, -8, -16,
3, 5, 8, 4, -5, -6, -8, -11,
4, 3, 13, 1, 2, 1, -1, 2,
7, 7, 7, 5, 4, -3, -5, -3,
11, 13, 13, 11, -3, 3, 8, 3,
13, 10, 18, 15, 12, 12, 8, 5 }},
{{ -1, -18, -9, 10, -15, -25, -31, -50, // Queen (MG)
-35, -8, 11, 2, 8, 15, -3, 1,
-14, 2, -11, -2, -5, 2, 14, 5,
-9, -26, -9, -10, -2, -4, 3, -3,
-27, -27, -16, -16, -1, 17, -2, 1,
-13, -17, 7, 8, 29, 56, 47, 57,
-24, -39, -5, 1, -16, 57, 28, 54,
-28, 0, 29, 12, 59, 44, 43, 45 },
{ -33, -28, -22, -43, -5, -32, -20, -41, // Queen (EG)
-22, -23, -30, -16, -16, -23, -36, -32,
-16, -27, 15, 6, 9, 17, 10, 5,
-18, 28, 19, 47, 31, 34, 39, 23,
3, 22, 24, 45, 57, 40, 57, 36,
-20, 6, 9, 49, 47, 35, 19, 9,
-17, 20, 32, 41, 58, 25, 30, 0,
-9, 22, 22, 27, 27, 19, 10, 20 }},
{{ -15, 36, 12, -54, 8, -28, 24, 14, // King (MG)
1, 7, -8, -64, -43, -16, 9, 8,
-14, -14, -22, -46, -44, -30, -15, -27,
-49, -1, -27, -39, -46, -44, -33, -51,
-17, -20, -12, -27, -30, -25, -14, -36,
-9, 24, 2, -16, -20, 6, 22, -22,
29, -1, -20, -7, -8, -4, -38, -29,
-65, 23, 16, -15, -56, -34, 2, 13 },
{ -53, -34, -21, -11, -28, -14, -24, -43, // King (EG)
-27, -11, 4, 13, 14, 4, -5, -17,
-19, -3, 11, 21, 23, 16, 7, -9,
-18, -4, 21, 24, 27, 23, 9, -11,
-8, 22, 24, 27, 26, 33, 26, 3,
10, 17, 23, 15, 20, 45, 44, 13,
-12, 17, 14, 17, 17, 38, 23, 11,
-74, -35, -18, -18, -11, 15, 4, -17 }}
};
constexpr std::uint64_t kRookMagics[3][64] = {
{ 0x548001400080106cULL, 0x900184000110820ULL, 0x428004200a81080ULL, 0x140088082000c40ULL, // Magics
0x1480020800011400ULL, 0x100008804085201ULL, 0x2a40220001048140ULL, 0x50000810000482aULL,
0x250020100020a004ULL, 0x3101880100900a00ULL, 0x200a040a00082002ULL, 0x1004300044032084ULL,
0x2100408001013ULL, 0x21f00440122083ULL, 0xa204280406023040ULL, 0x2241801020800041ULL,
0xe10100800208004ULL, 0x2010401410080ULL, 0x181482000208805ULL, 0x4080101000021c00ULL,
0xa250210012080022ULL, 0x4210641044000827ULL, 0x8081a02300d4010ULL, 0x8008012000410001ULL,
0x28c0822120108100ULL, 0x500160020aa005ULL, 0xc11050088c1000ULL, 0x48c00101000a288ULL,
0x494a184408028200ULL, 0x20880100240006ULL, 0x10b4010200081ULL, 0x40a200260000490cULL,
0x22384003800050ULL, 0x7102001a008010ULL, 0x80020c8010900c0ULL, 0x100204082a001060ULL,
0x8000118188800428ULL, 0x58e0020009140244ULL, 0x100145040040188dULL, 0x44120220400980ULL,
0x114001007a00800ULL, 0x80a0100516304000ULL, 0x7200301488001000ULL, 0x1000151040808018ULL,
0x3000a200010e0020ULL, 0x1000849180802810ULL, 0x829100210208080ULL, 0x1004050021528004ULL,
0x61482000c41820b0ULL, 0x241001018a401a4ULL, 0x45020c009cc04040ULL, 0x308210c020081200ULL,
0xa000215040040ULL, 0x10a6024001928700ULL, 0x42c204800c804408ULL, 0x30441a28614200ULL,
0x40100229080420aULL, 0x9801084000201103ULL, 0x8408622090484202ULL, 0x4022001048a0e2ULL,
0x280120020049902ULL, 0x1200412602009402ULL, 0x914900048020884ULL, 0x104824281002402ULL },
{ 0x101010101017eULL, 0x202020202027cULL, 0x404040404047aULL, 0x8080808080876ULL, // Masks
0x1010101010106eULL, 0x2020202020205eULL, 0x4040404040403eULL, 0x8080808080807eULL,
0x1010101017e00ULL, 0x2020202027c00ULL, 0x4040404047a00ULL, 0x8080808087600ULL,
0x10101010106e00ULL, 0x20202020205e00ULL, 0x40404040403e00ULL, 0x80808080807e00ULL,
0x10101017e0100ULL, 0x20202027c0200ULL, 0x40404047a0400ULL, 0x8080808760800ULL,
0x101010106e1000ULL, 0x202020205e2000ULL, 0x404040403e4000ULL, 0x808080807e8000ULL,
0x101017e010100ULL, 0x202027c020200ULL, 0x404047a040400ULL, 0x8080876080800ULL,
0x1010106e101000ULL, 0x2020205e202000ULL, 0x4040403e404000ULL, 0x8080807e808000ULL,
0x1017e01010100ULL, 0x2027c02020200ULL, 0x4047a04040400ULL, 0x8087608080800ULL,
0x10106e10101000ULL, 0x20205e20202000ULL, 0x40403e40404000ULL, 0x80807e80808000ULL,
0x17e0101010100ULL, 0x27c0202020200ULL, 0x47a0404040400ULL, 0x8760808080800ULL,
0x106e1010101000ULL, 0x205e2020202000ULL, 0x403e4040404000ULL, 0x807e8080808000ULL,
0x7e010101010100ULL, 0x7c020202020200ULL, 0x7a040404040400ULL, 0x76080808080800ULL,
0x6e101010101000ULL, 0x5e202020202000ULL, 0x3e404040404000ULL, 0x7e808080808000ULL,
0x7e01010101010100ULL, 0x7c02020202020200ULL, 0x7a04040404040400ULL, 0x7608080808080800ULL,
0x6e10101010101000ULL, 0x5e20202020202000ULL, 0x3e40404040404000ULL, 0x7e80808080808000ULL },
{ 0x101010101017eULL, 0x202020202027cULL, 0x404040404047aULL, 0x8080808080876ULL, // Moves
0x1010101010106eULL, 0x2020202020205eULL, 0x4040404040403eULL, 0x8080808080807eULL,
0x1010101017e00ULL, 0x2020202027c00ULL, 0x4040404047a00ULL, 0x8080808087600ULL,
0x10101010106e00ULL, 0x20202020205e00ULL, 0x40404040403e00ULL, 0x80808080807e00ULL,
0x10101017e0100ULL, 0x20202027c0200ULL, 0x40404047a0400ULL, 0x8080808760800ULL,
0x101010106e1000ULL, 0x202020205e2000ULL, 0x404040403e4000ULL, 0x808080807e8000ULL,
0x101017e010100ULL, 0x202027c020200ULL, 0x404047a040400ULL, 0x8080876080800ULL,
0x1010106e101000ULL, 0x2020205e202000ULL, 0x4040403e404000ULL, 0x8080807e808000ULL,
0x1017e01010100ULL, 0x2027c02020200ULL, 0x4047a04040400ULL, 0x8087608080800ULL,
0x10106e10101000ULL, 0x20205e20202000ULL, 0x40403e40404000ULL, 0x80807e80808000ULL,
0x17e0101010100ULL, 0x27c0202020200ULL, 0x47a0404040400ULL, 0x8760808080800ULL,
0x106e1010101000ULL, 0x205e2020202000ULL, 0x403e4040404000ULL, 0x807e8080808000ULL,
0x7e010101010100ULL, 0x7c020202020200ULL, 0x7a040404040400ULL, 0x76080808080800ULL,
0x6e101010101000ULL, 0x5e202020202000ULL, 0x3e404040404000ULL, 0x7e808080808000ULL,
0x7e01010101010100ULL, 0x7c02020202020200ULL, 0x7a04040404040400ULL, 0x7608080808080800ULL,
0x6e10101010101000ULL, 0x5e20202020202000ULL, 0x3e40404040404000ULL, 0x7e80808080808000ULL }
};
constexpr std::uint64_t kBishopMagics[3][64] = {
{ 0x2890208600480830ULL, 0x324148050f087ULL, 0x1402488a86402004ULL, 0xc2210a1100044bULL, // Magics
0x88450040b021110cULL, 0xc0407240011ULL, 0xd0246940cc101681ULL, 0x1022840c2e410060ULL,
0x4a1804309028d00bULL, 0x821880304a2c0ULL, 0x134088090100280ULL, 0x8102183814c0208ULL,
0x518598604083202ULL, 0x67104040408690ULL, 0x1010040020d000ULL, 0x600001028911902ULL,
0x8810183800c504c4ULL, 0x2628200121054640ULL, 0x28003000102006ULL, 0x4100c204842244ULL,
0x1221c50102421430ULL, 0x80109046e0844002ULL, 0xc128600019010400ULL, 0x812218030404c38ULL,
0x1224152461091c00ULL, 0x1c820008124000aULL, 0xa004868015010400ULL, 0x34c080004202040ULL,
0x200100312100c001ULL, 0x4030048118314100ULL, 0x410000090018ULL, 0x142c010480801ULL,
0x8080841c1d004262ULL, 0x81440f004060406ULL, 0x400a090008202ULL, 0x2204020084280080ULL,
0xb820060400008028ULL, 0x110041840112010ULL, 0x8002080a1c84400ULL, 0x212100111040204aULL,
0x9412118200481012ULL, 0x804105002001444cULL, 0x103001280823000ULL, 0x40088e028080300ULL,
0x51020d8080246601ULL, 0x4a0a100e0804502aULL, 0x5042028328010ULL, 0xe000808180020200ULL,
0x1002020620608101ULL, 0x1108300804090c00ULL, 0x180404848840841ULL, 0x100180040ac80040ULL,
0x20840000c1424001ULL, 0x82c00400108800ULL, 0x28c0493811082aULL, 0x214980910400080cULL,
0x8d1a0210b0c000ULL, 0x164c500ca0410cULL, 0xc6040804283004ULL, 0x14808001a040400ULL,
0x180450800222a011ULL, 0x600014600490202ULL, 0x21040100d903ULL, 0x10404821000420ULL },
{ 0x40201008040200ULL, 0x402010080400ULL, 0x4020100a00ULL, 0x40221400ULL, // Masks
0x2442800ULL, 0x204085000ULL, 0x20408102000ULL, 0x2040810204000ULL,
0x20100804020000ULL, 0x40201008040000ULL, 0x4020100a0000ULL, 0x4022140000ULL,
0x244280000ULL, 0x20408500000ULL, 0x2040810200000ULL, 0x4081020400000ULL,
0x10080402000200ULL, 0x20100804000400ULL, 0x4020100a000a00ULL, 0x402214001400ULL,
0x24428002800ULL, 0x2040850005000ULL, 0x4081020002000ULL, 0x8102040004000ULL,
0x8040200020400ULL, 0x10080400040800ULL, 0x20100a000a1000ULL, 0x40221400142200ULL,
0x2442800284400ULL, 0x4085000500800ULL, 0x8102000201000ULL, 0x10204000402000ULL,
0x4020002040800ULL, 0x8040004081000ULL, 0x100a000a102000ULL, 0x22140014224000ULL,
0x44280028440200ULL, 0x8500050080400ULL, 0x10200020100800ULL, 0x20400040201000ULL,
0x2000204081000ULL, 0x4000408102000ULL, 0xa000a10204000ULL, 0x14001422400000ULL,
0x28002844020000ULL, 0x50005008040200ULL, 0x20002010080400ULL, 0x40004020100800ULL,
0x20408102000ULL, 0x40810204000ULL, 0xa1020400000ULL, 0x142240000000ULL,
0x284402000000ULL, 0x500804020000ULL, 0x201008040200ULL, 0x402010080400ULL,
0x2040810204000ULL, 0x4081020400000ULL, 0xa102040000000ULL, 0x14224000000000ULL,
0x28440200000000ULL, 0x50080402000000ULL, 0x20100804020000ULL, 0x40201008040200ULL },
{ 0x40201008040200ULL, 0x402010080400ULL, 0x4020100a00ULL, 0x40221400ULL, // Moves
0x2442800ULL, 0x204085000ULL, 0x20408102000ULL, 0x2040810204000ULL,
0x20100804020000ULL, 0x40201008040000ULL, 0x4020100a0000ULL, 0x4022140000ULL,
0x244280000ULL, 0x20408500000ULL, 0x2040810200000ULL, 0x4081020400000ULL,
0x10080402000200ULL, 0x20100804000400ULL, 0x4020100a000a00ULL, 0x402214001400ULL,
0x24428002800ULL, 0x2040850005000ULL, 0x4081020002000ULL, 0x8102040004000ULL,
0x8040200020400ULL, 0x10080400040800ULL, 0x20100a000a1000ULL, 0x40221400142200ULL,
0x2442800284400ULL, 0x4085000500800ULL, 0x8102000201000ULL, 0x10204000402000ULL,
0x4020002040800ULL, 0x8040004081000ULL, 0x100a000a102000ULL, 0x22140014224000ULL,
0x44280028440200ULL, 0x8500050080400ULL, 0x10200020100800ULL, 0x20400040201000ULL,
0x2000204081000ULL, 0x4000408102000ULL, 0xa000a10204000ULL, 0x14001422400000ULL,
0x28002844020000ULL, 0x50005008040200ULL, 0x20002010080400ULL, 0x40004020100800ULL,
0x20408102000ULL, 0x40810204000ULL, 0xa1020400000ULL, 0x142240000000ULL,
0x284402000000ULL, 0x500804020000ULL, 0x201008040200ULL, 0x402010080400ULL,
0x2040810204000ULL, 0x4081020400000ULL, 0xa102040000000ULL, 0x14224000000000ULL,
0x28440200000000ULL, 0x50080402000000ULL, 0x20100804020000ULL, 0x40201008040200ULL }
};
// Enums
enum class MoveType { kKiller, kGood };
// Structs
struct Board { // 172B
std::uint64_t white[6]{}; // White bitboards
std::uint64_t black[6]{}; // Black bitboards
std::int32_t score{0}; // Sorting score
std::int8_t pieces[64]{}; // Pieces white and black
std::int8_t epsq{-1}; // En passant square
std::uint8_t index{0}; // Sorting index
std::uint8_t from{0}; // From square
std::uint8_t to{0}; // To square
std::uint8_t type{0}; // Move type ( 0:Normal 1:OOw 2:OOOw 3:OOb 4:OOOb 5:=n 6:=b 7:=r 8:=q )
std::uint8_t castle{0}; // Castling rights ( 0x1:K 0x2:Q 0x4:k 0x8:q )
std::uint8_t fifty{0}; // Rule 50 counter ( 256 max )
bool is_underpromo() const;
bool is_queen_promo() const;
bool is_castling() const;
const std::string movename() const;
const std::string to_fen() const;
const std::string to_s() const;
};
struct HashEntry { // 10B
std::uint32_t killer_hash{0}; // Killer move hash
std::uint32_t good_hash{0}; // Good move hash
std::uint8_t killer{0}; // Killer move index
std::uint8_t good{0}; // Good move index
template <MoveType> void update(const std::uint64_t, const std::uint8_t);
void put_hash_value_to_moves(const std::uint64_t, Board*) const;
};
struct Evaluation {
const std::uint64_t white{0}, black{0}, both{0};
const bool wtm{true};
int w_pieces[5]{}, b_pieces[5]{}, white_total{1}, black_total{1},
both_total{0}, piece_sum{0}, wk{0}, bk{0}, score{0},
mg{0}, eg{0}, scale_factor{1};
void check_blind_bishop_w();
void check_blind_bishop_b();
Evaluation* pesto_w(const int, const int);
Evaluation* pesto_b(const int, const int);
std::uint64_t reachable_w() const;
std::uint64_t reachable_b() const;
Evaluation* mobility_w(const int, const std::uint64_t);
Evaluation* mobility_b(const int, const std::uint64_t);
void eval_w(const int);
void eval_b(const int);
void pawn_w(const int);
void pawn_b(const int);
void knight_w(const int);
void knight_b(const int);
void bishop_w(const int);
void bishop_b(const int);
void rook_w(const int);
void rook_b(const int);
void queen_w(const int);
void queen_b(const int);
void king_w(const int);
void king_b(const int);
void eval_piece(const int);
Evaluation* evaluate_pieces();
void bonus_knbk_w();
void bonus_knbk_b();
Evaluation* bonus_tempo();
Evaluation* bonus_checks();
void bonus_mating_w();
void bonus_mating_b();
int wp(const std::size_t);
int bp(const std::size_t);
Evaluation* bonus_bishop_pair();
void bonus_special_4men();
void bonus_special_5men();
void white_is_mating();
void black_is_mating();
Evaluation* bonus_endgame();
int calculate_score() const;
int evaluate();
};
struct NnueEval {
const bool wtm{true};
int probe() const;
int evaluate();
};
struct RootCompFunctor {
bool operator()(const Board &, const Board &) const;
};
// Material detection for classical activation
struct Material {
const int white_n{0}, black_n{0};
bool is_rook_ending() const;
bool is_easy() const;
bool is_endgame() const;
bool is_9_plus_pawns() const;
bool is_3_plus_minors() const;
bool is_pawns_on_1_or_8_ranks() const;
bool is_lots_of_pieces() const;
bool is_weird() const;
};
// Save state (just in case) if multiple commands in a row
struct Save {
const bool nnue{false}, book{false};
const std::string fen{};
Save();
~Save();
};
// Variables
std::uint64_t g_black = 0, g_white = 0, g_both = 0, g_empty = 0, g_good = 0, g_stop_search_time = 0,
g_nodes = 0, g_pawn_sq = 0, g_pawn_1_moves_w[64]{}, g_pawn_1_moves_b[64]{}, g_pawn_2_moves_w[64]{},
g_pawn_2_moves_b[64]{}, g_knight_moves[64]{}, g_king_moves[64]{}, g_pawn_checks_w[64]{}, g_pawn_checks_b[64]{},
g_castle_no_checks_w[2]{}, g_castle_no_checks_b[2]{}, g_castle_empty_w[2]{}, g_castle_empty_b[2]{}, g_bishop_magic_moves[64][512]{},
g_rook_magic_moves[64][4096]{}, g_zobrist_ep[64]{}, g_zobrist_castle[16]{}, g_zobrist_wtm[2]{},
g_r50_positions[R50_ARR]{}, g_zobrist_board[13][64]{};
int g_move_overhead = MOVEOVERHEAD, g_level = LEVEL, g_root_n = 0, g_king_w = 0, g_king_b = 0, g_moves_n = 0,
g_max_depth = MAX_SEARCH_DEPTH, g_q_depth = 0, g_depth = 0, g_best_score = 0, g_noise = NOISE, g_last_eval = 0,
g_fullmoves = 1, g_rook_w[2]{}, g_rook_b[2]{}, g_nnue_pieces[64]{}, g_nnue_squares[64]{};
bool g_chess960 = false, g_wtm = false, g_underpromos = true, g_nullmove_active = false,
g_stop_search = false, g_is_pv = false, g_book_exist = false, g_nnue_exist = false,
g_classical = true, g_game_on = true, g_analyzing = false;
Board g_board_empty{}, *g_board = &g_board_empty, *g_moves = nullptr, *g_board_orig = nullptr,
g_boards[MAX_SEARCH_DEPTH + MAX_Q_SEARCH_DEPTH][MAX_MOVES]{};
std::uint32_t g_hash_entries = 0, g_tokens_nth = 0;
std::vector<std::string> g_tokens(256); // 300 plys init
polyglotbook::PolyglotBook g_book{};
std::unique_ptr<HashEntry[]> g_hash{};
// Prototypes
int SearchW(int, const int, const int, const int);
int SearchB(const int, int, const int, const int);
int QSearchB(const int, int, const int, const int);
int Evaluate(const bool);
bool ChecksW();
bool ChecksB();
std::uint64_t GetRookMagicMoves(const int, const std::uint64_t);
std::uint64_t GetBishopMagicMoves(const int, const std::uint64_t);
// Utils
// White bitboards
inline std::uint64_t White() {
return g_board->white[0] | g_board->white[1] | g_board->white[2] |
g_board->white[3] | g_board->white[4] | g_board->white[5];
}
// Black bitboards
inline std::uint64_t Black() {
return g_board->black[0] | g_board->black[1] | g_board->black[2] |
g_board->black[3] | g_board->black[4] | g_board->black[5];
}
// Both colors
inline std::uint64_t Both() {
return White() | Black();
}
// Set bit in 1 -> 64
inline constexpr std::uint64_t Bit(const int nth) {
return 0x1ULL << nth;
}
// Count rightmost zeros AND then pop BitBoard
inline int CtzrPop(std::uint64_t *b) {
const int ret = std::countr_zero(*b); // 1011000 -> 3
*b &= *b - 0x1ULL;
return ret;
}
// X axle of board
inline int MakeX(const int sq) {
return int(sq % 8);
}
// Y axle of board
inline int MakeY(const int sq) {
return int(sq / 8);
}
// Nodes Per Second
std::uint64_t Nps(const std::uint64_t nodes, const std::uint64_t ms) {
return static_cast<std::uint64_t>(1000 * nodes) / std::max<std::uint64_t>(1, ms);
}
// Is (x, y) on board ? Slow, but only for init
bool IsOnBoard(const int x, const int y) {
return x >= 0 && x <= 7 && y >= 0 && y <= 7;
}
// X-coord to char
char MakeFile2Char(const int f) {
switch (f) {
case 0: return 'a';
case 1: return 'b';
case 2: return 'c';
case 3: return 'd';
case 4: return 'e';
case 5: return 'f';
case 6: return 'g';
default: return 'h';
}
}
// Y-coord to char
char MakeRank2Char(const int r) {
switch (r) {
case 0: return '1';
case 1: return '2';
case 2: return '3';
case 3: return '4';
case 4: return '5';
case 5: return '6';
case 6: return '7';
default: return '8';
}
}
// Convert int coords to string
const std::string MakeMove2Str(const int from, const int to) {
return std::string{MakeFile2Char(MakeX(from)), MakeRank2Char(MakeY(from)),
MakeFile2Char(MakeX(to)), MakeRank2Char(MakeY(to))};
}
extern "C" {
bool IsInputAvailable() { // See if std::cin has smt
#ifdef WINDOWS
return _kbhit();
#else
fd_set fd{};
struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
FD_ZERO(&fd);
FD_SET(STDIN_FILENO, &fd);
select(STDIN_FILENO + 1, &fd, nullptr, nullptr, &tv);
return FD_ISSET(STDIN_FILENO, &fd) > 0;
#endif
}
}
// ms since 1.1.1970
std::uint64_t Now(const std::uint64_t ms = 0) {
return ms +
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()) .count();
}
std::uint64_t Mixer(const std::uint64_t num) {
return ((num) << 7) ^ ((num) >> 5);
}
// Deterministic Random()
std::uint64_t Random64() {
static std::uint64_t a = 0X12311227ULL, b = 0X1931311ULL, c = 0X13138141ULL;
a ^= b + c;
b ^= b * c + 0x1717711ULL;
c = (3 * c) + 0x1ULL;
return Mixer(a) ^ Mixer(b) ^ Mixer(c);
}
// 8x deterministic random for zobrist
std::uint64_t Random8x64() {
std::uint64_t ret = 0;
for (std::size_t i = 0; i < 8; i += 1) ret ^= Random64() << (8 * i);
return ret;
}
// Nondeterministic Rand()
int Random(const int min, const int max) {
static std::uint64_t seed = 0x202c7ULL + static_cast<std::uint64_t>(std::time(nullptr));
if (min == max) return min;
if (min > max) return Random(max, min);
seed = (seed << 5) ^ (seed + 1) ^ (seed >> 3);
return min + static_cast<int>(seed % static_cast<std::uint64_t>(std::abs(max - min) + 1));
}
// Split string by given str
template <class T>
void SplitString(const std::string &str, T &cont, const std::string &delims = " ") {
std::size_t cur = str.find_first_of(delims), prev = 0;
while (cur != std::string::npos) {
cont.push_back(str.substr(prev, cur - prev));
prev = cur + 1;
cur = str.find_first_of(delims, prev);
}
cont.push_back(str.substr(prev, cur - prev));
}
// Flips the fen and bm. Fully legal FEN expected.
// 8/5P1p/6kr/7p/7P/5K2/8/8 w - - 0 1 ; bm f7f8b
// -->
// 8/8/5k2/7p/7P/6KR/5p1P/8 b - - 0 1 ; bm f2f1b
const std::string FlipFen(const std::string &fen) {
std::string s{};
const std::string num = "12345678", num_flip = "87654321";
const std::string small = "pnbrqkwacdefgh", upper = "PNBRQKWACDEFGH";
bool only_number = false;
int empty = 0;
for (std::size_t i = 0; i < fen.size(); i += 1) {
if (fen[i] == ' ') {
empty += 1;
if (empty == 1) {
std::vector<std::string> pieces{};
SplitString< std::vector< std::string> >(s, pieces, "/");
if (pieces.size() != 8) throw std::runtime_error("info string ( #1 ) Bad FEN: " + fen);
s = "";
for (std::size_t k = 0; k < 8; k += 1) {
s += pieces[7 - k];
if (k <= 6) s += '/';
}
}
}
if (fen[i] == ';') only_number = true;
if (small.find(fen[i]) != std::string::npos && !only_number) {
s += empty == 1 ? (fen[i] == 'w' ? 'b' : 'w') : static_cast<char>(std::toupper(fen[i]));
} else if (upper.find(fen[i]) != std::string::npos && !only_number) {
s += static_cast<char>(std::tolower(fen[i]));
} else {
const std::size_t j = num.find(fen[i]);
s += (j != std::string::npos && (empty == 3 || empty == 8)) ? num_flip[j] : fen[i];
}
}
return s;
}
// Read input from std::cin
void ReadInput() {
std::string line{};
std::getline(std::cin, line);
g_tokens_nth = 0;
g_tokens.clear();
SplitString< std::vector<std::string> >(line, g_tokens);
}
// PolyGlot Book lib
void SetBook(const std::string &book_file = BOOK_FILE) {
g_book_exist = USE_BOOK && (book_file.length() <= 1 ? false : g_book.open_book(book_file));
}
// NNUE lib
void SetNNUE(const std::string &eval_file = EVAL_FILE) {
g_classical = USE_NNUE && (!(g_nnue_exist = eval_file.length() <= 1 ? false : nnue::nnue_init(eval_file.c_str())));
}
// Hashtable
void SetHashtable(const int hash_mb2 = DEF_HASH_MB) {
const int hash_mb = std::clamp(hash_mb2, 1, 1048576); // Limits 1MB -> 1TB
g_hash_entries = static_cast<std::uint32_t>((1 << 20) * hash_mb) / (sizeof(HashEntry)); // Hash(B) / Block(B)
g_hash.reset(new HashEntry[g_hash_entries]); // Claim space
}
// Hash
std::uint64_t Hash(const bool wtm) {
std::uint64_t ret = g_zobrist_ep[g_board->epsq + 1] ^
g_zobrist_wtm[wtm] ^ g_zobrist_castle[g_board->castle];
for (auto both = Both(); both; ) {
const auto sq = CtzrPop(&both);
ret ^= g_zobrist_board[g_board->pieces[sq] + 6][sq];
}
return ret;
}
// struct HashEntry
// Update hashtable sorting algorithm
template <MoveType type>
void HashEntry::update(const std::uint64_t hash, const std::uint8_t index) {
if constexpr (type == MoveType::kKiller) {
this->killer_hash = static_cast<std::uint32_t>(hash >> 32);
this->killer = index + 1;
} else { // == MoveType::kGood !
this->good_hash = static_cast<std::uint32_t>(hash >> 32);
this->good = index + 1;
}
}
// Best moves put first for maximum cutoffs
void HashEntry::put_hash_value_to_moves(const std::uint64_t hash, Board *moves) const {
if (this->killer && (this->killer_hash == static_cast<std::uint32_t>(hash >> 32)))
moves[this->killer - 1].score += 10000;
if (this->good && (this->good_hash == static_cast<std::uint32_t>(hash >> 32)))
moves[this->good - 1].score += 7000;
}
// struct Board
bool Board::is_queen_promo() const {
return this->type == 8; // e7e8q
}
bool Board::is_castling() const {
switch (this->type) {
// O-Ow / O-O-Ow / O-Ob / O-O-Ob
case 1: case 2: case 3: case 4: return true;
default: return false;
}
}
bool Board::is_underpromo() const {
switch (this->type) {
// e7e8n / e7e8n / e7e8r
case 5: case 6: case 7: return true;
default: return false;
}
}
const std::string Board::movename() const {
switch (this->type) {
case 1: return MakeMove2Str(g_king_w, g_chess960 ? g_rook_w[0] : 6); // O-Ow
case 2: return MakeMove2Str(g_king_w, g_chess960 ? g_rook_w[1] : 2); // O-O-Ow
case 3: return MakeMove2Str(g_king_b, g_chess960 ? g_rook_b[0] : 56 + 6); // O-Ob
case 4: return MakeMove2Str(g_king_b, g_chess960 ? g_rook_b[1] : 56 + 2); // O-O-Ob
case 5: return MakeMove2Str(this->from, this->to) + 'n'; // e7e8n
case 6: return MakeMove2Str(this->from, this->to) + 'b'; // e7e8b
case 7: return MakeMove2Str(this->from, this->to) + 'r'; // e7e8r
case 8: return MakeMove2Str(this->from, this->to) + 'q'; // e7e8q
default: return MakeMove2Str(this->from, this->to); // Normal
}
}
char GetPiece(const int piece) {
switch (piece) {
case +1: return 'P';
case +2: return 'N';
case +3: return 'B';
case +4: return 'R';
case +5: return 'Q';
case +6: return 'K';
case -1: return 'p';
case -2: return 'n';
case -3: return 'b';
case -4: return 'r';
case -5: return 'q';
case -6: return 'k';
default: return '.';
}
}
char GetCastleFile(const int file) {
switch (file) {
case 0: return 'A';
case 1: return 'B';
case 2: return 'C';
case 3: return 'D';
case 4: return 'E';
case 5: return 'F';
case 6: return 'G';
case 7: return 'H';
case 56: return 'a';
case 57: return 'b';
case 58: return 'c';
case 59: return 'd';
case 60: return 'e';
case 61: return 'f';
case 62: return 'g';
case 63: return 'h';
default: return '.';
}
}
// Board presentation in FEN ( Forsyth–Edwards Notation )
const std::string Board::to_fen() const {
std::stringstream s{};
for (auto r = 7; r >= 0; r -= 1) {
auto empty = 0;
for (auto f = 0; f <= 7; f += 1)
if (const auto p = GetPiece(this->pieces[8 * r + f]); p == '.') {
empty += 1;
} else {
if (empty) {
s << empty;
empty = 0;
}
s << p;
}
if (empty) s << empty;
if (r != 0) s << "/";
}
s << (g_wtm ? " w " : " b ");
if (this->castle & 0x1) s << GetCastleFile(g_rook_w[0]);
if (this->castle & 0x2) s << GetCastleFile(g_rook_w[1]);
if (this->castle & 0x4) s << GetCastleFile(g_rook_b[0]);
if (this->castle & 0x8) s << GetCastleFile(g_rook_b[1]);
s << (this->castle ? " " : "- ");
if (this->epsq == -1)
s << "-";
else
s << MakeFile2Char(MakeX(this->epsq)) << MakeRank2Char(MakeY(this->epsq));
s << " " << static_cast<int>(this->fifty) << " " << static_cast<int>(std::max(1, g_fullmoves));
return s.str();
}
// String presentation of board
const std::string Board::to_s() const {
std::stringstream s{};
s << " +---+---+---+---+---+---+---+---+\n";
for (auto r = 7; r >= 0; r -= 1) {
for (auto f = 0; f <= 7; f += 1)
s << " | " << GetPiece(this->pieces[8 * r + f]);
s << " | " << (1 + r) << "\n +---+---+---+---+---+---+---+---+\n";
}
s << " a b c d e f g h\n\n" <<
"> " << this->to_fen() << '\n' <<
"> Eval: " << std::showpos << Evaluate(g_wtm) << std::noshowpos << " | " <<
"NNUE: " << (g_nnue_exist ? "OK" : "FAIL") << " | " <<
"Book: " << (g_book_exist ? "OK" : "FAIL");
return s.str();
}
// Tokenizer
bool TokenIsOk(const std::uint32_t nth = 0) {
return g_tokens_nth + nth < g_tokens.size(); // O(1)
}
const std::string TokenGetNth(const std::uint32_t nth = 0) {
return TokenIsOk(nth) ? g_tokens[g_tokens_nth + nth] : "";
}
void TokenPop() {
g_tokens_nth += 1;
}
bool TokenPeek(const std::string &token, const std::uint32_t nth = 0) {
return TokenIsOk(nth) && token == g_tokens[g_tokens_nth + nth];
}
int TokenGetNumber(const std::uint32_t nth = 0) {
return TokenIsOk(nth) ? std::stoi(g_tokens[g_tokens_nth + nth]) : 0;
}
void TokenExpect(const std::string &token) {
if (!TokenPeek(token))
throw std::runtime_error("info string ( #11 ) Unexpected token: " + token);
TokenPop();
}
bool Token(const std::string &token) {
if (!TokenPeek(token)) return false;
TokenPop(); // If true then pop
return true;
}
// Fen handling
std::uint64_t FenFill(int from, const int to) { // from / to -> Always good
auto ret = Bit(from); // Build filled bitboard
if (from == to) return ret;
const auto diff = from > to ? -1 : +1;
do { from += diff; ret |= Bit(from); } while (from != to);
return ret;
}
// White: O-O
void FenBuildCastlingBitboard_O_O_W() {
if (!(g_board->castle & 0x1)) return;
g_castle_no_checks_w[0] = FenFill(g_king_w, 6);
g_castle_empty_w[0] = (g_castle_no_checks_w[0] | FenFill(g_rook_w[0], 5)) ^ (Bit(g_king_w) | Bit(g_rook_w[0]));
}
// White: O-O-O
void FenBuildCastlingBitboard_O_O_O_W() {
if (!(g_board->castle & 0x2)) return;
g_castle_no_checks_w[1] = FenFill(g_king_w, 2);
g_castle_empty_w[1] = (g_castle_no_checks_w[1] | FenFill(g_rook_w[1], 3)) ^ (Bit(g_king_w) | Bit(g_rook_w[1]));
}
// Black: O-O
void FenBuildCastlingBitboard_O_O_B() {
if (!(g_board->castle & 0x4)) return;
g_castle_no_checks_b[0] = FenFill(g_king_b, 56 + 6);
g_castle_empty_b[0] = (g_castle_no_checks_b[0] | FenFill(g_rook_b[0], 56 + 5)) ^ (Bit(g_king_b) | Bit(g_rook_b[0]));
}
// Black: O-O-O
void FenBuildCastlingBitboard_O_O_O_B() {
if (!(g_board->castle & 0x8)) return;
g_castle_no_checks_b[1] = FenFill(g_king_b, 56 + 2);
g_castle_empty_b[1] = (g_castle_no_checks_b[1] | FenFill(g_rook_b[1], 56 + 3)) ^ (Bit(g_king_b) | Bit(g_rook_b[1]));
}
void FenCheckCastlingBitboards() {
for (const std::size_t i : {0, 1}) {
g_castle_empty_w[i] &= 0xFFULL;
g_castle_empty_b[i] &= 0xFF00000000000000ULL;
g_castle_no_checks_w[i] &= 0xFFULL;
g_castle_no_checks_b[i] &= 0xFF00000000000000ULL;
}
}
void FenBuildCastlingBitboards() {
FenBuildCastlingBitboard_O_O_W();
FenBuildCastlingBitboard_O_O_O_W();
FenBuildCastlingBitboard_O_O_B();
FenBuildCastlingBitboard_O_O_O_B();
FenCheckCastlingBitboards();
}
void FenFindKings(const int sq, const int piece) {
switch (piece) {
case +6: g_king_w = sq; break; // K
case -6: g_king_b = sq; break; // k
}
}
void FenPutPieceOnBoard(const int sq, const int piece) {
g_board->pieces[sq] = piece;
}
void FenCreatePieceBitboards(const int sq, const int piece) {
if (piece > 0) g_board->white[+piece - 1] |= Bit(sq);
else if (piece < 0) g_board->black[-piece - 1] |= Bit(sq);
}
void FenPutPiece(const int sq, const int piece) {
FenFindKings(sq, piece);
FenPutPieceOnBoard(sq, piece);
FenCreatePieceBitboards(sq, piece);
}
int FenMakePiece2Num(const char p) {
switch (p) {
case 'P': return +1;
case 'N': return +2;
case 'B': return +3;
case 'R': return +4;
case 'Q': return +5;
case 'K': return +6;
case 'p': return -1;
case 'n': return -2;
case 'b': return -3;
case 'r': return -4;
case 'q': return -5;
case 'k': return -6;
default: return 0;
}
}
int FenMakeChar2Num(const char e) {
switch (e) {
case '1': return 1;
case '2': return 2;
case '3': return 3;