-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoChess7.4.cpp
More file actions
3770 lines (3452 loc) · 127 KB
/
oChess7.4.cpp
File metadata and controls
3770 lines (3452 loc) · 127 KB
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
/*=====================================
Programmer: Airzy T
Assignment: FINAL PROJECT CHESS
Description(I.P.O):
Input:
- menu driven program
enter letter inside [here] to select options
// inside playing
* chess is turn based and white always moves first
example input: e2 e4
or e2 enter e4 - to see moves beforehand
Process:
// move generation
- every possible move is calculated before hand and stores in big arrays
for fast access (caches)
- convert inputs into a number corresponding to the tile number in the
8x8 chessboard ex. a1 --> 0 or h8 --> 63
- find and generate moves using input by looking it up on the caches
// bot algorithm
- uses the RECURSION algorithm knows as alpha beta
alpha beta [plays the game ahead of time] to see which moves are
winning and loosing the bot will always play the best moves it can find
- everything is optimized in the binary level storing moves and pieces
smaller than an integer
Outputs:
// move generation
- displays all the legal moves as highlights in the output
// board
- top bar shows an evaluation bar determining the winning team
- uses ascii characters of chess pieces
Assumptions:
- user is expected to enter valid inputs
- putting variables in public instead of using setter getter FOR A FASTER
LOOKUP
- using ++i pre-increment insted of post-increment i++, its abit faster
- minimized & risking more memmory for speed
- needs at least 0.9 - 2 MB for storing pseudo-legal moves
SOURCES:
helped alot for understanding bitwise operations
https://bitwisecmd.com/
https://www.rapidtables.com/convert/number/decimal-to-binary.html
fen and to compare with
https://lichess.org/analysis
https://lichess.org/editor
main sources
https://www.chessprogramming.org/
https://www.youtube.com/@chessprogramming591
https://github.com/SebLague/Chess-Coding-Adventure
=======================================*/
/*
#include <WinNls.h>
#include <consoleapi2.h>
#include <processenv.h>
#include <stdlib.h>
#include <windows.h> // for console visuals
#include <algorithm> // for move sorting
#include <cctype>
#include <chrono> // for timing
#include <cstdint> // For UINT64_MAX and var types
#include <iostream> //
#include <random> // for random uint64 numbers
#include <string> // string to int stoi(
*/
#include <iostream>
#include <random>
#include <string>
#include <chrono>
#include <windows.h>
void setTxtColor(int colorValue);
void printUint32Binary(uint32_t num);
void toLowercase(std::string &input);
void allowEmojis();
uint16_t ***allocateMoveHHistory();
void deallocateMoveHHistory(uint16_t ***moveHHistory);
std::string intToString(int num);
std::string invertFen(const std::string STR);
// instead of using boolean array
// 1ULL = 00000000 00000000 00000000 00000000 00000000 00000000 00000000
// 00000001
class BitBoard {
private:
uint64_t bitBoard = 0; // 64 bits
public:
inline void setSquare(uint8_t square) {
bitBoard |=
(1ULL << square); // set the bit corresponding to the index square
};
inline void unSetSquare(uint8_t square) {
bitBoard &=
~(1ULL << square); // clear the bit corresponding to the index square
};
inline bool isSet(uint8_t square) const {
return (bitBoard >> square) &
1; // check if the bit corresponding to the square is set
};
inline void clearBoard() { bitBoard = 0; }; // clear the entire bitboard
inline uint64_t get() const { return bitBoard; }; // get bb
inline void set(uint64_t bb) { bitBoard = bb; }; // set bb
inline bool isEmpty() const { return bitBoard == 0; };
// Population count (Hamming weight) function
inline int populationCount() const {
uint64_t x = bitBoard; // copy bitboard for manipulation
x = x - ((x >> 1) & 0x5555555555555555ULL); // step 1: divide and conquer
// to sum 2 bits at a time
x = (x & 0x3333333333333333ULL) +
((x >> 2) & 0x3333333333333333ULL); // step 2: Sum groups of 4 bits
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL; // step 3: sum groups of 8 bits
x = x + (x >> 8); // step 4: Sum groups of 16 bits
x = x + (x >> 16); // step 5: Sum groups of 32 bits
x = x + (x >> 32); // step 6: Sum all bits in the 64-bit integer
return x & 0x7F; // return only the least significant 7 bits (to handle
// overflow)
}
inline int populationCountBAND(uint64_t x2) const {
uint64_t x = (bitBoard & x2); // copy bitboard and band for manip
x = x - ((x >> 1) & 0x5555555555555555ULL); // step 1: divide and conquer
// to sum 2 bits at a time
x = (x & 0x3333333333333333ULL) +
((x >> 2) & 0x3333333333333333ULL); // step 2: Sum groups of 4 bits
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL; // step 3: sum groups of 8 bits
x = x + (x >> 8); // step 4: Sum groups of 16 bits
x = x + (x >> 16); // step 5: Sum groups of 32 bits
x = x + (x >> 32); // step 6: Sum all bits in the 64-bit integer
return x & 0x7F; // return only the least significant 7 bits (to handle
// overflow)
}
BitBoard(){};
BitBoard(uint64_t &newbb) { bitBoard = newbb; };
};
// lookup class
class PieceCache {
private:
static constexpr uint8_t TYPE_MASK = 0b111; // 7
static constexpr uint8_t COLOR_MASK = 0b1000; // 8
// stored unicodes for displaying PIECES
const std::string KING_UNICODE = "\xE2\x99\x9A";
const std::string QUEEN_UNICODE = "\xE2\x99\x9B";
const std::string ROOK_UNICODE = "\xE2\x99\x9C";
const std::string BISHOP_UNICODE = "\xE2\x99\x9D";
const std::string KNIGHT_UNICODE = "\xE2\x99\x9E";
const std::string PAWN_UNICODE = "\xE2\x99\x99";
const std::string EMPTY_STRING = " ";
const std::string UNKNOWN_TYPE = "?";
public:
// last three bit represent piece type
// first bit represents whtie/black
// (value & 7) to get. Reason = 7 = 0111 piece type bitmask
const uint8_t EMPTY = 0; // 0000
const uint8_t PAWN = 1; // 0001
const uint8_t KNIGHT = 2; // 0010
const uint8_t BISHOP = 3; // 0011
const uint8_t ROOK = 4; // 0100
const uint8_t QUEEN = 5; // 0101
const uint8_t KING = 6; // 0110
// colors
// (value & 8) to get. Reason = 8 = 1000 color bitmask
const uint8_t WHITE = 0;
const uint8_t BLACK = 8;
// pre defined PIECES
const uint8_t WPAWN = PAWN | WHITE; // 1
const uint8_t WKNIGHT = KNIGHT | WHITE; // 2
const uint8_t WBISHOP = BISHOP | WHITE; // 3
const uint8_t WROOK = ROOK | WHITE; // 4
const uint8_t WQUEEN = QUEEN | WHITE; // 5
const uint8_t WKING = KING | WHITE; // 6
const uint8_t BPAWN = PAWN | BLACK; // 9
const uint8_t BKNIGHT = KNIGHT | BLACK; // 10
const uint8_t BBISHOP = BISHOP | BLACK; // 11
const uint8_t BROOK = ROOK | BLACK; // 12
const uint8_t BQUEEN = QUEEN | BLACK; // 13
const uint8_t BKING = KING | BLACK; // 14
inline uint8_t type(const uint8_t piece) const { return (piece & TYPE_MASK); }
inline uint8_t color(const uint8_t piece) const {
return (piece & COLOR_MASK);
}
const std::string toUnicode(const uint8_t pieceType) const {
switch (pieceType) {
case 0:
return EMPTY_STRING;
case 1:
return PAWN_UNICODE;
case 2:
return KNIGHT_UNICODE;
case 3:
return BISHOP_UNICODE;
case 4:
return ROOK_UNICODE;
case 5:
return QUEEN_UNICODE;
case 6:
return KING_UNICODE;
default:
return UNKNOWN_TYPE;
}
return 0;
}
};
// defining my lookup
const PieceCache PIECES;
// lookup struct
struct MoveCache {
// Masks
static constexpr unsigned short fromTileMask = 0b0000000000111111;
static constexpr unsigned short toTileMask = 0b0000111111000000;
static constexpr unsigned short flagMask = 0b1111000000000000;
static constexpr unsigned short inverseFlagMask = 0b0000111111111111;
// Flags
static constexpr uint8_t NoFlag = 0b0000; // 0
static constexpr uint8_t EnPassantCaptureFlag = 0b0001; // 1
static constexpr uint8_t CastleFlag = 0b0010; // 2
static constexpr uint8_t PawnTwoUpFlag = 0b0011; // 3
static constexpr uint8_t PromoteToQueenFlag = 0b0100; // 4
static constexpr uint8_t PromoteToKnightFlag = 0b0101; // 5
static constexpr uint8_t PromoteToRookFlag = 0b0110; // 6
static constexpr uint8_t PromoteToBishopFlag = 0b0111; // 7
// other
static constexpr unsigned short Null = 0b0000000000000000;
};
// Compact (16 bit) move representation to preserve memory during search.
// The format is as follows (ffffttttttssssss)
// Bits 0-5: start square index
// Bits 6-11: target square index
// Bits 12-15: flag (promotion type, ect)
class Move {
private:
// 16bit move value ffffttttttssssss;
unsigned short moveValue = 0;
uint8_t flag() const { return moveValue >> 12; };
public:
inline uint8_t moveFrom() const {
return moveValue & MoveCache::fromTileMask;
};
inline uint8_t moveTo() const {
return (moveValue & MoveCache::toTileMask) >> 6;
};
inline bool isNull() const { return moveValue == MoveCache::Null; };
inline bool isCastling() const { return flag() == MoveCache::CastleFlag; };
inline bool isPawnTwoUp() const {
return flag() == MoveCache::PawnTwoUpFlag;
};
inline bool isEnPassant() const {
return flag() == MoveCache::EnPassantCaptureFlag;
};
inline bool promoteQueen() const {
return flag() == MoveCache::PromoteToQueenFlag;
};
inline bool promoteRook() const {
return flag() == MoveCache::PromoteToRookFlag;
};
inline bool promoteBishop() const {
return flag() == MoveCache::PromoteToBishopFlag;
};
inline bool promoteKnight() const {
return flag() == MoveCache::PromoteToKnightFlag;
};
inline bool isPromotion() const {
uint8_t fg = flag();
return (fg == MoveCache::PromoteToQueenFlag ||
fg == MoveCache::PromoteToRookFlag ||
fg == MoveCache::PromoteToBishopFlag ||
fg == MoveCache::PromoteToKnightFlag);
}
inline bool isDoublePawnPush() const {
return flag() == MoveCache::PawnTwoUpFlag;
}
inline void setFlag(uint8_t newFlag) {
// Clear the existing flag bits
moveValue &= MoveCache::inverseFlagMask;
// Set the new flag bits
moveValue |= (newFlag << 12);
}
inline void clearMove() { moveValue = MoveCache::Null; };
Move() { moveValue = MoveCache::Null; };
Move(uint8_t fromTile, uint8_t toTile) {
moveValue = (fromTile | (toTile << 6));
};
Move(uint8_t fromTile, uint8_t toTile, uint8_t flag) {
moveValue = (fromTile | toTile << 6 | flag << 12);
}
};
// lookup cache for colors
struct globalColors {
// visual colors
const uint8_t hlightCol = 240; // White background Black text
uint8_t wBlackCol = 128; // Gray background black letter
uint8_t wWhiteCol = 143; // Gray background white letter
uint8_t bWhiteCol = 15; // Black background white latter
uint8_t bBlackCol = 8; // Black background gray letter
const uint8_t greyLetCol = 8; // Black background gray letter
// green previous move
const uint8_t prevMBlackCol = 32;
const uint8_t prevMWhiteCol = 47;
// red check color
const uint8_t checkBlackCol = 207; // 64;
const uint8_t checkWhiteCol = 192; // 79;
};
globalColors chessColors;
// biggest lookup class
class PreComputedCache {
private:
uint8_t rowColValues[8][8]; // each element of [row][col] is a tile
public:
uint8_t preComputedRows[64]; // each element is set to which row its on
uint8_t preComputedCols[64]; // each element is set to which col its on
static constexpr int directionOffsets[8] = {
// diagonals
7, // [0] up right
9, // [1] up left
-9, // [2] down right
-7, // [3] down left
// orthogonals
8, // [4] up
-8, // [5] down
-1, // [6] right
1, // [7] left
};
// pre-compute pseudo legal moves for each tiles
Move bPawnMoves[64][8]; // 2 max possible moves 2 diagonal takes + promotions
Move wPawnMoves[64][8]; // 2 max possible moves 2 diagonal takes + promotions
Move knightMoves[64][8]; // 8 max possible moves
Move kingMoves[64][8]; // 8 max possible moves
// directions
Move bishopMoves[4][64]
[7]; // 13 max, 4 directions, 7 moves max in each direction
Move rookMoves[4][64]
[7]; // 14 max, 4 directions, 7 moves max in each direction
// castling
Move whiteKingSideCastle;
Move whiteQueenSideCastle;
Move blackKingSideCastle;
Move blackQueenSideCastle;
// returns bitboard ray between from tile to tile
BitBoard rays[64][64]; // [fromtile][totile]
BitBoard
rays1Extra[64][64]; // [fromtile][totile] but 1 extra to preventKingMoves
// distances between tiles
unsigned int distances[64][64]; // [fromtile][totile]
// normal rook tiles
const uint8_t whiteKingRook = 7;
const uint8_t whiteQueenRook = 0;
const uint8_t blackKingRook = 63;
const uint8_t blackQueenRook = 56;
// castled rook tiles
const uint8_t whiteKingRookCastleTo = 5;
const uint8_t whiteQueenRookCastleTo = 3;
const uint8_t blackKingRookCastleTo = 61;
const uint8_t blackQueenRookCastleTo = 59;
// castled king tiles
const uint8_t whiteKingCastleTo = 6;
const uint8_t whiteQueenCastleTo = 2;
const uint8_t blackKingCastleTo = 62;
const uint8_t blackQueenCastleTo = 58;
// queen side vacant tiles - tiles in where it has to be empty to allow
// castling
const uint8_t whiteQueenCastleVacant = 1;
const uint8_t blackQueenCastleVacant = 57;
std::string startingFen =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
std::string notatedTiles[64]; // lookup array for tile notations
uint8_t notationToTile(std::string ¬ation) const {
int fileNum =
notation[0] - 'a'; // Convert file to a number between 0 and 7
int rankNum =
notation[1] - '1'; // Convert rank to a number between 0 and 7
if (fileNum >= 0 && fileNum <= 7 && rankNum >= 0 && rankNum <= 7) {
return rowColValues[rankNum][fileNum];
} else {
return 0;
}
}
std::string tileToNotation(const uint8_t &tile) const {
return notatedTiles[tile];
}
// piece values in hundy
static constexpr int pawnValue = 100 * 10;
static constexpr int knightValue = 300 * 10;
static constexpr int bishopValue = 300 * 10;
static constexpr int rookValue = 500 * 10;
static constexpr int queenValue = 900 * 10;
// in 1 Million
static constexpr int evalPositiveInf = 1000000;
static constexpr int evalNegativeInf = -1000000;
// in 900K
static constexpr int evalWhiteWins = 900000;
static constexpr int evalWhiteLoss = -900000;
// piece square tables
int centerPST[64] = {
-10, 0, 1, 2, 2, 1, 0, -10, //
0, 10, 10, 15, 15, 10, 10, 0, //
0, 15, 30, 35, 35, 30, 15, 1, //
2, 25, 40, 50, 50, 40, 25, 2, //
2, 25, 40, 50, 50, 40, 25, 2, // for king endgame
0, 15, 30, 35, 35, 30, 15, 1, //
0, 10, 10, 15, 15, 10, 10, 0, //
-10, 0, 1, 2, 2, 1, 0, -10, //
};
// for ordering moves
int statOrdPST[64] = {
0, 0, 1, 1, 1, 1, 0, 0, //
0, 1, 2, 2, 2, 2, 1, 0, //
1, 4, 9, 6, 6, 9, 4, 1, //
2, 8, 8, 10, 10, 8, 8, 2, //
2, 8, 8, 10, 10, 8, 8, 2, //
1, 4, 9, 6, 6, 9, 4, 1, //
0, 1, 2, 2, 2, 2, 1, 0, //
0, 0, 1, 1, 1, 1, 0, 0, //
};
int pawnPST[2][64] = {
0, 0, 0, 0,
0, 0, 0, 0, //
90, 90, 90, 90,
90, 90, 90, 90, //
10, 10, 20, 40,
40, 20, 10, 10, //
0, 0, 20, 40,
40, 0, 0, 0, //
-4, -2, 20, 40,
40, -5, -8, -8, //
-4, 0, 5, -10,
0, -5, 5, 3, //
-5, 0, -10, -30,
-30, 0, 5, 2, // push center pawns away from original position
0, 0, 0, 0,
0, 0, 0, 0, //
};
int pawnEndPST[2][64] = {
0, 0, 0, 0, 0, 0, 0, 0, //
400, 400, 400, 400, 400, 400, 400, 400, // pawn near promotion
80, 80, 80, 80, 80, 80, 80, 80, //
30, 30, 30, 20, 20, 30, 30, 30, //
20, 20, 20, 20, 20, 20, 20, 20, //
15, 15, 15, 15, 15, 15, 15, 15, //
0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, //
};
int horsePST[2][64] = {
-90, -30, -30, -30, -30, -30, -30, -90, // away from corner to center
-40, -20, 15, 0, 0, 15, -20, -40, //
-30, 0, 20, 20, 20, 20, 0, -30, //
-30, 5, 15, 20, 20, 15, 5, -30, //
-30, 0, 15, 20, 20, 15, 0, -30, //
-30, 5, 20, 5, 5, 20, 5, -30, //
-40, -20, 0, 10, 10, 0, -20, -40, //
-90, -40, -30, -30, -30, -30, -40, -90, // away from corner to center
};
int bishopPST[2][64] = {
-30, -10, -10, -10, -10, -10, -10, -30, //
-10, 15, 0, 0, 0, 0, 15, -10, //
-10, 0, 5, 10, 10, 5, 0, -10, //
-10, 5, 10, 10, 10, 10, 5, -10, //
-10, 5, 10, 10, 10, 10, 5, -10, //
-10, 10, 10, 5, 5, 10, 10, -10, //
-10, 15, 10, 0, 0, 10, 15, -10, //
-30, -20, -20, -10, -10, -20, -20, -30, //
};
int rookPST[2][64] = {
-5, -5, 0, 0, 0, 0, -5, -5, //
-5, 10, 10, 10, 10, 10, 10, -5, // eat up pawns behind
-5, 0, 0, 0, 0, 0, 0, -5, //
-5, 0, 0, 0, 0, 0, 0, -5, //
-5, 0, 0, 0, 0, 0, 0, -5, //
0, 0, 0, 0, 0, 0, 0, 0, //
-5, 0, 5, 8, 8, 5, 0, -5, //
-5, -5, -5, 6, 6, 5, -5, -5, //
};
int kingPST[2][64] = {
-90, -90, -90, -90, -90, -90, -90, -90, //
-90, -90, -90, -90, -90, -90, -90, -90, //
-90, -90, -90, -90, -90, -90, -90, -90, //
-90, -90, -90, -90, -90, -90, -90, -90, //
-60, -60, -60, -60, -60, -60, -60, -60, //
-50, -50, -50, -40, -40, -40, -50, -50, //
-40, -30, -50, -40, -40, -50, -30, -40, //
-5, 5, -10, -20, -20, -20, 5, -5, //
-10, 7, 5, -30, -10, -10, 5, -16, // early king safety
};
BitBoard checkerBB;
inline bool colorOfSquare(int tile) const {
return (checkerBB.get() >> tile) & 1;
}
inline int value(const uint8_t &piece) const { // pseudo for move ordering
uint8_t pieceType = PIECES.type(piece);
if (pieceType == PIECES.PAWN) {
return 10; // simple pawn eval
} else if (pieceType == PIECES.KNIGHT) {
return 30; // simple knight eval
} else if (pieceType == PIECES.BISHOP) {
return 32; // simple bishop eval
} else if (pieceType == PIECES.ROOK) {
return 50; // simple rook eval
} else if (pieceType == PIECES.QUEEN) {
return 90; // simple queen eval
}
return 0;
}
// two colors // 6 types of PIECES
uint64_t zobristLookup[64][2][6];
uint64_t zobristCastling[16];
uint64_t zobristEnPassant[9]; // 8 columns and 1 for no enpassant
uint64_t blackTurnZobrist;
// generates random uint 64number controlled by seed
inline uint64_t RandUINT64(uint64_t seed) const {
std::mt19937_64 gen(seed); // Mersenne Twister algorithm
return gen();
}
bool inBounds(int tile) const { return tile >= 0 && tile < 64; };
PreComputedCache() { // very expensive constructor lol
int dfar = 0;
// pre compute moves
whiteKingSideCastle = Move(4, whiteKingCastleTo, MoveCache::CastleFlag);
whiteQueenSideCastle = Move(4, whiteQueenCastleTo, MoveCache::CastleFlag);
blackKingSideCastle = Move(60, blackKingCastleTo, MoveCache::CastleFlag);
blackQueenSideCastle = Move(60, blackQueenCastleTo, MoveCache::CastleFlag);
checkerBB.set(0x55aa55aa55aa55aa); // 0xaa55aa55aa55aa55 is just a checker
// board bitbaord all whites set
// piece square tables
uint64_t seedIncrement = 31; // key
for (int i = 0; i < 64; ++i) {
int row = (i / 8);
int col = (i % 8);
int i2 = ((7 - row) * 8) + (col);
pawnPST[1][i] = pawnPST[0][i2];
horsePST[1][i] = horsePST[0][i2];
bishopPST[1][i] = bishopPST[0][i2];
kingPST[1][i] = kingPST[0][i2];
rookPST[1][i] = rookPST[0][i2];
pawnEndPST[1][i] = pawnEndPST[1][i2];
// set the zobrist lookup
for (int t = 0; t < 6; ++t) {
++seedIncrement;
zobristLookup[i][0][t] = RandUINT64(seedIncrement);
++seedIncrement;
zobristLookup[i][1][t] = RandUINT64(seedIncrement);
}
}
// for castling
for (int i = 0; i < 16; ++i) {
++seedIncrement;
zobristCastling[i] = RandUINT64(seedIncrement);
}
// for en passant
for (int f = 0; f < 9; ++f) {
++seedIncrement;
zobristEnPassant[f] = RandUINT64(seedIncrement);
}
// for turn zobrist
++seedIncrement;
blackTurnZobrist = RandUINT64(seedIncrement);
for (int i = 0; i < 64; ++i) {
int row = (i / 8);
int col = (i % 8);
rowColValues[row][col] = i;
preComputedRows[i] = row;
preComputedCols[i] = col;
// for lookup notations
std::string notation = "";
notation += 'a' + col; // Convert fileNum to letter
notation += '1' + row; // Convert rankNum to number
notatedTiles[i] = notation;
// white pawns
if (row < 7) {
if (row == 6) {
wPawnMoves[i][4] = Move(i, i + 8, MoveCache::PromoteToQueenFlag);
wPawnMoves[i][5] = Move(i, i + 8, MoveCache::PromoteToRookFlag);
wPawnMoves[i][6] = Move(i, i + 8, MoveCache::PromoteToBishopFlag);
wPawnMoves[i][7] = Move(i, i + 8, MoveCache::PromoteToKnightFlag);
wPawnMoves[i][0] = Move(i, i + 8);
} else {
wPawnMoves[i][0] = Move(i, i + 8);
}
if (col < 7) { // capture
wPawnMoves[i][3] = Move(i, i + 9);
}
if (col > 0) { // capture
wPawnMoves[i][2] = Move(i, i + 7);
}
if (row == 1) {
wPawnMoves[i][1] = Move(i, i + 16, MoveCache::PawnTwoUpFlag);
}
}
// black pawns
if (row > 0) {
if (row == 1) {
bPawnMoves[i][4] = Move(i, i - 8, MoveCache::PromoteToQueenFlag);
bPawnMoves[i][5] = Move(i, i - 8, MoveCache::PromoteToRookFlag);
bPawnMoves[i][6] = Move(i, i - 8, MoveCache::PromoteToBishopFlag);
bPawnMoves[i][7] = Move(i, i - 8, MoveCache::PromoteToKnightFlag);
bPawnMoves[i][0] = Move(i, i - 8);
} else {
bPawnMoves[i][0] = Move(i, i - 8);
}
if (col < 7) { // capture
bPawnMoves[i][3] = Move(i, i - 7);
}
if (col > 0) { // capture
bPawnMoves[i][2] = Move(i, i - 9);
}
if (row == 6) {
bPawnMoves[i][1] = Move(i, i - 16, MoveCache::PawnTwoUpFlag);
}
}
// knights
// Knight move: 2 up, 1 left
if (row < 6 && col > 0) {
knightMoves[i][0] = Move(i, i + 15);
}
// Knight move: 2 up, 1 right
if (row < 6 && col < 7) {
knightMoves[i][1] = Move(i, i + 17);
}
// Knight move: 1 up, 2 left
if (row < 7 && col > 1) {
knightMoves[i][2] = Move(i, i + 6);
}
// Knight move: 1 up 2 right
if (row < 7 && col < 6) {
knightMoves[i][3] = Move(i, i + 10);
}
// Knight move: 1 down, 2 left
if (row > 0 && col > 1) {
knightMoves[i][4] = Move(i, i - 10);
}
// Knight move: 1 down, 2 right
if (row > 0 && col < 6) {
knightMoves[i][5] = Move(i, i - 6);
}
// Knight move: 2 down, 1 left
if (row > 1 && col > 0) {
knightMoves[i][6] = Move(i, i - 17);
}
// Knight move: 2 down, 1 right
if (row > 1 && col < 7) {
knightMoves[i][7] = Move(i, i - 15);
}
// bishops
dfar = 0;
if (col > 0) {
for (int j = 1; j < 8; ++j) { // up left col > 0
int destination = i + (j * directionOffsets[0]);
int dCol = destination % 8;
if (inBounds(destination) && dCol >= 0) {
bishopMoves[0][i][dfar] = Move(i, destination);
++dfar;
if (dCol == 0) {
break;
}
} else {
break;
}
}
}
dfar = 0;
if (col < 7) {
for (int j = 1; j < 8; ++j) { // up right col < 7
int destination = i + (j * directionOffsets[1]);
int dCol = destination % 8;
if (inBounds(destination) && dCol <= 7) {
bishopMoves[1][i][dfar] = Move(i, destination);
++dfar;
if (dCol == 7) {
break;
}
} else {
break;
}
}
}
dfar = 0;
if (col > 0) {
for (int j = 1; j < 8; ++j) { // down left col > 0
int destination = i + (j * directionOffsets[2]);
int dCol = destination % 8;
if (inBounds(destination) && dCol >= 0) {
bishopMoves[2][i][dfar] = Move(i, destination);
++dfar;
if (dCol == 0) {
break;
}
} else {
break;
}
}
}
dfar = 0;
if (col < 7) {
for (int j = 1; j < 8; ++j) { // down right col < 7
int destination = i + (j * directionOffsets[3]);
int dCol = destination % 8;
if (inBounds(destination) && dCol <= 7) {
bishopMoves[3][i][dfar] = Move(i, destination);
++dfar;
if (dCol == 7) {
break;
}
} else {
break;
}
}
}
// rooks
dfar = 0;
for (int j = 1; j < 8; ++j) { // up
int destination = i + (j * directionOffsets[4]);
if (inBounds(destination)) {
rookMoves[0][i][dfar] = Move(i, destination);
++dfar;
} else {
break;
}
}
dfar = 0;
for (uint8_t j = 1; j < 8; ++j) { // down
int destination = i + (j * directionOffsets[5]);
if (inBounds(destination)) {
rookMoves[1][i][dfar] = Move(i, destination);
++dfar;
} else {
break;
}
}
dfar = 0;
if (col > 0) {
for (uint8_t j = 1; j < 8; ++j) { // left
int destination = i + j * directionOffsets[6];
int dCol = destination % 8;
if (inBounds(destination) && dCol >= 0) {
rookMoves[2][i][dfar] = Move(i, destination);
++dfar;
if (dCol == 0) { // reached end of left
break;
}
} else {
break;
}
}
}
dfar = 0;
if (col < 7) {
for (uint8_t j = 1; j < 8; ++j) { // right
int destination = i + j * directionOffsets[7];
int dCol = destination % 8;
if (inBounds(destination) && dCol <= 7) {
rookMoves[3][i][dfar] = Move(i, destination);
++dfar;
if (dCol == 7) { // reached end of right
break;
}
} else {
break;
}
}
}
// king
// kings
// up
if (row < 7) {
kingMoves[i][0] = Move(i, i + 8);
}
// down
if (row > 0) {
kingMoves[i][1] = Move(i, i - 8);
}
// right
if (col > 0) {
kingMoves[i][2] = Move(i, i - 1);
}
// left
if (col < 7) {
kingMoves[i][3] = Move(i, i + 1);
}
// top right
if (col > 0 && row < 7) {
kingMoves[i][4] = Move(i, i + 7);
}
// top left
if (col < 7 && row < 7) {
kingMoves[i][5] = Move(i, i + 9);
}
// bottom right
if (col > 0 && row > 0) {
kingMoves[i][6] = Move(i, i - 9);
}
// bottom left
if (col < 7 && row > 0) {
kingMoves[i][7] = Move(i, i - 7);
}
}
for (int i = 0; i < 64; ++i) { // i is fromtile
for (int j = 0; j < 64; ++j) { // j is totile
int rowI = preComputedRows[i];
int colI = preComputedCols[i];
int rowJ = preComputedRows[j];
int colJ = preComputedCols[j];
distances[i][j] =
abs(rowI - rowJ) +
abs(colI - colJ) * 2; // distance of cols more significant cuz this
// is used for king/pawn evaluation
bool breakOut = false;
BitBoard &bb = rays[i][j];
BitBoard &bb2 = rays1Extra[i][j];
bb.setSquare(i);
// bb2.setSquare(i);
// things between
// orthos
if (breakOut == false) {
for (int d = 0; d < 4; ++d) {
for (int r = 0; r < 7; ++r) {
const Move addingMove = rookMoves[d][i][r];
if (!addingMove.isNull()) {
if (addingMove.moveTo() == j) {
breakOut = true;
if (r < 6 && !rookMoves[d][i][r + 1].isNull()) {
bb2.setSquare(rookMoves[d][i][r + 1].moveTo());
}
for (int r2 = r; r2 >= 0; --r2) {
bb2.setSquare(rookMoves[d][i][r2].moveTo());
bb.setSquare(rookMoves[d][i][r2].moveTo());
}
break;
}
}
}
if (breakOut) {
break;
}
}
}
// diags
if (breakOut == false) {
for (int d = 0; d < 4; ++d) {
for (int r = 0; r < 7; ++r) {
const Move addingMove = bishopMoves[d][i][r];
if (!addingMove.isNull()) {
if (addingMove.moveTo() == j) {
breakOut = true;
if (r < 6 && !bishopMoves[d][i][r + 1].isNull()) {
bb2.setSquare(bishopMoves[d][i][r + 1].moveTo());
}
for (int r2 = r; r2 >= 0; --r2) {
bb2.setSquare(bishopMoves[d][i][r2].moveTo());
bb.setSquare(bishopMoves[d][i][r2].moveTo());
}
break;
}
}
}
if (breakOut) {
break;
}
}
}
bb2.setSquare(j);
bb.setSquare(j);
}
}
};
};
// defining big lookup class
const PreComputedCache chessCache; // uses atleast like 0.9 MB
struct movePair {
int score = 0;
Move move;
};
struct moveList {
movePair moves[218]; // 218 max moves in chess
uint8_t amt = 0; // 255 limit
void addConstMove(const Move &m) {
if (!m.isNull()) {
moves[amt].move = m;
++amt;
}
}
};
// using uint8_t since doesnt need to store more than 255
class pieceList {
private: