-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchessposition.cpp
More file actions
804 lines (697 loc) · 19 KB
/
Copy pathchessposition.cpp
File metadata and controls
804 lines (697 loc) · 19 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
/*
MIT License
Copyright(c) 2016-2026 Judd Niemann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "chessposition.h"
#include "zobristkeyset.h"
#include "movegen.h"
namespace juddperft {
//////////////////////////////////////////////
// Implementation of Class ChessPosition //
//////////////////////////////////////////////
ChessPosition::ChessPosition()
{
A = 0;
B = 0;
C = 0;
D = 0;
flags = 0;
ChessPosition::hk = 0;
}
ChessPosition& ChessPosition::setupStartPosition()
{
flags = 0;
A = 0x4Aff00000000ff4A;
B = 0x3C0000000000003C;
C = 0xDB000000000000DB;
D = 0xffff000000000000;
blackCanCastle = 1;
whiteCanCastle = 1;
blackCanCastleLong = 1;
whiteCanCastleLong = 1;
blackForfeitedCastle = 0;
whiteForfeitedCastle = 0;
blackForfeitedCastleLong = 0;
whiteForfeitedCastleLong = 0;
blackDidCastle = 0;
whiteDidCastle = 0;
blackDidCastleLong = 0;
whiteDidCastleLong = 0;
moveNumber = 1;
halfMoves = 0;
calculateMaterial();
ChessPosition::calculateHash();
return *this;
}
ChessPosition & ChessPosition::calculateHash()
{
hk = 0;
for (unsigned int q = 0; q < 64; q++) {
piece_t piece = getPieceAtSquare(q);
if (piece & 0x7) {
ChessPosition::hk ^= zobristKeys.zkPieceOnSquare[piece][q];
}
}
if (blackToMove)
ChessPosition::hk ^= zobristKeys.zkBlackToMove;
if (whiteCanCastle)
ChessPosition::hk ^= zobristKeys.zkWhiteCanCastle;
if (whiteCanCastleLong)
ChessPosition::hk ^= zobristKeys.zkWhiteCanCastleLong;
if (blackCanCastle)
ChessPosition::hk ^= zobristKeys.zkBlackCanCastle;
if (blackCanCastleLong)
ChessPosition::hk ^= zobristKeys.zkBlackCanCastleLong;
return *this;
}
int ChessPosition::calculateMaterial() const
{
int material = 0;
for (int q = 0; q < 64; q++) {
material += pieceMaterialValue[getPieceAtSquare(q)];
}
return material;
}
ChessPosition& ChessPosition::performMove(const ChessMove& m)
{
Bitboard To = 1ull << m.destination;
const Bitboard O = ~((1ull << m.origin) | To);
const unsigned long nFromSquare = m.origin;
const unsigned long nToSquare = m.destination;
// if move is known to be delivering checkmate, immediately flag checkmate in the position
if (get_flag(m, checkmate) && m.blackToMove == static_cast<bool>(blackToMove)) {
if (m.blackToMove) {
whiteIsCheckmated = 1;
} else {
blackIsCheckmated = 1;
}
}
// clear any enpassant squares
const Bitboard EnPassant = A & B & ~C;
A &= ~EnPassant;
B &= ~EnPassant;
C &= ~EnPassant;
D &= ~EnPassant;
const unsigned long epSquareIdx = getSquareIndex(EnPassant);
hk ^= zobristKeys.zkPieceOnSquare[WENPASSANT][epSquareIdx]; // Remove EP from nEPSquare
switch (m.piece) {
case BKING:
if (get_flag(m, castle)) {
// APPLY CASTLING MOVES:
// we use magic XOR-tricks to do the job ! :
// The following is for O-O:
//
// K..R .RK.
// A: 1000 ^ 1010 = 0010
// B: 1000 ^ 1010 = 0010
// C: 1001 ^ 1111 = 0110
// For Black:
// D: 1001 ^ 1111 = 0110
// For White:
// D &= 0xfffffffffffffff0 (ie clear colour of affected squares from K to KR)
// The following is for O-O-O:
//
// R... K... ..KR ....
// A: 0000 1000 ^ 0010 1000 (0x28) = 0010 0000
// B: 0000 1000 ^ 0010 1000 (0x28) = 0010 0000
// C: 1000 1000 ^ 1011 1000 (0xB8) = 0011 0000
// For Black:
// D: 1000 1000 ^ 1011 1000 (0xB8) = 0011 0000
// For White:
// D &= 0xffffffffffffff07 (ie clear colour of affected squares from QR to K)
A ^= 0x0a00000000000000;
B ^= 0x0a00000000000000;
C ^= 0x0f00000000000000;
D ^= 0x0f00000000000000;
hk ^= zobristKeys.zkDoBlackCastle;
if (blackCanCastleLong) {
hk ^= zobristKeys.zkBlackCanCastleLong; // flip black castling long
}
blackDidCastle = 1;
blackCanCastle = 0;
blackCanCastleLong = 0;
return *this;
}
else if (get_flag(m, castleLong)) {
A ^= 0x2800000000000000;
B ^= 0x2800000000000000;
C ^= 0xb800000000000000;
D ^= 0xb800000000000000;
hk ^= zobristKeys.zkDoBlackCastleLong;
if (blackCanCastle) {
hk ^= zobristKeys.zkBlackCanCastle; // conditionally flip black castling
}
blackDidCastleLong = 1;
blackCanCastle = 0;
blackCanCastleLong = 0;
return *this;
}
// ordinary king move; Black could have castled,
// but chose to move the King in a non-castling move
if (blackCanCastle) {
hk ^= zobristKeys.zkBlackCanCastle; // flip black castling
}
if (blackCanCastleLong) {
hk ^= zobristKeys.zkBlackCanCastleLong; // flip black castling long
}
blackForfeitedCastle = 1;
blackForfeitedCastleLong = 1;
blackCanCastle = 0;
blackCanCastleLong = 0;
break;
case WKING:
if (get_flag(m, castle)) {
A ^= 0x000000000000000a;
B ^= 0x000000000000000a;
C ^= 0x000000000000000f;
D &= 0xfffffffffffffff0; // clear colour of e1, f1, g1, h1 (make white)
hk ^= zobristKeys.zkDoWhiteCastle;
if (whiteCanCastleLong) {
hk ^= zobristKeys.zkWhiteCanCastleLong; // conditionally flip white castling long
}
whiteDidCastle = 1;
whiteCanCastle = 0;
whiteCanCastleLong = 0;
return *this;
}
else if (get_flag(m, castleLong)) {
A ^= 0x0000000000000028;
B ^= 0x0000000000000028;
C ^= 0x00000000000000b8;
D &= 0xffffffffffffff07; // clear colour of a1, b1, c1, d1, e1 (make white)
hk ^= zobristKeys.zkDoWhiteCastleLong;
if (whiteCanCastle) {
hk ^= zobristKeys.zkWhiteCanCastle; // conditionally flip white castling
}
whiteDidCastleLong = 1;
whiteCanCastle = 0;
whiteCanCastleLong = 0;
return *this;
}
// ordinary king move; White could have castled,
// but chose to move the King in a non-castling move
if (whiteCanCastle) {
hk ^= zobristKeys.zkWhiteCanCastle; // flip white castling
}
if (whiteCanCastleLong) {
hk ^= zobristKeys.zkWhiteCanCastleLong; // flip white castling long
}
whiteForfeitedCastle = 1;
whiteCanCastle = 0;
whiteForfeitedCastleLong = 1;
whiteCanCastleLong = 0;
break;
case BROOK:
if (nFromSquare == SquareIndex::h8) {
// Black moved K-side Rook and forfeits right to castle K-side
if (blackCanCastle){
blackForfeitedCastle = 1;
hk ^= zobristKeys.zkBlackCanCastle; // flip black castling
blackCanCastle = 0;
}
} else if (nFromSquare == SquareIndex::a8) {
// Black moved the QS Rook and forfeits right to castle Q-side
if (blackCanCastleLong) {
blackForfeitedCastleLong = 1;
hk ^= zobristKeys.zkBlackCanCastleLong; // flip black castling long
blackCanCastleLong = 0;
}
}
break;
case WROOK:
if (nFromSquare == SquareIndex::h1) {
// White moved K-side Rook and forfeits right to castle K-side
if (whiteCanCastle) {
whiteForfeitedCastle = 1;
hk ^= zobristKeys.zkWhiteCanCastle; // flip white castling BROKEN !!!
whiteCanCastle = 0;
}
} else if (nFromSquare == SquareIndex::a1) {
// White moved the QSide Rook and forfeits right to castle Q-side
if (whiteCanCastleLong) {
whiteForfeitedCastleLong = 1;
hk ^= zobristKeys.zkWhiteCanCastleLong; // flip white castling long
whiteCanCastleLong = 0;
}
}
break;
default:
break;
} // ends switch (M.Piece)
// Ordinary Captures
if (get_flag(m, capture)) {
// find out what was captured
const unsigned int capturedpiece =
((D & To) >> nToSquare) << 3
| ((C & To) >> nToSquare) << 2
| ((B & To) >> nToSquare) << 1
| ((A & To) >> nToSquare);
// Update Hash
hk ^= zobristKeys.zkPieceOnSquare[capturedpiece][nToSquare]; // Remove captured Piece
// if a rook was captured, it may take away castling rights
if (To & CORNERS) {
switch (capturedpiece) {
case WROOK:
if (whiteCanCastle && (To & H1)) {
hk ^= zobristKeys.zkWhiteCanCastle;
whiteCanCastle = 0;
} else if (whiteCanCastleLong && (To & A1)) {
hk ^= zobristKeys.zkWhiteCanCastleLong;
whiteCanCastleLong = 0;
}
break;
case BROOK:
if (blackCanCastle && (To & H8)){
hk ^= zobristKeys.zkBlackCanCastle;
blackCanCastle = 0;
} else if (blackCanCastleLong && (To & A8)) {
hk ^= zobristKeys.zkBlackCanCastleLong;
blackCanCastleLong = 0;
}
default:
break;
}
}
}
// Render "ordinary" moves:
A &= O;
B &= O;
C &= O;
D &= O;
// Populate new square (Branchless method):
A |= static_cast<int64_t>(m.piece & 1) << m.destination;
B |= static_cast<int64_t>((m.piece & 2) >> 1) << m.destination;
C |= static_cast<int64_t>((m.piece & 4) >> 2) << m.destination;
D |= static_cast<int64_t>((m.piece & 8) >> 3) << m.destination;
// Update Hash
hk ^= zobristKeys.zkPieceOnSquare[m.piece][nFromSquare]; // Remove piece at From square
hk ^= zobristKeys.zkPieceOnSquare[m.piece][nToSquare]; // Place piece at To Square
if ((m.piece & 7) == WPAWN) {
// For double-pawn moves, set EP square:
if (get_flag(m, doublePawnMove)) {
// Set EnPassant Square
if (m.blackToMove) {
To <<= 8;
A |= To;
B |= To;
C &= ~To;
D |= To;
hk ^= zobristKeys.zkPieceOnSquare[BENPASSANT][nToSquare + 8]; // Place Black EP at (To+8)
} else {
To >>= 8;
A |= To;
B |= To;
C &= ~To;
D &= ~To;
hk ^= zobristKeys.zkPieceOnSquare[WENPASSANT][nToSquare - 8]; // Place White EP at (To-8)
}
return *this;
}
// En-Passant Captures
if (get_flag(m, enPassantCapture)) {
// remove the actual pawn (it is different to the capture square)
if (m.blackToMove) {
To <<= 8;
A &= ~To; // clear the pawn's square
B &= ~To;
C &= ~To;
D &= ~To;
hk ^= zobristKeys.zkPieceOnSquare[WPAWN][nToSquare + 8]; // Remove WHITE Pawn at (To+8)
} else {
To >>= 8;
A &= ~To;
B &= ~To;
C &= ~To;
D &= ~To;
hk ^= zobristKeys.zkPieceOnSquare[BPAWN][nToSquare - 8]; // Remove BLACK Pawn at (To-8)
}
return *this;
}
// Promotions - Change the piece:
if (get_flag(m, promoteQueen)) {
A &= ~To;
B |= To;
C |= To;
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BPAWN : WPAWN)][nToSquare]; // Remove pawn at To square
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BQUEEN : WQUEEN)][nToSquare]; // place Queen at To square
return *this;
}
if (get_flag(m, promoteKnight)) {
C |= To;
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BPAWN : WPAWN)][nToSquare]; // Remove pawn at To square
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BKNIGHT : WKNIGHT)][nToSquare]; // place Knight at To square
return *this;
}
if (get_flag(m, promoteBishop)) {
A &= ~To;
B |= To;
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BPAWN : WPAWN)][nToSquare]; // Remove pawn at To square
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BBISHOP : WBISHOP)][nToSquare]; // place Bishop at To square
return *this;
}
if (get_flag(m, promoteRook)) {
A &= ~To;
C |= To;
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BPAWN : WPAWN)][nToSquare]; // Remove pawn at To square
hk ^= zobristKeys.zkPieceOnSquare[(m.blackToMove ? BROOK : WROOK)][nToSquare]; // place Rook at To square
return *this;
}
}
return *this;
}
ChessPosition& ChessPosition::performMoveNoHash(const ChessMove& m)
{
Bitboard To = 1ull << m.destination;
const Bitboard O = ~((1ull << m.origin) | To);
const unsigned long nFromSquare = m.origin;
// if move is known to be delivering checkmate, immediately flag checkmate in the position
if (get_flag(m, checkmate) && m.blackToMove == static_cast<bool>(blackToMove)) {
if (m.blackToMove) {
whiteIsCheckmated = 1;
} else {
blackIsCheckmated = 1;
}
}
// clear any enpassant squares
const Bitboard EnPassant = A & B & ~C;
A &= ~EnPassant;
B &= ~EnPassant;
C &= ~EnPassant;
D &= ~EnPassant;
switch (m.piece) {
case BKING:
if (get_flag(m, castle)) {
A ^= 0x0a00000000000000;
B ^= 0x0a00000000000000;
C ^= 0x0f00000000000000;
D ^= 0x0f00000000000000;
blackDidCastle = 1;
blackCanCastle = 0;
blackCanCastleLong = 0;
return *this;
}
else if (get_flag(m, castleLong)) {
A ^= 0x2800000000000000;
B ^= 0x2800000000000000;
C ^= 0xb800000000000000;
D ^= 0xb800000000000000;
blackDidCastleLong = 1;
blackCanCastle = 0;
blackCanCastleLong = 0;
return *this;
}
blackForfeitedCastle = 1;
blackForfeitedCastleLong = 1;
blackCanCastle = 0;
blackCanCastleLong = 0;
break;
case WKING:
if (get_flag(m, castle)) {
A ^= 0x000000000000000a;
B ^= 0x000000000000000a;
C ^= 0x000000000000000f;
D &= 0xfffffffffffffff0; // clear colour of e1, f1, g1, h1 (make white)
whiteDidCastle = 1;
whiteCanCastle = 0;
whiteCanCastleLong = 0;
return *this;
}
else if (get_flag(m, castleLong)) {
A ^= 0x0000000000000028;
B ^= 0x0000000000000028;
C ^= 0x00000000000000b8;
D &= 0xffffffffffffff07; // clear colour of a1, b1, c1, d1, e1 (make white)
whiteDidCastleLong = 1;
whiteCanCastle = 0;
whiteCanCastleLong = 0;
return *this;
}
whiteForfeitedCastle = 1;
whiteCanCastle = 0;
whiteForfeitedCastleLong = 1;
whiteCanCastleLong = 0;
break;
case BROOK:
if (nFromSquare == SquareIndex::h8) {
// Black moved K-side Rook and forfeits right to castle K-side
if (blackCanCastle){
blackForfeitedCastle = 1;
blackCanCastle = 0;
}
} else if (nFromSquare == SquareIndex::a8) {
// Black moved the QS Rook and forfeits right to castle Q-side
if (blackCanCastleLong) {
blackForfeitedCastleLong = 1;
blackCanCastleLong = 0;
}
}
break;
case WROOK:
if (nFromSquare == SquareIndex::h1) {
// White moved K-side Rook and forfeits right to castle K-side
if (whiteCanCastle) {
whiteForfeitedCastle = 1;
whiteCanCastle = 0;
}
} else if (nFromSquare == SquareIndex::a1) {
// White moved the QSide Rook and forfeits right to castle Q-side
if (whiteCanCastleLong) {
whiteForfeitedCastleLong = 1;
whiteCanCastleLong = 0;
}
}
break;
default:
break;
} // ends switch (M.Piece)
// Ordinary Captures
if (get_flag(m, capture)) {
// find out what was captured
const unsigned int capturedpiece
= ((D & To) >> m.destination) << 3
| ((C & To) >> m.destination) << 2
| ((B & To) >> m.destination) << 1
| ((A & To) >> m.destination);
// if a rook was captured, it may take away castling rights
if (To & CORNERS) {
switch (capturedpiece) {
case WROOK:
if (whiteCanCastle && (To & H1)) {
whiteCanCastle = 0;
} else if (whiteCanCastleLong && (To & A1)) {
whiteCanCastleLong = 0;
}
break;
case BROOK:
if (blackCanCastle && (To & H8)){
blackCanCastle = 0;
} else if (blackCanCastleLong && (To & A8)) {
blackCanCastleLong = 0;
}
default:
break;
}
}
}
// Render "ordinary" moves:
A &= O;
B &= O;
C &= O;
D &= O;
// Populate new square (Branchless method):
A |= static_cast<int64_t>(m.piece & 1) << m.destination;
B |= static_cast<int64_t>((m.piece & 2) >> 1) << m.destination;
C |= static_cast<int64_t>((m.piece & 4) >> 2) << m.destination;
D |= static_cast<int64_t>((m.piece & 8) >> 3) << m.destination;
if ((m.piece & 7) == WPAWN) {
// For double-pawn moves, set EP square:
if (get_flag(m, doublePawnMove)) {
// Set EnPassant Square
if (m.blackToMove) {
To <<= 8;
A |= To;
B |= To;
C &= ~To;
D |= To;
} else {
To >>= 8;
A |= To;
B |= To;
C &= ~To;
D &= ~To;
}
return *this;
}
// En-Passant Captures
if (get_flag(m, enPassantCapture)) {
// remove the actual pawn (it is different to the capture square)
if (m.blackToMove) {
To <<= 8;
A &= ~To; // clear the pawn's square
B &= ~To;
C &= ~To;
D &= ~To;
} else {
To >>= 8;
A &= ~To;
B &= ~To;
C &= ~To;
D &= ~To;
}
return *this;
}
// Promotions - Change the piece:
if (get_flag(m, promoteQueen)) {
A &= ~To;
B |= To;
C |= To;
return *this;
}
if (get_flag(m, promoteKnight)) {
C |= To;
return *this;
}
if (get_flag(m, promoteBishop)) {
A &= ~To;
B |= To;
return *this;
}
if (get_flag(m, promoteRook)) {
A &= ~To;
C |= To;
return *this;
}
}
return *this;
}
std::vector<ChessMove> ChessPosition::getLegalMoves() const
{
std::vector<ChessMove> movelist(MOVELIST_SIZE);
MoveGenerator::generateMoves(*this, movelist.data());
movelist.resize(move_count(movelist.at(0)));
return movelist;
}
void ChessPosition::getLegalMoves(ChessMove *movelist) const
{
if (movelist) {
MoveGenerator::generateMoves(*this, movelist);
}
}
void ChessPosition::switchSides()
{
blackToMove ^= 1;
hk ^= zobristKeys.zkBlackToMove;
return;
}
void ChessPosition::clear(void)
{
A = B = C = D = 0;
flags = 0;
hk = 0;
}
// Text Print functions //////////////////
void ChessPosition::printBitboard(Bitboard b)
{
std::cout << "\n";
for (int q = 63; q >= 0; q--)
{
if ((b & (1ull << q)) != 0)
{
std::cout << ("o ");
}
else
{
std::cout << (". ");
}
if ((q % 8) == 0)
{
std::cout << ("\n");
}
}
std::cout << std::flush;
}
void ChessPosition::printPosition() const
{
printf("\n---------------------------------\n");
for (int q = 63; q >= 0; q--)
{
switch(getPieceAtSquare(q))
{
case WPAWN:
printf("| P ");
break;
case WBISHOP:
printf("| B ");
break;
case WROOK:
printf("| R ");
break;
case WQUEEN:
printf("| Q ");
break;
case WKING:
printf("| K ");
break;
case WKNIGHT:
printf("| N ");
break;
case WENPASSANT:
printf("| EP");
break;
case BPAWN:
printf("| p ");
break;
case BBISHOP:
printf("| b ");
break;
case BROOK:
printf("| r ");
break;
case BQUEEN:
printf("| q ");
break;
case BKING:
printf("| k ");
break;
case BKNIGHT:
printf("| n ");
break;
case BENPASSANT:
printf("| ep");
break;
default:
printf("| ");
}
if ((q % 8) == 0)
{
printf("| ");
if ((q == 56) && (blackCanCastle))
printf("Black can Castle");
if ((q == 48) && (blackCanCastleLong))
printf("Black can Castle Long");
if ((q == 40) && (whiteCanCastle))
printf("White can Castle");
if ((q == 32) && (whiteCanCastleLong))
printf("White can Castle Long");
if (q == 8) printf("material= %d", calculateMaterial());
if (q == 0) printf("%s to move", blackToMove ? "Black" : "White");
printf("\n---------------------------------\n");
}
}
}
} // namespace juddperft