-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.js
1485 lines (1336 loc) · 62.1 KB
/
chess.js
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
import {
FILE_MASKS,
NO_CELL,
NO_CELL_I,
idToIndex,
CELL_I_E1,
CELL_I_C1,
CELL_I_G1,
CELL_I_E8,
CELL_N_A1,
CELL_N_D1,
CELL_N_H1,
CELL_N_F1,
CELL_I_C8,
CELL_N_A8,
CELL_N_D8,
CELL_I_G8,
CELL_N_H8,
CELL_N_F8,
RANK_3,
RANK_7,
RANK_MASKS,
indexToId,
CELL_I_F8,
CELL_I_F1, CELL_I_H1, CELL_I_A1, CELL_I_A8, CELL_I_H8, CELL_I_D1, CELL_I_D8
} from './constants.js';
import {whitePawnsThatCanCaptureOn, blackPawnsThatCanCaptureOn, whitePawnsThatCanMoveTo, blackPawnsThatCanMoveTo } from './pawns.js';
import {singleBitToIndex} from './single-bit.js';
import {getFenishString, getFenString} from './serialize.js';
import {knightMoves} from './knights.js';
import {kingMoves} from './kings.js';
import {$$, $$$, assert} from './utils.js';
import {displayHistory, moveHistoryPointer, reflectHistory} from './history.js';
// Some inspiration: https://www.chessprogramming.org/Bitboards
/**
* @param G {Game}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param fromn {BigInt} A single bit is set, representing the field to move from
* @param ton {BigInt} A single bit is set, representing the field to move to
*/
export function makeMove(G, fromi, toi, fromn = 1n << fromi, ton = 1n << toi) {
// This function only actually moves a piece from one cell to another and updates state accordingly.
// Note: the move may be an invalid chess move (!).
// Note: result will not apply pawn promotion (see makeCompleteMove for that)
// Castling and en-passant is handled explicitly
// The result should be able to be used for "is checked" validations
const isWhite = G.white & fromn;
const clearToFrom = BigInt.asUintN(64, ~(fromn | ton));
if ((G.pawns & fromn) || ((G.white | G.black) & ton)) G.fiftyTurnCounter = 0;
else G.fiftyTurnCounter += 1;
const isCapture = (G.white | G.black) & ton;
const isPawnMove = G.pawns & fromn;
if (isWhite) {
G.black &= clearToFrom;
G.white = (G.white & clearToFrom) | ton;
} else {
G.white &= clearToFrom;
G.black = (G.black & clearToFrom) | ton;
}
// Whatever the state, switch the turn to the opponent color.
G.turnWhite = !isWhite;
// Have to confirm whether this is castling because it requires a slightly different board update.
// There are four cases (two sides on each side of the board) and must be a king.
// In particular, the rook from that side needs to be put to the other side of the new position of the king.
if (G.kings & fromn) {
// Note: we only need to update the rooks (and opp color) here. General move logic will move the king.
if (isWhite && fromi === CELL_I_E1) {
if (toi === CELL_I_C1) {
// Queen side
G.white |= CELL_N_A1 | CELL_N_D1;
G.white ^= CELL_N_A1;
G.rooks |= CELL_N_A1 | CELL_N_D1;
G.rooks ^= CELL_N_A1;
} else if (toi === CELL_I_G1) {
// King side
G.white |= CELL_N_H1 | CELL_N_F1;
G.white ^= CELL_N_H1;
G.rooks |= CELL_N_H1 | CELL_N_F1;
G.rooks ^= CELL_N_H1;
} else {
// Either way, this king moved so it burned their opp to castle
G.castleKingsideWhite = false;
G.castleQueensideWhite = false;
}
} else if (!isWhite && fromi === CELL_I_E8) {
// Note: have not explicitly asserted black to occupy e8, only that white
// did not, so we explicitly set it first before the toggle (-> xor)
if (toi === CELL_I_C8) {
// Queen side
G.black |= CELL_N_A8 | CELL_N_D8;
G.black ^= CELL_N_A8;
G.rooks |= CELL_N_A8 | CELL_N_D8;
G.rooks ^= CELL_N_A8;
} else if (toi === CELL_I_G8) {
// King side
G.black |= CELL_N_H8 | CELL_N_F8;
G.black ^= CELL_N_H8;
G.rooks |= CELL_N_H8 | CELL_N_F8;
G.rooks ^= CELL_N_H8;
} else {
// Either way, this king moved so it burned their opp to castle
G.castleKingsideBlack = false;
G.castleQueensideBlack = false;
}
}
}
// If you move a rook, you can't castle that side
if (G.rooks & fromn) {
if (isWhite) {
if (fromi === CELL_I_A1) G.castleQueensideWhite = false;
else if (fromi === CELL_I_H1) G.castleKingsideWhite = false;
} else {
if (fromi === CELL_I_A8) G.castleQueensideBlack = false;
else if (fromi === CELL_I_H8) G.castleKingsideBlack = false;
}
}
// Check for en passant when capturing with pawn.
// We could assert that an empty capture implies en passant capture. But it's too ugly and hacky.
if (isPawnMove && !isCapture) {
const dx = (fromi - toi) % 8n;
let removePasser = true;
if (fromi + 1n === G.enpassant && dx === -1n) {
//console.log('capturing en-passant up-left');
} else if (fromi - 1n === G.enpassant && dx === -7n) {
//console.log('capturing en-passant up-right');
} else if (fromi + 1n === G.enpassant && dx === 7n) {
//console.log('capturing en-passant down-left');
} else if (fromi - 1n === G.enpassant && dx === 1n) {
//console.log('capturing en-passant down-right');
} else {
// Not capturing en passant
removePasser = false;
}
if (removePasser) {
// Must remove the pawn that tried to pass
const epn = 1n << G.enpassant;
G.pawns = (G.pawns | epn) ^ epn;
if (isWhite) G.black = (G.black | epn) ^ epn;
else G.white = (G.white | epn) ^ epn;
}
}
// Track a pawn "two square advance", moving two cells from any starting position, if so store it in the en-passant state
G.enpassant = NO_CELL_I;
if (isPawnMove) {
if (isStartingEnPassant(fromi, toi)) {
// (We don't need to store the pawn for black and white at the same time so use same storage for them)
G.enpassant = toi;
}
}
// Check which piece is being moved and then unset the `from` cell and set the `to` cell for that state
G.kings = (G.kings & fromn ? ton : 0n) | (G.kings & clearToFrom);
G.queens = (G.queens & fromn ? ton : 0n) | (G.queens & clearToFrom);
G.rooks = (G.rooks & fromn ? ton : 0n) | (G.rooks & clearToFrom);
G.knights = (G.knights & fromn ? ton : 0n) | (G.knights & clearToFrom);
G.bishops = (G.bishops & fromn ? ton : 0n) | (G.bishops & clearToFrom);
G.pawns = (G.pawns & fromn ? ton : 0n) | (G.pawns & clearToFrom);
}
/**
* @param G {Game}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param fromn {BigInt} A single bit is set, representing the field to move from
* @param ton {BigInt} A single bit is set, representing the field to move to
* @param promotionDefault {'' | 'queen' | 'rook' | 'knight' | 'bishop'}
*/
export function makeCompleteMove(G, fromi, toi, fromn = 1n << fromi, ton = 1n << toi, promotionDefault = '', updateThreefold = true) {
const isWhite = G.white & fromn;
const wasWhiteTurn = G.turnWhite;
const isPawnMove = G.pawns & fromn;
if (isWhite && !wasWhiteTurn) G.fullTurnCounter += 1; // Illegal but if mode is enabled, back to back same color moves should increment counter too
makeMove(G, fromi, toi, fromn, ton);
// Track pawn promotion
if (isPawnMove) {
// We need to apply promotion when a pawn reaches the back rank. The `from` does not matter.
if (isWhite ? (toi >= 56n && toi <= 63n) : (toi >= 0n && toi <= 7n)) {
// it was a beautiful butterfly
if (promotionDefault) {
// Leave the color, scrub the pawn from the `toi` cell, add a <piece> to the `toi` cell. We know it was set above.
G.pawns ^= ton;
if (promotionDefault === 'queen') G.queens |= ton;
else if (promotionDefault === 'rook') G.rooks |= ton;
else if (promotionDefault === 'knight') G.knights |= ton;
else if (promotionDefault === 'bishop') G.bishops |= ton;
else FIXME;
} else {
// Look, this was never meant to be a full fledged board. What can I say.
console.log('Going to crash now because there is no promotion popup and no default piece was given. oopsie.');
TODO // ask.
}
} else {
// Not promotion
}
}
if (!isWhite) G.fullTurnCounter += 1; // A "whole" turn is both players. The full move counter flips after every move by black.
if (updateThreefold) recordBoardState(G);
G.prevFrom = fromi;
G.prevTo = toi;
}
/**
* @param L {LocalState}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param fromn {BigInt} A single bit is set, representing the field to move from
* @param ton {BigInt} A single bit is set, representing the field to move to
* @param promotionDefault {'' | 'queen' | 'rook' | 'knight' | 'bishop'}
*/
export function makeCompleteMoveIncHistory(L, fromi, toi, fromn = 1n << fromi, ton = 1n << toi, promotionDefault = '') {
// console.log('-->', L.history.index, L.history.moves[L.history.index + 1])
// const prev = L.history.moves[L.history.index + 1];
const next = L.history.moves[L.history.index + 1];
if (next?.from === indexToId[fromi] && next?.to === indexToId[toi]) {
// Same move as next in the move list; just move the pointer...
console.log('Only moving history');
reflectHistory(L, 1);
} else {
L.history.moves.length = L.history.index + 1;
const fromWhiteCell = (L.G.white & fromn) > 0n;
makeCompleteMove(L.G, fromi, toi, fromn, ton, promotionDefault);
// Note: the full turn is flaky when moving the same color twice (in particular for white). Black always flips after the move so we subtract one.
const move = {turn: L.G.fullTurnCounter - (fromWhiteCell ? 0 : 1), fen: getFenString(L.G), white: fromWhiteCell, from: indexToId[fromi], to: indexToId[toi], piece: '?', an: '??'};
L.history.moves.push(move);
moveHistoryPointer(L, 1);
displayHistory(L);
}
}
/**
* @param {Game} G
* @param {boolean} forWhite
* @param {string} piece
* @param {string} tos
* @param {string} fromFile
* @param {string} fromRank
* @param {boolean} [debug]
* @returns {{i: BigInt, n: BigInt}}
*/
export function findSourceCellFromPgnMove(G, forWhite, piece, tos, fromFile, fromRank, debug = false) {
if (debug) console.log('findSourceCellFromPgnMove:', 'white:', forWhite, ', to:', tos, ', file hint:', fromFile, ', rank hint:', fromRank)
// Given a game move relative to the current state of the game (PGN), resolve the source cell from which the move starts.
// More complicated than it needs to be.
// http://www6.chessclub.com/help/PGN-spec -> pgn viewers must be worst case pedantic. If an ambiguity arises with one of the two pieces pinned (unable to capture), it is not considered ambiguous.
// -> https://www.chessclub.com/help/PGN-spec
// https://chess.stackexchange.com/questions/1864/is-this-case-considered-an-ambiguity-or-not
// https://chess.stackexchange.com/questions/1817/how-are-pgn-ambiguities-handled
/**
* @type {BigInt}
*/
const turnMask = forWhite ? G.white : G.black;
/**
* @type {BigInt}
*/
const oppoMask = forWhite ? G.black : G.white;
const toi = idToIndex[tos];
const ton = 1n << toi;
switch (piece) {
case 'P': {
// Find the pawn of given color that can move to given coordinate. Have to check moves and captures. Only assume disambiguation when there are actually more than one possible.
const pawns = G.pawns & turnMask;
if (debug) console.log(forWhite ? 'White' : 'Black', 'pawns:', $$(pawns));
// If the target cell is taken then it must be the opponent (for otherwise the move is illegal)
// If the cell is taken, we only consider possible attacking pawns. Otherwise we only consider possible moving pawns.
const capturing = oppoMask & ton;
if (debug) console.log('To cell taken?', Boolean(capturing), $$(capturing));
const eligible = (forWhite ? (capturing ? whitePawnsThatCanCaptureOn[toi] : whitePawnsThatCanMoveTo[toi]) : (capturing ? blackPawnsThatCanCaptureOn[toi] : blackPawnsThatCanMoveTo[toi]));
if (debug) console.log('Eligibility mask:', $$(eligible));
const candidates = eligible & pawns;
if (debug) console.log('Candidate', forWhite ? 'white' : 'black', 'pawns:', $$(candidates), ', for a', capturing ? 'capture' : 'move');
// We now have at most two pawns to choose from.
// - When there's one pawn, that's the one we must use, period
// - When moving, we simply take the pawn closest to the target cell, period.
// - If not a single bit result then mask with the third or sixth rank to confirm that it's occupied. That should tell us enough.
// - When capturing, the fromFile should be set so we don't need to disambiguate any further
if (capturing) {
assert(Boolean(fromFile), true, 'pawn capturing should have a fromFile annotation');
const fileMask = FILE_MASKS[fromFile.toLowerCase()];
if (debug) console.log('fileMask:', $$(fileMask));
assert(Boolean(fileMask !== undefined), true, 'the from file should be a-h');
const target = candidates & fileMask;
if (debug) console.log('target:', $$(target));
const index = singleBitToIndex.get(target);
if (debug) console.log('index:', $$(index));
assert(Boolean(index !== undefined), true, 'should now have a single bit');
return {i: index, n: target};
} else {
const maybeTarget = singleBitToIndex.get(candidates);
if (debug) console.log('Single bit index of', candidates, ':', maybeTarget);
if (maybeTarget === undefined) {
// So this is a doubled pawn on the second and third rank and the third rank is moving forward. Get the third rank.
const rank = forWhite ? RANK_3 : RANK_7;
const target = candidates & rank;
if (debug) console.log('doubled pawn:', $$(candidates), '\nand', $$(rank), '\nis', $$(target))
const index = singleBitToIndex.get(target);
assert(Boolean(index !== undefined), true, 'should now have a single bit');
return {i: index, n: target};
} else {
return {i: maybeTarget, n: candidates};
}
}
}
case 'K': {
// In general a player only ever has one king and there is no legal way of obtaining more than one.
// If the game does have multiple, we should try to just move the first one, or if there's a "from" coordinate, just use that verbatim.
if (debug) console.log('turnmask', $$(turnMask))
let kings = G.kings & turnMask;
if (debug) console.log('a', $$(kings))
const only = singleBitToIndex.get(kings);
if (debug) console.log('b', $$(only))
if (only !== undefined) {
// This is the common case. Anything else is not an actual game of chess.
return {i: only, n: kings};
}
// Try to reduce the number of kings by looking at realistic valid moves
kings = kings & kingMoves[toi];
if (debug) console.log('step1:', $$(kings));
if (fromFile) kings &= FILE_MASKS[fromFile.toLowerCase()];
if (debug) console.log('step2:', $$(kings));
if (fromRank) kings &= RANK_MASKS[fromRank.toLowerCase()];
if (debug) console.log('step3:', $$(kings));
const filtered = singleBitToIndex.get(kings);
if (debug) console.log('step4:', $$(filtered));
if (filtered !== undefined) {
return {i: filtered, n: kings};
}
throw new Error('No single king to move...? Giving up.');
}
case 'Q': {
// There can be multiple queens after promotion
let queens = G.queens & turnMask;
const only = singleBitToIndex.get(queens);
if (debug) console.log('Queens:', $$(queens), 'only:', only);
if (only !== undefined) {
// This is the common case.
return {i: only, n: queens};
}
// Try to apply the disambiguation hints
if (fromFile) queens &= FILE_MASKS[fromFile.toLowerCase()];
if (fromRank) queens &= RANK_MASKS[fromRank.toLowerCase()];
const filtered = singleBitToIndex.get(queens);
if (debug) console.log('Queens filtered:', $$(queens), 'only:', filtered);
if (filtered !== undefined) {
return {i: filtered, n: queens};
}
// Despite disambiguation there are still multiple queen candidates left. Eliminate queens that are blocked or even pinned.
// For each, check if the move would be valid. Discard queens that are invalid (like if they're pinned to the king).
// After that we should have one queen left... We optimistically bail when we have one queen left (and assume the move is legit)
for (let i=0n; i<64n; ++i) {
const n = (1n << i);
if (queens & n) {
if (debug) console.log('Considering queen moving from', indexToId[i], 'to', indexToId[toi], $$(queens));
const can = canQueenMove(G, i, toi, n, ton);
if (debug) console.log('- can:', can);
if (can !== 'ok') { // expensive
if (can === 'blocked') if (debug) console.log('- This queen is blocked from moving to to', indexToId[toi]);
else if (debug) console.log('- It is not valid for this queen to move to', indexToId[toi]);
// Remove
queens ^= n;
if (debug) console.log('- After discarding this queen:', $$(queens));
// Check if we have one queen _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
const filtered = singleBitToIndex.get(queens);
if (filtered !== undefined) {
return {i: filtered, n: queens};
}
continue;
}
const {state, byn} = doesMoveLeaveChecked(G, forWhite, i, toi, n, ton); // expensive
if (state === 'checked') {
if (debug) console.log('- This queen cannot move from', indexToId[i], 'to', indexToId[toi], 'because it would leave its king checked (byn=', byn, singleBitToIndex.get(byn), indexToId[singleBitToIndex.get(byn)], ')');
if (debug) console.log('Discarding', $$(n))
// Remove
queens ^= n;
if (debug) console.log('- After discarding this queen:', $$(queens));
// Check if we have one queen _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
const filtered = singleBitToIndex.get(queens);
if (filtered !== undefined) {
return {i: filtered, n: queens};
}
}
}
}
throw new Error('No single queen to move...?');
}
case 'R': {
let rooks = G.rooks & turnMask;
const only = singleBitToIndex.get(rooks);
if (debug) console.log('Rooks:', $$(rooks), 'only:', only);
if (only !== undefined) {
// This is a common case in the endgame
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: only, n: rooks};
}
// Try to apply the disambiguation hints
if (fromFile) rooks &= FILE_MASKS[fromFile.toLowerCase()];
if (fromRank) rooks &= RANK_MASKS[fromRank.toLowerCase()];
const filtered = singleBitToIndex.get(rooks);
if (debug) console.log('Rooks filtered:', $$(rooks), 'only:', filtered);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
// This is a relatively common case when the rooks are doubled
return {i: filtered, n: rooks};
}
// Small optimization; only consider rooks in the same rank or file as the target cell, often avoids expensive move checks
rooks = rooks & (RANK_MASKS[tos[1]] | FILE_MASKS[tos[0]]);
const crossed = singleBitToIndex.get(rooks);
if (debug) console.log('Rooks crossed:', $$(rooks), 'only:', crossed);
if (crossed !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
// This is a relatively common case when the rooks are doubled
return {i: crossed, n: rooks};
}
// Despite disambiguation there are still multiple rook candidates left. Eliminate rooks that are blocked or even pinned.
// For each, check if the move would be valid. Discard rooks that are invalid (like if they're pinned to the king).
// After that we should have one rook left... We optimistically bail when we have one rook left (and assume the move is legit)
for (let i=0n; i<64n; ++i) {
const n = (1n << i);
if (rooks & n) {
if (debug) console.log('Considering rook moving from', indexToId[i], 'to', indexToId[toi], $$(rooks));
const can = canRookMove(G, i, toi, n, ton);
if (debug) console.log('- can:', can);
if (can !== 'ok') { // expensive
if (can === 'blocked') if (debug) console.log('- This rook is blocked from moving to to', indexToId[toi]);
else if (debug) console.log('- It is not valid for this rook to move to', indexToId[toi]);
// Remove
rooks ^= n;
if (debug) console.log('- After discarding this rook:', $$(rooks));
// Check if we have one rook _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
const filtered = singleBitToIndex.get(rooks);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: filtered, n: rooks};
}
continue;
}
const {state, byn} = doesMoveLeaveChecked(G, forWhite, i, toi, n, ton); // expensive
if (state === 'checked') {
if (debug) console.log('- This rook cannot move from', indexToId[i], 'to', indexToId[toi], 'because it would leave its king checked (byn=', byn, singleBitToIndex.get(byn), indexToId[singleBitToIndex.get(byn)], ')');
if (debug) console.log('Discarding', $$(n))
// Remove
rooks ^= n;
if (debug) console.log('- After discarding this rook:', $$(rooks));
// Check if we have one rook _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
const filtered = singleBitToIndex.get(rooks);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: filtered, n: rooks};
}
}
}
}
throw new Error('No single rook to move...?');
}
case 'N': {
let knights = G.knights & turnMask;
const only = singleBitToIndex.get(knights);
if (debug) console.log('Knights:', $$(knights), 'only:', only);
if (only !== undefined) {
// This is a common case as knights are some of the earliest pieces to be traded
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: only, n: knights};
}
// Try to apply the disambiguation hints
if (fromFile) knights &= FILE_MASKS[fromFile.toLowerCase()];
if (fromRank) knights &= RANK_MASKS[fromRank.toLowerCase()];
const filtered = singleBitToIndex.get(knights);
if (debug) console.log('Knights filtered:', $$(knights), 'only:', filtered);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
// This is a relatively common case when the knights are doubled
return {i: filtered, n: knights};
}
// Small optimization; only consider knights that can attack the target cell
knights = knights & knightMoves[toi];
const nit = singleBitToIndex.get(knights);
if (debug) console.log('Knights that can reach target cell:', $$(knights), 'only:', nit);
if (nit !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
// This is a relatively common case when the knights are doubled
return {i: nit, n: knights};
}
// Despite disambiguation there are still multiple knight candidates left. Eliminate knights that are blocked or even pinned.
// For each, check if the move would be valid. Discard knights that are invalid (like if they're pinned to the king).
// After that we should have one knight left... We optimistically bail when we have one knight left (and assume the move is legit)
for (let i=0n; i<64n; ++i) {
const n = (1n << i);
if (knights & n) {
if (debug) console.log('Considering knight moving from', indexToId[i], 'to', indexToId[toi], $$(knights));
// I don't think "block" and "can" checks are necessary here because we applied the knightMoves
// mask already and knights can't be blocked by other pieces. The capture may not be possible but in that case
// it applies to all knights in this set so that's not a validation step that helps with disambiguation.
//const can = canKnightMove(G, i, toi, n, ton);
//if (debug) console.log('- can:', can);
//if (can !== 'ok') { // expensive
// if (can === 'blocked') if (debug) console.log('- This knight is blocked from moving to to', indexToId[toi]);
// else if (debug) console.log('- It is not valid for this knight to move to', indexToId[toi]);
// // Remove
// knight ^= n;
// if (debug) console.log('- After discarding this knight:', $$(knight));
// // Check if we have one knight _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
// const filtered = singleBitToIndex.get(knight);
// if (filtered !== undefined) {
// // It could be that this move is also invalid for this last piece... But we should not need to confirm it.
// return {i: filtered, n: knight};
// }
// continue;
//}
const {state, byn} = doesMoveLeaveChecked(G, forWhite, i, toi, n, ton); // expensive
if (state === 'checked') {
if (debug) console.log('- This knight cannot move from', indexToId[i], 'to', indexToId[toi], 'because it would leave its king checked (byn=', byn, singleBitToIndex.get(byn), indexToId[singleBitToIndex.get(byn)], ')');
if (debug) console.log('Discarding', $$(n))
// Remove
knights ^= n;
if (debug) console.log('- After discarding this knight:', $$(knights));
// Check if we have one knight _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
const filtered = singleBitToIndex.get(knights);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: filtered, n: knights};
}
}
}
}
throw new Error('No single knight to move...?');
}
case 'B': {
let bishops = G.bishops & turnMask;
const only = singleBitToIndex.get(bishops);
if (debug) console.log('Bishops:', $$(bishops), 'only:', only);
if (only !== undefined) {
// This is a common case in the endgame
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: only, n: bishops};
}
// Try to apply the disambiguation hints
if (fromFile) bishops &= FILE_MASKS[fromFile.toLowerCase()];
if (fromRank) bishops &= RANK_MASKS[fromRank.toLowerCase()];
const filtered = singleBitToIndex.get(bishops);
if (debug) console.log('Bishops filtered:', $$(bishops), 'only:', filtered);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
// This is a relatively common case when the bishops are doubled
return {i: filtered, n: bishops};
}
// TODO: create cross maps per cell and reduce the bishops set to ones inside that mask
// Despite disambiguation there are still multiple bishop candidates left. Eliminate bishops that are blocked or even pinned.
// For each, check if the move would be valid. Discard bishops that are invalid (like if they're pinned to the king).
// After that we should have one bishop left... We optimistically bail when we have one bishop left (and assume the move is legit)
for (let i=0n; i<64n; ++i) {
const n = (1n << i);
if (bishops & n) {
if (debug) console.log('Considering bishop moving from', indexToId[i], 'to', indexToId[toi], $$(bishops));
const can = canBishopMove(G, i, toi, n, ton);
if (debug) console.log('- can:', can);
if (can !== 'ok') { // expensive
if (can === 'blocked') if (debug) console.log('- This bishop is blocked from moving to to', indexToId[toi]);
else if (debug) console.log('- It is not valid for this bishop to move to', indexToId[toi]);
// Remove
bishops ^= n;
if (debug) console.log('- After discarding this bishop:', $$(bishops));
// Check if we have one bishop _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
const filtered = singleBitToIndex.get(bishops);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: filtered, n: bishops};
}
continue;
}
const {state, byn} = doesMoveLeaveChecked(G, forWhite, i, toi, n, ton); // expensive
if (state === 'checked') {
if (debug) console.log('- This bishop cannot move from', indexToId[i], 'to', indexToId[toi], 'because it would leave its king checked (byn=', byn, singleBitToIndex.get(byn), indexToId[singleBitToIndex.get(byn)], ')');
if (debug) console.log('Discarding', $$(n))
// Remove
bishops ^= n;
if (debug) console.log('- After discarding this bishop:', $$(bishops));
// Check if we have one bishop _now_. Very most likely, at this point, we should be finished. And otherwise repeat :)
const filtered = singleBitToIndex.get(bishops);
if (filtered !== undefined) {
// It could be that this move is also invalid for this last piece... But we should not need to confirm it.
return {i: filtered, n: bishops};
}
}
}
}
throw new Error('No single bishop to move...?');
}
default: huh
}
}
/**
* @param G {Game}
*/
function recordBoardState(G) {
const hash = getFenishString(G);
const seen = G.threefold.get(hash);
G.threefold.set(hash, seen === undefined ? 1 : seen + 1);
}
/**
* Returns true if this would trigger a threefold draw. In that case the register is not updated.
* Returns false if the register is updated and the state has not been seen three or more times.
*
* @param G {Game}
* @param [max=3] {number} In real chess the value is 3 but if you want you can change that
*/
export function updateThreefoldOrBail(G, max = 3) {
const hash = getFenishString(G);
const seen = G.threefold.get(hash) ?? 0;
//console.log('hash:', hash, seen);
if (seen >= max) return true;
G.threefold.set(hash, seen + 1);
return false;
}
/**
* @param G {Game}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param fromn {BigInt} A single bit is set, representing the field to move from
* @param ton {BigInt} A single bit is set, representing the field to move to
* @param [ignoreTurn] {boolean}
* @param [targetCellI] {BigInt}
* @returns { 'ok' | 'bad' | 'blocked' }
*/
export function canMove(G, fromi, toi, fromn = 1n << fromi, ton = 1n << toi, ignoreTurn = false, targetCellI = NO_CELL_I) {
// Assumes the game state has one king. If there are multiple, the no-check rule is skipped.
const filled = G.white | G.black;
const forWhite = !!(G.white & fromn);
if (fromn === ton) {
//console.warn(fromi, toi, 'false: same cell')
return 'bad';
}
if (!(filled & fromn)) {
return 'bad';
}
if (!ignoreTurn && G.turnWhite !== forWhite) {
// It's not your turn so you can't move
return 'bad';
}
if (targetCellI !== NO_CELL_I && targetCellI !== toi) {
return 'bad';
}
// First check legibility at all
const pieceMove = canPieceMove(G, fromi, toi, fromn, ton, true);
if (pieceMove !== 'ok') {
return pieceMove;
}
// Now check if the move would leave you in a checked state
const {state: danger} = doesMoveLeaveChecked(G, forWhite, fromi, toi, fromn, ton);
if (danger === 'checked') return 'blocked';
return pieceMove;
}
/**
* Expensive
* Only works with one king
* Make move in temporary state and verify if the king is checked. In that case returns 'checked', else 'ok'
*
* @param G {Game}
* @param forWhite {boolean}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param fromn {BigInt} A single bit is set, representing the field to move from
* @param ton {BigInt} A single bit is set, representing the field to move to
* @returns {{state: 'ok'} | {state: 'checked', byn: BigInt, piece: 'P' | 'Q' | 'R' | 'B' | 'N' | ''}}
*/
function doesMoveLeaveChecked(G, forWhite, fromi, toi, fromn = 1n << fromi, ton = 1n << toi) {
// Make move in temporary state and verify if the king is checked. In that case, the move is bad.
/**
* @type {Game}
*/
const T = {...G};
makeMove(T, fromi, toi, fromn, ton);
const kingn = T.kings & (forWhite ? T.white : T.black);
const kingi = singleBitToIndex.get(kingn) ?? NO_CELL;
if (kingi === NO_CELL) return {state: 'ok'};
return isCheck(T, kingi, kingn, forWhite, forWhite ? blackPawnsThatCanCaptureOn : whitePawnsThatCanCaptureOn);
}
/**
* @param G {Game}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param fromn {BigInt} A single bit is set, representing the field to move from
* @param ton {BigInt} A single bit is set, representing the field to move to
* @param skipCheckCheck {boolean}
* @returns { 'ok' | 'bad' | 'blocked' }
*/
function canPieceMove(G, fromi, toi, fromn = 1n << fromi, ton = 1n << toi, skipCheckCheck = false) {
// For other game validation see canMove. This only validates individual piece rules.
if (G.pawns & fromn) {
return canPawnMove(G, fromi, toi, fromn, ton);
}
if (G.rooks & fromn) {
return canRookMove(G, fromi, toi, fromn, ton);
}
if (G.bishops & fromn) {
return canBishopMove(G, fromi, toi, fromn, ton);
}
if (G.knights & fromn) {
return canKnightMove(G, fromi, toi, fromn, ton);
}
if (G.queens & fromn) {
return canQueenMove(G, fromi, toi, fromn, ton);
}
if (G.kings & fromn) {
return canKingMove(G, fromi, toi, fromn, ton, skipCheckCheck);
}
throw new Error(`if its taken then it should be reflected by one of the states ${fromn} ${ton}`);
}
/**
* @param G {Game}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param fromn {BigInt} A single bit is set, representing the field to move from
* @param ton {BigInt} A single bit is set, representing the field to move to
* @returns { 'ok' | 'bad' | 'blocked' }
*/
function canPawnMove(G, fromi, toi, fromn, ton) {
// This skips validating the "from" cell and does no "left in check" validation
//// Not sure if this will be faster or have a meaningful difference versus the below
//// Note: this does not yet check double cell advancement between block (TODO)
//if (G.pawns & ton) {
// // Capturing. Need to check from and to cell to see who's attacking who
// const fromWhite = G.white & fromn;
// const toWhite = G.white & ton;
// if (!toWhite !== !fromWhite) {
// // Capturing opponent is okay, provided it's a legal move in the first place
// const canCapture = G.pawns & fromn & (toWhite ? whitePawnsThatCanCaptureOn[toi] : blackPawnsThatCanCaptureOn[toi]);
// if (canCapture) return 'ok';
// return 'bad';
// } else {
// // Cannot capture your own piece. Bail.
// return 'blocked';
// }
//} else {
// // Moving
// if ((G.white | G.black) & ton) {
// // Regardless of colors, a pawn can't move forward into any other piece
// return 'blocked';
// }
// // If the "from" cell index is lower than the "to" cell index, the pawn is moving towards black's side
// // (or any arbitrary position but not backwards), and it should be a white pawn, or probably won't matter
// // This prevents us from having to validate whether there actually was a pawn in that cell in the first place.
// const canMove = G.pawns & fromn & (fromi < toi ? whitePawnsThatCanMoveTo[toi] : blackPawnsThatCanMoveTo[toi]);
// if (canMove) return 'ok';
// return 'bad';
//}
const filled = G.white | G.black;
if (G.white & fromn) {
switch (toi - fromi) {
case 7n: { // up-right
//console.log('up-right', fromi, toi, (fromi % 8n) > 0n, (G.black & to) === 1n);
return (fromi % 8n) === 0n ? 'bad' : fromi - 1n === G.enpassant ? 'ok' : (G.black & ton) !== 0n ? 'ok' : 'blocked';
}
case 8n: { // up
//console.log('up', fromi, toi, (filled & to), (filled & to) === 0n);
return (filled & ton) === 0n ? 'ok' : 'blocked';
}
case 16n: { // up 2x
if (fromi / 8n !== 1n) return 'bad'; // Only from starting rank (rank 1 for white)
if ((filled & (fromn << 8n)) > 0n) return 'blocked'; // Can not jump over any other piece
if ((filled & ton) > 0n) return 'blocked'; // Can not capture any piece
return 'ok';
}
case 9n: { // up-left
//console.log('up-left', fromi, toi, (fromi % 8n) < 7n, (G.black & to) !== 0n);
return (fromi % 8n) === 7n ? 'bad' : fromi + 1n === G.enpassant ? 'ok' : (G.black & ton) !== 0n ? 'ok' : 'blocked';
}
default: { // illegal move
return 'bad';
}
}
} else {
//console.log('is black pawn', fromi,toi, toi - fromi)
switch (toi - fromi) {
case -9n: { // down-right
//console.log('down-right', (fromi % 8n) > 0n, (G.white & to) === 1n);
return (fromi % 8n) === 0n ? 'bad' : fromi - 1n === G.enpassant ? 'ok' : (G.white & ton) !== 0n ? 'ok' : 'blocked';
}
case -8n: { // down
//console.log('down', (filled & to), (filled & to) === 0n);
return (filled & ton) === 0n ? 'ok' : 'blocked';
}
case -16n: { // down 2x
return fromi / 8n !== 6n ? 'bad' : (filled & (fromn >> 8n)) !== 0n ? 'blocked' : (filled & ton) === 0n ? 'ok' : 'blocked';
}
case -7n: { // down-left
//console.log('down-left', (fromi % 8n) < 7n, (G.white & to) === 1n);
return (fromi % 8n) === 7n ? 'bad' : fromi + 1n === G.enpassant ? 'ok' : (G.white & ton) !== 0n ? 'ok' : 'blocked';
}
default: { // illegal move
return 'bad';
}
}
}
}
/**
* @param fromi {BigInt}
* @param toi {BigInt}
* @returns {boolean}
*/
function isStartingEnPassant(fromi, toi) {
// Pawns only move forward, so they can never (legally) return to their own 7th rank so we don't need to check/track this
// We simply check whether the pawn is moving from their starting row and making two steps forward.
if (fromi >= 8n && fromi <= 15n && toi >= 24n && toi <= 31n) {
return true;
}
if (fromi >= 48n && fromi <= 55n && toi >= 32n && toi <= 39n) {
return true;
}
return false;
}
/**
* @param G {Game}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param from {BigInt} A single bit is set, representing the field to move from
* @param to {BigInt} A single bit is set, representing the field to move to
* @returns { 'ok' | 'bad' | 'blocked' }
*/
function canRookMove(G, fromi, toi, from, to) {
// This skips validating the "from" cell and does no "left in check" validation
const filled = G.white | G.black;
//console.log('rook', fromi, toi, fromi % 8n, toi % 8n, fromi / 8n, toi / 8n, );
const dx = (toi % 8n) - (fromi % 8n);
const dy = (toi / 8n) - (fromi / 8n);
if (dx !== 0n && dy !== 0n) {
// Not moving on same line
return 'bad';
}
if ((filled & to) !== 0n && ((G.black & from) === (G.black & to) || (G.white & from) === (G.white & to))) {
// can't move rook to cell with piece of same color
//console.log('target blocked by own piece');
return 'blocked';
}
// We now know
// - the from is not empty
// - the to is not the same color as from, or empty
// - the from and to are on the same line (implies not OOB)
// Must now validate that in between cells are empty
// TODO: there's a bitwise hack for this but meanwhile this is a worst-case O(6) operation :shrug:
//console.log('- delta:', dx, dy, 'from:', fromi, 'to:', toi)
if (dx > 1n || dx < -1n || dy > 1n || dy < -1n) {
const step = dx === 0n ? 8n : 1n;
//console.log('step:', step, 'moving from', fromi, indexToId[fromi], ', to', toi, indexToId[toi], ', dx', dx, ', dy', dy);
// Force order to be incremental. It doesn't matter which cell is actually blocking (for us here). Just whether any in between is.
let i = (fromi < toi ? fromi : toi) + step;
let j = (fromi < toi ? toi : fromi) - step;
//console.log('offsets: i=', fromi + step, indexToId[fromi + step], ', j=', toi - step, indexToId[toi - step]);
for (; i<=j; i+=step) {
if ((filled) & (1n << i)) {
//console.log('rook is blocked at', i, indexToId[i])
return 'blocked';
}
}
}
return 'ok';
}
/**
* @param G {Game}
* @param fromi {BigInt} The index (0..63)
* @param toi {BigInt} The index (0..63)
* @param from {BigInt} A single bit is set, representing the field to move from
* @param to {BigInt} A single bit is set, representing the field to move to
* @returns { 'ok' | 'bad' | 'blocked' }
*/
function canBishopMove(G, fromi, toi, from, to) {
// This skips validating the "from" cell and does no "left in check" validation
const filled = G.white | G.black;
const rb = fromi < toi ? fromi : toi;
const lt = fromi < toi ? toi : fromi;
const dx_s = (rb % 8n) - (lt % 8n);
const dy_s = (rb / 8n) - (lt / 8n);
const dx_u = dx_s < 0n ? -dx_s : dx_s;
const dy_u = dy_s < 0n ? -dy_s : dy_s;
//console.log('bishop', fromi, toi, dx_s, dy_s, dx_u, dy_u);
if (dx_u !== dy_u) {
// Not moving exactly diagonal
return 'bad';
}
//console.warn('bishop; from:', fromi, indexToId[fromi], ', to:', toi, indexToId[toi], ', dx:', dx_s, dy_s);
if ((filled & to) !== 0n && ((G.black & from) === (G.black & to) || (G.white & from) === (G.white & to))) {
// can't move rook to cell with piece of same color
//console.log('target blocked by own piece');
return 'blocked';
}
// We now know
// - the from is not empty
// - the to is not the same color as from, or empty
// - the from and to are on the same diagonal (implies not OOB)
// Must now validate that in between cells are empty
// TODO: there's a bitwise hack for this but meanwhile this is a worst-case O(6) operation :shrug:
if (dx_u > 1n || dy_u > 1n) {
// Note: cell index order is right-to-left, bottom-to-top. It's a bit inverted.
// With i<j init, always stepping to up-left (+9n) or up-right (+7n)
const step = dx_s < 0n ? 9n : 7n;
//console.log('step:', step, 'moving from', fromi, indexToId[fromi], ', to', toi, indexToId[toi], ', dx', dx, ', dy', dy);
// Force order to be incremental (bottom-right to top-left).
// It doesn't matter which cell is actually blocking (for us here). Just whether any in between is.
let i = rb + step;
let j = lt - step;