-
Notifications
You must be signed in to change notification settings - Fork 0
/
board-moves.js
852 lines (808 loc) · 27 KB
/
board-moves.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
class UndoMove {
constructor() {
this.undoPiecesAtIndex = [];
}
addUndoPiece(index, piece) {
this.undoPiecesAtIndex.push({ index, piece });
}
}
class Move {
constructor(
board,
from,
to,
isHit = false,
enPassant = undefined,
enPassantTarget = undefined,
castlingKingTargetIndex = undefined,
castlingRookStartIndex = undefined,
castlingRookTargetIndex = undefined
) {
this.board = board;
this.calculateFromAndTo(from, to);
this.isHit = isHit ? true : undefined;
this.isCheck =
isHit && (this.targetPiece & Piece.PIECES_MASK) === Piece.KING;
this.enPassant = enPassant; // enPassant
this.enPassantTarget = enPassantTarget;
this.castlingKingTargetIndex = castlingKingTargetIndex; // castling king - king position
this.castlingRookStartIndex = castlingRookStartIndex; // castling king - rook position start
this.castlingRookTargetIndex = castlingRookTargetIndex; // castling king - rook position target
this.undoMove = undefined;
this.promotionPiece = Piece.None;
this.moveScoreGuess = 0;
this.randomScoreGuess = 0;
}
calculateFromAndTo(from, to) {
this.from = from;
this.to = to;
this.piece = this.board.getPiece(from);
this.pieceOnly = this.piece & Piece.PIECES_MASK;
this.color = this.piece & Piece.COLOR_MASK;
this.colorName = PieceNames[this.color];
this.pieceName = PieceNames[this.pieceOnly];
this.targetPiece = this.board.getPiece(to);
this.targetPieceOnly = this.targetPiece & Piece.PIECES_MASK;
this.targetColor = this.targetPiece & Piece.COLOR_MASK;
this.targetColorName = PieceNames[this.targetColor];
this.targetPieceName = PieceNames[this.targetPieceOnly];
}
clone() {
return new Move(
this.board,
this.from,
this.to,
this.isHit,
this.enPassant,
this.enPassantTarget,
this.castlingKingTargetIndex,
this.castlingRookStartIndex,
this.castlingRookTargetIndex
);
}
eq(other) {
return (
other?.from === this.from &&
other?.to === this.to &&
other?.promotionPiece === this.promotionPiece &&
other?.isHit === this.isHit
);
}
eqFromTo(other) {
return (
other?.from === this.from &&
other?.to === this.to &&
other?.promotionPiece === this.promotionPiece
);
}
isEnPassantAttackable() {
return (
(this.piece & Piece.PIECES_MASK) === Piece.PAWN &&
Math.abs(this.from - this.to) === 16 // 2 move
);
}
getIndexes() {
if (this.pieceOnly === Piece.KNIGHT) {
return [this.from, this.to];
}
const gridFrom = this.board.indexToGrid(this.from);
const gridTo = this.board.indexToGrid(this.to);
const diff = {
gridY: gridTo.gridY - gridFrom.gridY,
gridX: gridTo.gridX - gridFrom.gridX,
};
const sign = {
gridY: Math.sign(diff.gridY),
gridX: Math.sign(diff.gridX),
};
const newGrid = {
gridY: gridFrom.gridY,
gridX: gridFrom.gridX,
};
const indexes = [];
let counter = 0;
while (newGrid.gridY != gridTo.gridY || newGrid.gridX != gridTo.gridX) {
const index = newGrid.gridY * ROW_CELLS + newGrid.gridX;
indexes.push(index);
newGrid.gridY += sign.gridY;
newGrid.gridX += sign.gridX;
counter++;
if (counter > 8) {
throw Error("Looping ");
}
}
const index = newGrid.gridY * ROW_CELLS + newGrid.gridX;
indexes.push(index);
return indexes;
}
toCoordinateNotation() {
const sourceSquare = this.board.indexToAlgebraic(this.from);
const targetSquare = this.board.indexToAlgebraic(this.to);
if (this.promotionPiece > 0) {
return sourceSquare + targetSquare + toPieceNotation(this.promotionPiece);
} else {
return sourceSquare + targetSquare;
}
}
toAlgebraicNotation() {
if (this.castlingRookTargetIndex && this.castlingRookStartIndex) {
const isLong =
Math.abs(this.castlingKingTargetIndex - this.castlingRookStartIndex) ===
3;
return isLong ? "O-O-O" : "O-O";
}
const sourcePieceNotation = toPieceNotation(this.piece);
const targetPieceNotation = toPieceNotation(this.targetPiece);
const sourceSquare = this.board.indexToAlgebraic(this.from);
const targetSquare = this.board.indexToAlgebraic(this.to);
const hitString = this.isHit ? "x" : "";
const checkString = this.isCheck ? "+" : "";
const enPassantString = this.enPassant ? "e.p." : "";
const sourceString =
sourcePieceNotation +
hitString +
sourceSquare +
enPassantString +
checkString;
const targetString =
targetPieceNotation +
hitString +
targetSquare +
enPassantString +
checkString;
return targetSquare;
}
setPiece(index, piece) {
const oldPiece = this.board.setPiece(index, piece);
this.undoMove.addUndoPiece(index, oldPiece);
}
undoLastMove() {
if (this.undoMove) {
for (const indexAndPiece of this.undoMove.undoPiecesAtIndex) {
this.board.setPieceInternal(indexAndPiece.index, indexAndPiece.piece);
}
this.undoMove = undefined;
}
}
makeMove() {
this.undoMove = new UndoMove();
let toPiece = this.board.getPiece(this.from);
if (this.promotionPiece > 0) {
toPiece = this.promotionPiece;
}
this.setPiece(this.to, toPiece);
this.setPiece(this.from, Piece.None);
if (this.enPassant) {
this.setPiece(this.enPassant, Piece.None);
}
if (this.castlingKingTargetIndex) {
this.setPiece(
this.castlingRookTargetIndex,
this.board.getPiece(this.castlingRookStartIndex)
);
this.setPiece(this.castlingRookStartIndex, Piece.None);
}
}
isPartOf(listOfMoves) {
return listOfMoves.find((x) => x.eqFromTo(this)) !== undefined;
}
}
class LegalMoves {
constructor(color, boardData) {
this.boardData = boardData;
this.color = color;
this.moves = [];
this.checkAttackIndexes = [];
this.checkAttackOnPinnedPieces = [];
}
eq(other) {
if (this.moves.length != other?.moves.length) return false;
for (let i = 0; i < this.moves.length; i++) {
const move = this.moves[i];
const otherMove = other.moves[i];
if (!move.eq(otherMove)) return false;
}
return true;
}
getCheckMovesTo(index) {
return this.moves.filter((x) => x.to === index && x.isCheck);
}
getMovesTo(index) {
return this.moves.filter((x) => x.to === index);
}
getMovesTo(index) {
return this.moves.filter((x) => x.to === index);
}
getMovesFrom(index) {
return this.moves.filter((x) => x.from === index);
}
hasAnyMoveFromIndex(index) {
return this.moves.find((x) => x.from === index) != undefined;
}
hasAnyMoveToIndex(index) {
return this.moves.find((x) => x.to === index) != undefined;
}
addMoveTo(move, newMoves) {
if (0 <= move.to && move.to < 64) {
newMoves.push(move);
}
}
addMove(move) {
if (0 <= move.to && move.to < 64) {
this.moves.push(move);
}
}
addMove(move, newMoves) {
if (0 <= move.to && move.to < 64) {
if (newMoves) {
newMoves.push(move);
} else {
this.moves.push(move);
}
}
}
hasMoveForMyKing() {
return this.getMovesToMyKing().length > 0;
}
getMovesToMyKing() {
const opponentColor = this.color ^ Piece.COLOR_MASK;
const index = this.boardData.getKingPosition(opponentColor);
return this.getMovesTo(index);
}
getMovesOfMyKing() {
const index = this.boardData.getKingPosition(this.color);
verbose === 2 &&
console.log("Index of my KING (" + PieceNames[this.color] + ")=" + index);
return this.getMovesFrom(index);
}
generateMoveForPieceFromIndex(newMoves, index, piece, color) {
if (isSlidingPiece(piece)) {
this.generateSlidingMoves(newMoves, index, piece, color);
} else if (isKing(piece)) {
this.generateKingMoves(newMoves, index, piece, color);
this.generateCastlingKings(newMoves, index, piece, color);
} else if (isKnight(piece)) {
this.generateKnightMoves(newMoves, index, piece, color);
} else if (isPawn(piece)) {
const pawnMoves = [];
this.generatePawnMoves(pawnMoves, index, piece, color);
this.generatePawnPromotions(pawnMoves, color);
newMoves.push(...pawnMoves);
}
}
generateMoves(color) {
color = color || this.color;
const newMoves = [];
const pieces = this.boardData.getPiecesCacheByColor(color); // [ {piece: <int>, indexes: [ int, int ]} ]
for (const pieceAndIndexes of pieces) {
for (const index of pieceAndIndexes.indexes) {
const piece = pieceAndIndexes.piece;
this.generateMoveForPieceFromIndex(newMoves, index, piece, color);
}
}
return newMoves;
}
getCastlingOptions(kingPiece, color) {
if (this.boardData.history.hasMoved(kingPiece))
return { long: false, short: false };
const rookPiece = Piece.ROOK | color;
const rookPositions =
color === Piece.WHITE ? CastlingPositionsWhite : CastlingPositionsBlack;
const rookLongPiece = this.boardData.getPiece(rookPositions[0]);
const rookLongStillThere =
(rookLongPiece & Piece.PIECES_MASK) === Piece.ROOK &&
(rookLongPiece & Piece.COLOR_MASK) === color;
const rookLongMoved =
!rookLongStillThere ||
this.boardData.history.hasMovedFromIndex(rookPiece, rookPositions[0]);
const rookShortPiece = this.boardData.getPiece(rookPositions[1]);
const rookShortStillThere =
(rookShortPiece & Piece.PIECES_MASK) === Piece.ROOK &&
(rookShortPiece & Piece.COLOR_MASK) === color;
const rookShortMoved =
!rookShortStillThere ||
this.boardData.history.hasMovedFromIndex(rookPiece, rookPositions[1]);
return { long: !rookLongMoved, short: !rookShortMoved };
}
myLegalMoves(color) {
if (this.boardData.legalMoves.color === color) {
return this.boardData.legalMoves;
} else {
return this.boardData.opponentLegalMoves;
}
}
myOpponentLegalMoves(color) {
if (this.boardData.legalMoves.color === color) {
return this.boardData.opponentLegalMoves;
} else {
return this.boardData.legalMoves;
}
}
generateCastlingKings(newMoves, startIndex, piece, color) {
if (this.boardData.history.hasMoved(piece)) return;
const rookPositions =
color === Piece.WHITE ? CastlingPositionsWhite : CastlingPositionsBlack;
const castlingOptions = this.getCastlingOptions(piece, color);
const longPossible = castlingOptions.long;
const shortPossible = castlingOptions.short;
if (longPossible) {
const targetIndex = rookPositions[0] + 2;
let isEmpty = true;
for (let index = rookPositions[0] + 1; index < startIndex; index++) {
const shouldBeEmptyPiece = this.boardData.getPiece(index);
if (shouldBeEmptyPiece != 0) {
isEmpty = false;
break;
}
// is empty - check now if attack opponent attacks this index - only for index where king is moving
if (targetIndex <= index && index < startIndex) {
if (this.myOpponentLegalMoves(color).moves.length === 0) {
verbose === 2 && console.log("Opponent moves = empty");
}
if (this.myOpponentLegalMoves(color).hasAnyMoveToIndex(index)) {
verbose === 1 &&
console.log("Castling not allowed due to attack on " + index);
isEmpty = false;
break;
}
}
}
if (
isEmpty &&
this.myOpponentLegalMoves(color).hasAnyMoveToIndex(startIndex)
) {
isEmpty = false;
verbose === 1 &&
console.log(
"Castling not allowed due because King " +
startIndex +
" is in check"
);
}
if (isEmpty) {
const newMove = new Move(
this.boardData,
startIndex,
targetIndex,
false,
undefined,
undefined,
targetIndex,
rookPositions[0],
targetIndex + 1
);
this.addMoveTo(newMove, newMoves);
if (startIndex === 4) {
verbose === 1 &&
console.log(
"Castling allowed for King " +
startIndex +
" for target " +
targetIndex
);
verbose === 1 && console.log(newMove);
}
}
}
if (shortPossible) {
let isEmpty = true;
const targetIndex = startIndex + 2;
for (let index = startIndex + 1; index < rookPositions[1]; index++) {
const shouldBeEmptyPiece = this.boardData.getPiece(index);
if (shouldBeEmptyPiece != 0) {
isEmpty = false;
break;
}
// is empty - check now if attack opponent attacks this index - only for index where king is moving
if (startIndex < index && index <= targetIndex) {
if (this.myOpponentLegalMoves(color).hasAnyMoveToIndex(index)) {
verbose === 2 &&
console.log("Castling not allowed due to attack on " + index);
isEmpty = false;
break;
}
}
}
if (
isEmpty &&
this.myOpponentLegalMoves(color).hasAnyMoveToIndex(startIndex)
) {
isEmpty = false;
verbose === 2 &&
console.log(
"Castling not allowed due because King " +
startIndex +
" is in check"
);
}
if (isEmpty) {
const newMove = new Move(
this.boardData,
startIndex,
targetIndex,
false,
undefined,
undefined,
targetIndex,
rookPositions[1],
targetIndex - 1
);
this.addMoveTo(newMove, newMoves);
}
}
}
generateKingMoves(newMoves, startIndex, piece, color) {
const oppositeColor = color ^ Piece.COLOR_MASK;
for (let i = 0; i < directionOffsets.length; i++) {
const targetIndex = startIndex + directionOffsets[i];
const grid = this.boardData.indexToGrid(startIndex);
const targetGrid = this.boardData.indexToGrid(targetIndex);
if (
(grid.gridX === 0 && targetGrid.gridX == 7) ||
(grid.gridX === 7 && targetGrid.gridX === 0) ||
(grid.gridY === 0 && targetGrid.gridY === 7) ||
(grid.gridY === 7 && targetGrid.gridY === 0)
) {
// no moves allowed over edges
} else {
const pieceOnTargetIndex = this.boardData.getPiece(targetIndex);
const pieceOnTargetIndexColor = pieceOnTargetIndex & Piece.COLOR_MASK;
if (pieceOnTargetIndexColor === color) {
// skip
} else if (pieceOnTargetIndexColor === oppositeColor) {
this.addMoveTo(
new Move(this.boardData, startIndex, targetIndex, true),
newMoves
);
} else {
this.addMoveTo(
new Move(this.boardData, startIndex, targetIndex, false),
newMoves
);
}
}
}
}
generateKnightMoves(newMoves, startIndex, piece, color) {
//
const knightsDirectionOffsetsYX = [
[-2, -1],
[-2, 1],
[2, -1],
[2, 1],
[-1, -2],
[-1, 2],
[1, -2],
[1, 2],
];
const oppositeColor = color ^ Piece.COLOR_MASK;
const grid = this.boardData.indexToGrid(startIndex);
for (let i = 0; i < knightsDirectionOffsetsYX.length; i++) {
const knightsGridDiff = knightsDirectionOffsetsYX[i];
const newY = grid.gridY + knightsGridDiff[0];
const newX = grid.gridX + knightsGridDiff[1];
if (0 <= newY && newY < 8 && 0 <= newX && newX < 8) {
const targetIndex = newY * 8 + newX;
const pieceOnTargetIndex = this.boardData.getPiece(targetIndex);
const pieceOnTargetIndexColor = pieceOnTargetIndex & Piece.COLOR_MASK;
if (pieceOnTargetIndexColor === color) {
// skip
} else if (pieceOnTargetIndexColor === oppositeColor) {
this.addMoveTo(
new Move(this.boardData, startIndex, targetIndex, true),
newMoves
);
} else {
this.addMoveTo(
new Move(this.boardData, startIndex, targetIndex, false),
newMoves
);
}
}
}
}
generatePawnPromotions(newPawnMoves, color) {
const targetPawnIndexes =
(color & Piece.WHITE) > 0
? { startIndex: 0, endIndex: 7 }
: { startIndex: 56, endIndex: 63 };
const movesToIterate = [...newPawnMoves];
for (const move of movesToIterate) {
if (
targetPawnIndexes.startIndex <= move.to &&
move.to <= targetPawnIndexes.endIndex
) {
let newMove = move;
newMove.promotionPiece = Piece.QUEEN | color;
newMove = move.clone();
newPawnMoves.push(newMove);
newMove.promotionPiece = Piece.ROOK | color;
newMove = move.clone();
newPawnMoves.push(newMove);
newMove.promotionPiece = Piece.KNIGHT | color;
newMove = move.clone();
newPawnMoves.push(newMove);
newMove.promotionPiece = Piece.BISHOP | color;
}
}
}
generatePawnMoves(newMoves, startIndex, piece, color) {
const startPawnIndex = (color & Piece.WHITE) > 0 ? 6 : 1;
const directionOffsetY = (color & Piece.WHITE) > 0 ? -1 : 1;
const pieceOnly = piece & Piece.PIECES_MASK;
const pieceColor = piece & Piece.COLOR_MASK;
const oppositeColor = color ^ Piece.COLOR_MASK;
const grid = this.boardData.indexToGrid(startIndex);
// move 2
if (grid.gridY === startPawnIndex) {
const newY = grid.gridY + 2 * directionOffsetY;
const newY1 = grid.gridY + directionOffsetY;
const newX = grid.gridX;
if (0 <= newY && newY < 8 && 0 <= newX && newX < 8) {
const targetIndex = newY * 8 + newX;
const middleIndex = newY1 * 8 + newX;
const enPassantTargetIndex = (newY - directionOffsetY) * 8 + newX;
if (0 <= targetIndex && targetIndex < 64) {
const pieceOnTargetIndex = this.boardData.getPiece(targetIndex);
const pieceOnTargetIndexColor = pieceOnTargetIndex & Piece.COLOR_MASK;
const pieceOnMiddleIndex = this.boardData.getPiece(middleIndex);
if (pieceOnTargetIndexColor === 0 && pieceOnMiddleIndex === 0) {
const newMove = new Move(
this.boardData,
startIndex,
targetIndex,
false,
undefined,
enPassantTargetIndex
);
this.addMoveTo(newMove, newMoves);
}
}
}
}
// move normal: 1
let newY = grid.gridY + directionOffsetY;
let newX = grid.gridX;
if (0 <= newY && newY < 8 && 0 <= newX && newX < 8) {
const targetIndex = newY * 8 + newX;
if (0 <= targetIndex && targetIndex < 64) {
const pieceOnTargetIndex = this.boardData.getPiece(targetIndex);
const pieceOnTargetIndexColor = pieceOnTargetIndex & Piece.COLOR_MASK;
if (pieceOnTargetIndexColor === 0) {
const newMove = new Move(
this.boardData,
startIndex,
targetIndex,
false
);
this.addMoveTo(newMove, newMoves);
}
}
}
// hit rule left
const directionsX = [-1, 1];
for (const dirX of directionsX) {
newY = grid.gridY + directionOffsetY;
newX = grid.gridX + dirX;
if (0 <= newY && newY < 8 && 0 <= newX && newX < 8) {
const targetIndex = newY * 8 + newX;
const pieceOnTargetIndex = this.boardData.getPiece(targetIndex);
const pieceOnTargetIndexColor = pieceOnTargetIndex & Piece.COLOR_MASK;
if (pieceOnTargetIndexColor === oppositeColor) {
const newMove = new Move(
this.boardData,
startIndex,
targetIndex,
true
);
this.addMoveTo(newMove, newMoves);
} else if (pieceOnTargetIndexColor === 0) {
// check for en-passang
newY = grid.gridY;
newX = grid.gridX + dirX;
if (0 <= newY && newY < 8 && 0 <= newX && newX < 8) {
const targetEnPIndex = newY * 8 + newX;
const pieceEnPOnTargetIndex =
this.boardData.getPiece(targetEnPIndex);
const pieceEnPOnTargetIndexColor =
pieceEnPOnTargetIndex & Piece.COLOR_MASK;
if (pieceEnPOnTargetIndexColor === oppositeColor) {
if (this.boardData.isLegalEnPassant(targetEnPIndex)) {
const newMove = new Move(
this.boardData,
startIndex,
targetIndex,
true,
targetEnPIndex
);
this.addMoveTo(newMove, newMoves);
}
}
}
}
}
}
}
generateSlidingMoves(newMoves, startIndex, piece, color) {
const pieceOnly = piece & Piece.PIECES_MASK;
const pieceColor = piece & Piece.COLOR_MASK;
const oppositeColor = color ^ Piece.COLOR_MASK;
const startDirIndex = pieceOnly == Piece.BISHOP ? 4 : 0;
const endDirIndex = pieceOnly === Piece.ROOK ? 4 : 8;
for (
let directionIndex = startDirIndex;
directionIndex < endDirIndex;
directionIndex++
) {
const distanceToTarget = distanceToEdge[startIndex][directionIndex];
if (distanceToTarget) {
let n = 0;
const inc = Math.sign(distanceToTarget);
do {
const targetIndex =
startIndex + directionOffsets[directionIndex] * (Math.abs(n) + 1);
if (0 <= targetIndex && targetIndex < 64) {
const pieceOnTargetIndex = this.boardData.getPiece(targetIndex);
const pieceOnTargetIndexColor =
pieceOnTargetIndex & Piece.COLOR_MASK;
if (pieceOnTargetIndexColor === color) {
break;
}
if (pieceOnTargetIndexColor === oppositeColor) {
this.addMoveTo(
new Move(this.boardData, startIndex, targetIndex, true),
newMoves
);
break;
} else {
this.addMoveTo(
new Move(this.boardData, startIndex, targetIndex, false),
newMoves
);
}
} else {
break;
}
n = n + inc;
} while (inc === 1 ? n < distanceToTarget : n > distanceToTarget);
}
}
}
removePseudoIllegalMovesForMyKing(color) {
const legalMovesForKing = this.getMovesFrom(
this.boardData.getKingPosition(color)
);
for (const moveOfKing of legalMovesForKing) {
moveOfKing.makeMove();
const opponentColor = color ^ Piece.COLOR_MASK;
const newMoves =
this.myOpponentLegalMoves(color).generateMoves(opponentColor);
moveOfKing.undoLastMove();
if (
newMoves.find(
(x) =>
x.to === moveOfKing.to &&
x.isHit &&
x.targetPieceOnly === Piece.KING
)
) {
this.moves = this.moves.filter((x) => !x.eqFromTo(moveOfKing));
}
// old algo: not covering new moves for check - check if any opponent move target at the legal move target
/*
let hasAnyCheck = this.myOpponentLegalMoves(color).hasAnyMoveToIndex(
moveOfKing.to
);
if (hasAnyCheck) {
this.moves = this.moves.filter((x) => !x.eqFromTo(moveOfKing));
}
*/
}
}
// checkout: http://127.0.0.1:5500/#r3k3/1p3p2/p2q2p1/bn3P2/1N2PQP1/PB6/3K1R1r/3R4%20w%20KQkq%20-%200%201
limitingMovementPinnedPieces() {
// current legal moves: check for all moves if opponent offers new attacks to king if piece would move away
const movesOfNotKing = this.moves.filter((x) => x.pieceOnly != Piece.KING);
this.checkAttackOnPinnedPieces = [];
const movesToRemove = [];
for (const move of movesOfNotKing) {
move.makeMove();
let newMoves = this.boardData.opponentLegalMoves.generateMoves();
newMoves = newMoves.filter(
(x) => x.isHit && x.targetPieceOnly === Piece.KING
);
move.undoLastMove();
if (newMoves.length > 0) {
this.checkAttackOnPinnedPieces.push(
...newMoves.flatMap((x) => x.getIndexes())
);
movesToRemove.push(move);
}
}
if (movesToRemove) {
this.moves = this.moves.filter((x) => !x.isPartOf(movesToRemove));
}
}
removePseudoIllegalMoves(movesToCheck) {
if (movesToCheck.length === 0) return;
const color = movesToCheck[0].color;
this.checkAttackIndexes = [];
for (const move of movesToCheck) {
this.checkAttackIndexes.push(...move.getIndexes());
}
let movesToKeep = [];
for (const move of this.moves) {
const canPreventCheck = this.checkAttackIndexes.includes(move.to);
if (canPreventCheck && move.pieceOnly !== Piece.KING) {
movesToKeep.push(move);
}
}
const movesOfTheKing = this.getMovesOfMyKing();
verbose >= 2 && console.table(movesToKeep);
// check which are moves that are in attack by opponent
// opponent.to == movesOfTheKing.to
for (const moveOfKing of movesOfTheKing) {
const kingIsInCheckAndCastlingMove =
moveOfKing.castlingKingTargetIndex &&
this.checkAttackIndexes.includes(moveOfKing.from);
if (!kingIsInCheckAndCastlingMove) {
const kingsTargetInAttackLineNotAllowed =
this.checkAttackIndexes.includes(moveOfKing.to);
if (!kingsTargetInAttackLineNotAllowed) {
for (const opponentMove of this.myLegalMoves(color).moves) {
if (moveOfKing.to !== opponentMove.from) {
if (
movesToKeep.find((x) => x.eqFromTo(moveOfKing)) === undefined
) {
movesToKeep.push(moveOfKing);
}
}
}
} else if (moveOfKing.isHit) {
// check 8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - move the king
movesToKeep.push(moveOfKing);
}
} else {
verbose === 2 &&
console.log(
"Castling not allowed due because King " +
moveOfKing.from +
" is in check (remove pseudo illegal move)"
);
}
}
// check if new opponent hits would check the king
const movesToKeepWithoutCheck = [];
for (const moveOfKing of movesToKeep) {
if (moveOfKing.pieceOnly === Piece.KING) {
this.boardData.setPieceInternal(moveOfKing.from, 0);
const oldTargetPiece = this.boardData.setPieceInternal(
moveOfKing.to,
moveOfKing.piece
);
let newMoves = this.boardData.opponentLegalMoves.generateMoves();
newMoves = newMoves.filter(
(x) => x.isHit && x.targetPieceOnly === Piece.KING
);
if (newMoves.length == 0) {
movesToKeepWithoutCheck.push(moveOfKing);
} else {
verbose === 2 && console.log("Remove move for king to go into check");
}
this.boardData.setPieceInternal(moveOfKing.from, moveOfKing.piece);
this.boardData.setPieceInternal(moveOfKing.to, oldTargetPiece);
} else {
movesToKeepWithoutCheck.push(moveOfKing);
}
}
verbose >= 2 && console.table(movesToKeepWithoutCheck);
this.moves = movesToKeepWithoutCheck;
}
}
if (typeof module !== "undefined") {
module.exports = {
Move,
MoveGeneratorStats,
MoveGeneratorTest,
};
}