1 import { ArrayFun
} from "@/utils/array";
2 import { randInt
} from "@/utils/alea";
3 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
5 export class EightpiecesRules
extends ChessRules
{
16 static get IMAGE_EXTENSION() {
17 // Temporarily, for the time SVG pieces are being designed:
21 // Lancer directions *from white perspective*
22 static get LANCER_DIRS() {
36 return ChessRules
.PIECES
37 .concat([V
.JAILER
, V
.SENTRY
])
38 .concat(Object
.keys(V
.LANCER_DIRS
));
42 const piece
= this.board
[i
][j
].charAt(1);
43 // Special lancer case: 8 possible orientations
44 if (Object
.keys(V
.LANCER_DIRS
).includes(piece
)) return V
.LANCER
;
48 getPpath(b
, color
, score
, orientation
) {
49 if ([V
.JAILER
, V
.SENTRY
].includes(b
[1])) return "Eightpieces/tmp_png/" + b
;
50 if (Object
.keys(V
.LANCER_DIRS
).includes(b
[1])) {
51 if (orientation
== 'w') return "Eightpieces/tmp_png/" + b
;
52 // Find opposite direction for adequate display:
80 return "Eightpieces/tmp_png/" + b
[0] + oppDir
;
82 // TODO: after we have SVG pieces, remove the folder and next prefix:
83 return "Eightpieces/tmp_png/" + b
;
86 getPPpath(b
, orientation
) {
87 return this.getPpath(b
, null, null, orientation
);
90 static ParseFen(fen
) {
91 const fenParts
= fen
.split(" ");
93 ChessRules
.ParseFen(fen
),
94 { sentrypush: fenParts
[5] }
98 static IsGoodFen(fen
) {
99 if (!ChessRules
.IsGoodFen(fen
)) return false;
100 const fenParsed
= V
.ParseFen(fen
);
101 // 5) Check sentry push (if any)
103 fenParsed
.sentrypush
!= "-" &&
104 !fenParsed
.sentrypush
.match(/^([a-h][1-8],?)+$/)
112 return super.getFen() + " " + this.getSentrypushFen();
116 return super.getFenForRepeat() + "_" + this.getSentrypushFen();
120 const L
= this.sentryPush
.length
;
121 if (!this.sentryPush
[L
-1]) return "-";
123 this.sentryPush
[L
-1].forEach(coords
=>
124 res
+= V
.CoordsToSquare(coords
) + ",");
125 return res
.slice(0, -1);
128 setOtherVariables(fen
) {
129 super.setOtherVariables(fen
);
130 // subTurn == 2 only when a sentry moved, and is about to push something
132 // Sentry position just after a "capture" (subTurn from 1 to 2)
133 this.sentryPos
= null;
134 // Stack pieces' forbidden squares after a sentry move at each turn
135 const parsedFen
= V
.ParseFen(fen
);
136 if (parsedFen
.sentrypush
== "-") this.sentryPush
= [null];
139 parsedFen
.sentrypush
.split(",").map(sq
=> {
140 return V
.SquareToCoords(sq
);
146 static GenRandInitFen(randomness
) {
149 return "jfsqkbnr/pppppppp/8/8/8/8/PPPPPPPP/JDSQKBNR w 0 ahah - -";
151 let pieces
= { w: new Array(8), b: new Array(8) };
153 // Shuffle pieces on first (and last rank if randomness == 2)
154 for (let c
of ["w", "b"]) {
155 if (c
== 'b' && randomness
== 1) {
156 const lancerIdx
= pieces
['w'].findIndex(p
=> {
157 return Object
.keys(V
.LANCER_DIRS
).includes(p
);
160 pieces
['w'].slice(0, lancerIdx
)
162 .concat(pieces
['w'].slice(lancerIdx
+ 1));
167 let positions
= ArrayFun
.range(8);
169 // Get random squares for bishop and sentry
170 let randIndex
= 2 * randInt(4);
171 let bishopPos
= positions
[randIndex
];
172 // The sentry must be on a square of different color
173 let randIndex_tmp
= 2 * randInt(4) + 1;
174 let sentryPos
= positions
[randIndex_tmp
];
176 // Check if white sentry is on the same color as ours.
177 // If yes: swap bishop and sentry positions.
178 // NOTE: test % 2 == 1 because there are 7 slashes.
179 if ((pieces
['w'].indexOf('s') - sentryPos
) % 2 == 1)
180 [bishopPos
, sentryPos
] = [sentryPos
, bishopPos
];
182 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
183 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
185 // Get random squares for knight and lancer
186 randIndex
= randInt(6);
187 const knightPos
= positions
[randIndex
];
188 positions
.splice(randIndex
, 1);
189 randIndex
= randInt(5);
190 const lancerPos
= positions
[randIndex
];
191 positions
.splice(randIndex
, 1);
193 // Get random square for queen
194 randIndex
= randInt(4);
195 const queenPos
= positions
[randIndex
];
196 positions
.splice(randIndex
, 1);
198 // Rook, jailer and king positions are now almost fixed,
199 // only the ordering rook->jailer or jailer->rook must be decided.
200 let rookPos
= positions
[0];
201 let jailerPos
= positions
[2];
202 const kingPos
= positions
[1];
203 flags
+= V
.CoordToColumn(rookPos
) + V
.CoordToColumn(jailerPos
);
204 if (Math
.random() < 0.5) [rookPos
, jailerPos
] = [jailerPos
, rookPos
];
206 pieces
[c
][rookPos
] = "r";
207 pieces
[c
][knightPos
] = "n";
208 pieces
[c
][bishopPos
] = "b";
209 pieces
[c
][queenPos
] = "q";
210 pieces
[c
][kingPos
] = "k";
211 pieces
[c
][sentryPos
] = "s";
212 // Lancer faces north for white, and south for black:
213 pieces
[c
][lancerPos
] = c
== 'w' ? 'c' : 'g';
214 pieces
[c
][jailerPos
] = "j";
217 pieces
["b"].join("") +
218 "/pppppppp/8/8/8/8/PPPPPPPP/" +
219 pieces
["w"].join("").toUpperCase() +
220 " w 0 " + flags
+ " - -"
224 canTake([x1
, y1
], [x2
, y2
]) {
225 if (this.subTurn
== 2)
226 // Only self captures on this subturn:
227 return this.getColor(x1
, y1
) == this.getColor(x2
, y2
);
228 return super.canTake([x1
, y1
], [x2
, y2
]);
231 // Is piece on square (x,y) immobilized?
232 isImmobilized([x
, y
]) {
233 const color
= this.getColor(x
, y
);
234 const oppCol
= V
.GetOppCol(color
);
235 for (let step
of V
.steps
[V
.ROOK
]) {
236 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
239 this.board
[i
][j
] != V
.EMPTY
&&
240 this.getColor(i
, j
) == oppCol
242 if (this.getPiece(i
, j
) == V
.JAILER
) return [i
, j
];
248 // Because of the lancers, getPiece() could be wrong:
249 // use board[x][y][1] instead (always valid).
250 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
251 const initColor
= this.getColor(sx
, sy
);
252 const initPiece
= this.board
[sx
][sy
].charAt(1);
258 c: tr
? tr
.c : initColor
,
259 p: tr
? tr
.p : initPiece
272 // The opponent piece disappears if we take it
273 if (this.board
[ex
][ey
] != V
.EMPTY
) {
278 c: this.getColor(ex
, ey
),
279 p: this.board
[ex
][ey
].charAt(1)
287 canIplay(side
, [x
, y
]) {
289 (this.subTurn
== 1 && this.turn
== side
&& this.getColor(x
, y
) == side
) ||
290 (this.subTurn
== 2 && x
== this.sentryPos
.x
&& y
== this.sentryPos
.y
)
294 getPotentialMovesFrom([x
, y
]) {
295 // At subTurn == 2, jailers aren't effective (Jeff K)
296 const piece
= this.getPiece(x
, y
);
297 const L
= this.sentryPush
.length
;
298 if (this.subTurn
== 1) {
299 const jsq
= this.isImmobilized([x
, y
]);
302 // Special pass move if king:
303 if (piece
== V
.KING
) {
308 start: { x: x
, y: y
},
309 end: { x: jsq
[0], y: jsq
[1] }
313 else if (piece
== V
.LANCER
&& !!this.sentryPush
[L
-1]) {
314 // A pushed lancer next to the jailer: reorient
315 const color
= this.getColor(x
, y
);
316 const curDir
= this.board
[x
][y
].charAt(1);
317 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
320 appear: [{ x: x
, y: y
, c: color
, p: k
}],
321 vanish: [{ x: x
, y: y
, c: color
, p: curDir
}],
322 start: { x: x
, y: y
},
323 end: { x: jsq
[0], y: jsq
[1] }
334 moves
= this.getPotentialJailerMoves([x
, y
]);
337 moves
= this.getPotentialSentryMoves([x
, y
]);
340 moves
= this.getPotentialLancerMoves([x
, y
]);
343 moves
= super.getPotentialMovesFrom([x
, y
]);
346 if (!!this.sentryPush
[L
-1]) {
347 // Delete moves walking back on sentry push path,
348 // only if not a pawn, and the piece is the pushed one.
349 const pl
= this.sentryPush
[L
-1].length
;
350 const finalPushedSq
= this.sentryPush
[L
-1][pl
-1];
351 moves
= moves
.filter(m
=> {
353 m
.vanish
[0].p
!= V
.PAWN
&&
354 m
.start
.x
== finalPushedSq
.x
&& m
.start
.y
== finalPushedSq
.y
&&
355 this.sentryPush
[L
-1].some(sq
=> sq
.x
== m
.end
.x
&& sq
.y
== m
.end
.y
)
361 } else if (this.subTurn
== 2) {
362 // Put back the sentinel on board:
363 const color
= this.turn
;
365 m
.appear
.push({x: x
, y: y
, p: V
.SENTRY
, c: color
});
371 getPotentialPawnMoves([x
, y
]) {
372 const color
= this.getColor(x
, y
);
374 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
375 let shiftX
= (color
== "w" ? -1 : 1);
376 if (this.subTurn
== 2) shiftX
*= -1;
377 const firstRank
= color
== "w" ? sizeX
- 1 : 0;
378 const startRank
= color
== "w" ? sizeX
- 2 : 1;
379 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
381 // Pawns might be pushed on 1st rank and attempt to move again:
382 if (!V
.OnBoard(x
+ shiftX
, y
)) return [];
385 // A push cannot put a pawn on last rank (it goes backward)
386 x
+ shiftX
== lastRank
387 ? Object
.keys(V
.LANCER_DIRS
).concat(
388 [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.SENTRY
, V
.JAILER
])
390 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
391 // One square forward
392 for (let piece
of finalPieces
) {
394 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
401 // 2-squares jumps forbidden if pawn push
403 [startRank
, firstRank
].includes(x
) &&
404 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
407 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
411 for (let shiftY
of [-1, 1]) {
414 y
+ shiftY
< sizeY
&&
415 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
416 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
418 for (let piece
of finalPieces
) {
420 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
429 // En passant: only on subTurn == 1
430 const Lep
= this.epSquares
.length
;
431 const epSquare
= this.epSquares
[Lep
- 1];
435 epSquare
.x
== x
+ shiftX
&&
436 Math
.abs(epSquare
.y
- y
) == 1
438 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
439 enpassantMove
.vanish
.push({
443 c: this.getColor(x
, epSquare
.y
)
445 moves
.push(enpassantMove
);
451 // Obtain all lancer moves in "step" direction
452 getPotentialLancerMoves_aux([x
, y
], step
, tr
) {
454 // Add all moves to vacant squares until opponent is met:
455 const color
= this.getColor(x
, y
);
459 // at subTurn == 2, consider own pieces as opponent
461 let sq
= [x
+ step
[0], y
+ step
[1]];
462 while (V
.OnBoard(sq
[0], sq
[1]) && this.getColor(sq
[0], sq
[1]) != oppCol
) {
463 if (this.board
[sq
[0]][sq
[1]] == V
.EMPTY
)
464 moves
.push(this.getBasicMove([x
, y
], sq
, tr
));
468 if (V
.OnBoard(sq
[0], sq
[1]))
469 // Add capturing move
470 moves
.push(this.getBasicMove([x
, y
], sq
, tr
));
474 getPotentialLancerMoves([x
, y
]) {
476 // Add all lancer possible orientations, similar to pawn promotions.
477 // Except if just after a push: allow all movements from init square then
478 const L
= this.sentryPush
.length
;
479 const color
= this.getColor(x
, y
);
480 if (!!this.sentryPush
[L
-1]) {
481 // Maybe I was pushed
482 const pl
= this.sentryPush
[L
-1].length
;
484 this.sentryPush
[L
-1][pl
-1].x
== x
&&
485 this.sentryPush
[L
-1][pl
-1].y
== y
487 // I was pushed: allow all directions (for this move only), but
488 // do not change direction after moving, *except* if I keep the
489 // same orientation in which I was pushed.
490 const curDir
= V
.LANCER_DIRS
[this.board
[x
][y
].charAt(1)];
491 Object
.values(V
.LANCER_DIRS
).forEach(step
=> {
492 const dirCode
= Object
.keys(V
.LANCER_DIRS
).find(k
=> {
494 V
.LANCER_DIRS
[k
][0] == step
[0] &&
495 V
.LANCER_DIRS
[k
][1] == step
[1]
499 this.getPotentialLancerMoves_aux(
502 { p: dirCode
, c: color
}
504 if (curDir
[0] == step
[0] && curDir
[1] == step
[1]) {
505 // Keeping same orientation: can choose after
506 let chooseMoves
= [];
507 dirMoves
.forEach(m
=> {
508 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
509 let mk
= JSON
.parse(JSON
.stringify(m
));
514 Array
.prototype.push
.apply(moves
, chooseMoves
);
515 } else Array
.prototype.push
.apply(moves
, dirMoves
);
520 // I wasn't pushed: standard lancer move
521 const dirCode
= this.board
[x
][y
][1];
523 this.getPotentialLancerMoves_aux([x
, y
], V
.LANCER_DIRS
[dirCode
]);
524 // Add all possible orientations aftermove except if I'm being pushed
525 if (this.subTurn
== 1) {
526 monodirMoves
.forEach(m
=> {
527 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
528 let mk
= JSON
.parse(JSON
.stringify(m
));
535 // I'm pushed: add potential nudges
536 let potentialNudges
= [];
537 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
539 V
.OnBoard(x
+ step
[0], y
+ step
[1]) &&
540 this.board
[x
+ step
[0]][y
+ step
[1]] == V
.EMPTY
542 const newDirCode
= Object
.keys(V
.LANCER_DIRS
).find(k
=> {
543 const codeStep
= V
.LANCER_DIRS
[k
];
544 return (codeStep
[0] == step
[0] && codeStep
[1] == step
[1]);
546 potentialNudges
.push(
549 [x
+ step
[0], y
+ step
[1]],
550 { c: color
, p: newDirCode
}
555 return monodirMoves
.concat(potentialNudges
);
559 getPotentialSentryMoves([x
, y
]) {
560 // The sentry moves a priori like a bishop:
561 let moves
= super.getPotentialBishopMoves([x
, y
]);
562 // ...but captures are replaced by special move, if and only if
563 // "captured" piece can move now, considered as the capturer unit.
564 // --> except is subTurn == 2, in this case I don't push anything.
565 if (this.subTurn
== 2) return moves
.filter(m
=> m
.vanish
.length
== 1);
567 if (m
.vanish
.length
== 2) {
568 // Temporarily cancel the sentry capture:
573 const color
= this.getColor(x
, y
);
574 const fMoves
= moves
.filter(m
=> {
575 // Can the pushed unit make any move? ...resulting in a non-self-check?
576 if (m
.appear
.length
== 0) {
579 let moves2
= this.getPotentialMovesFrom([m
.end
.x
, m
.end
.y
]);
580 for (let m2
of moves2
) {
582 res
= !this.underCheck(color
);
594 getPotentialJailerMoves([x
, y
]) {
595 return super.getPotentialRookMoves([x
, y
]).filter(m
=> {
596 // Remove jailer captures
597 return m
.vanish
[0].p
!= V
.JAILER
|| m
.vanish
.length
== 1;
601 getPotentialKingMoves(sq
) {
602 const moves
= this.getSlideNJumpMoves(
604 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
609 ? moves
.concat(this.getCastleMoves(sq
))
615 // If in second-half of a move, we already know that a move is possible
616 if (this.subTurn
== 2) return true;
617 return super.atLeastOneMove();
621 if (moves
.length
== 0) return [];
622 const basicFilter
= (m
, c
) => {
624 const res
= !this.underCheck(c
);
628 // Disable check tests for sentry pushes,
629 // because in this case the move isn't finished
630 let movesWithoutSentryPushes
= [];
631 let movesWithSentryPushes
= [];
633 // Second condition below for special king "pass" moves
634 if (m
.appear
.length
> 0 || m
.vanish
.length
== 0)
635 movesWithoutSentryPushes
.push(m
);
636 else movesWithSentryPushes
.push(m
);
638 const color
= this.turn
;
639 const oppCol
= V
.GetOppCol(color
);
640 const filteredMoves
=
641 movesWithoutSentryPushes
.filter(m
=> basicFilter(m
, color
));
642 // If at least one full move made, everything is allowed.
643 // Else: forbid checks and captures.
647 : filteredMoves
.filter(m
=> {
648 return (m
.vanish
.length
<= 1 && basicFilter(m
, oppCol
));
650 ).concat(movesWithSentryPushes
);
654 if (this.subTurn
== 1) return super.getAllValidMoves();
656 const sentrySq
= [this.sentryPos
.x
, this.sentryPos
.y
];
657 return this.filterValid(this.getPotentialMovesFrom(sentrySq
));
660 isAttacked(sq
, color
) {
662 super.isAttacked(sq
, color
) ||
663 this.isAttackedByLancer(sq
, color
) ||
664 this.isAttackedBySentry(sq
, color
)
665 // The jailer doesn't capture.
669 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
670 for (let step
of steps
) {
671 let rx
= x
+ step
[0],
673 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
679 this.getPiece(rx
, ry
) == piece
&&
680 this.getColor(rx
, ry
) == color
&&
681 !this.isImmobilized([rx
, ry
])
689 isAttackedByPawn([x
, y
], color
) {
690 const pawnShift
= (color
== "w" ? 1 : -1);
691 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< V
.size
.x
) {
692 for (let i
of [-1, 1]) {
696 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
697 this.getColor(x
+ pawnShift
, y
+ i
) == color
&&
698 !this.isImmobilized([x
+ pawnShift
, y
+ i
])
707 isAttackedByLancer([x
, y
], color
) {
708 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
709 // If in this direction there are only enemy pieces and empty squares,
710 // and we meet a lancer: can he reach us?
711 // NOTE: do not stop at first lancer, there might be several!
712 let coord
= { x: x
+ step
[0], y: y
+ step
[1] };
715 V
.OnBoard(coord
.x
, coord
.y
) &&
717 this.board
[coord
.x
][coord
.y
] == V
.EMPTY
||
718 this.getColor(coord
.x
, coord
.y
) == color
722 this.getPiece(coord
.x
, coord
.y
) == V
.LANCER
&&
723 !this.isImmobilized([coord
.x
, coord
.y
])
725 lancerPos
.push({x: coord
.x
, y: coord
.y
});
730 for (let xy
of lancerPos
) {
731 const dir
= V
.LANCER_DIRS
[this.board
[xy
.x
][xy
.y
].charAt(1)];
732 if (dir
[0] == -step
[0] && dir
[1] == -step
[1]) return true;
738 // Helper to check sentries attacks:
739 selfAttack([x1
, y1
], [x2
, y2
]) {
740 const color
= this.getColor(x1
, y1
);
741 const oppCol
= V
.GetOppCol(color
);
742 const sliderAttack
= (allowedSteps
, lancer
) => {
743 const deltaX
= x2
- x1
,
744 absDeltaX
= Math
.abs(deltaX
);
745 const deltaY
= y2
- y1
,
746 absDeltaY
= Math
.abs(deltaY
);
747 const step
= [ deltaX
/ absDeltaX
|| 0, deltaY
/ absDeltaY
|| 0 ];
749 // Check that the step is a priori valid:
750 (absDeltaX
!= absDeltaY
&& deltaX
!= 0 && deltaY
!= 0) ||
751 allowedSteps
.every(st
=> st
[0] != step
[0] || st
[1] != step
[1])
755 let sq
= [ x1
+ step
[0], y1
+ step
[1] ];
756 while (sq
[0] != x2
|| sq
[1] != y2
) {
758 // NOTE: no need to check OnBoard in this special case
759 (!lancer
&& this.board
[sq
[0]][sq
[1]] != V
.EMPTY
) ||
760 (!!lancer
&& this.getColor(sq
[0], sq
[1]) == oppCol
)
769 switch (this.getPiece(x1
, y1
)) {
771 // Pushed pawns move as enemy pawns
772 const shift
= (color
== 'w' ? 1 : -1);
773 return (x1
+ shift
== x2
&& Math
.abs(y1
- y2
) == 1);
776 const deltaX
= Math
.abs(x1
- x2
);
777 const deltaY
= Math
.abs(y1
- y2
);
779 deltaX
+ deltaY
== 3 &&
780 [1, 2].includes(deltaX
) &&
781 [1, 2].includes(deltaY
)
785 return sliderAttack(V
.steps
[V
.ROOK
]);
787 return sliderAttack(V
.steps
[V
.BISHOP
]);
789 return sliderAttack(V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]));
791 // Special case: as long as no enemy units stands in-between, it attacks
792 // (if it points toward the king).
793 const allowedStep
= V
.LANCER_DIRS
[this.board
[x1
][y1
].charAt(1)];
794 return sliderAttack([allowedStep
], "lancer");
796 // No sentries or jailer tests: they cannot self-capture
801 isAttackedBySentry([x
, y
], color
) {
802 // Attacked by sentry means it can self-take our king.
803 // Just check diagonals of enemy sentry(ies), and if it reaches
804 // one of our pieces: can I self-take?
805 const myColor
= V
.GetOppCol(color
);
807 for (let i
=0; i
<V
.size
.x
; i
++) {
808 for (let j
=0; j
<V
.size
.y
; j
++) {
810 this.getPiece(i
,j
) == V
.SENTRY
&&
811 this.getColor(i
,j
) == color
&&
812 !this.isImmobilized([i
, j
])
814 for (let step
of V
.steps
[V
.BISHOP
]) {
815 let sq
= [ i
+ step
[0], j
+ step
[1] ];
817 V
.OnBoard(sq
[0], sq
[1]) &&
818 this.board
[sq
[0]][sq
[1]] == V
.EMPTY
824 V
.OnBoard(sq
[0], sq
[1]) &&
825 this.getColor(sq
[0], sq
[1]) == myColor
827 candidates
.push([ sq
[0], sq
[1] ]);
833 for (let c
of candidates
)
834 if (this.selfAttack(c
, [x
, y
])) return true;
838 // Jailer doesn't capture or give check
841 if (move.appear
.length
== 0 && move.vanish
.length
== 1)
842 // The sentry is about to push a piece: subTurn goes from 1 to 2
843 this.sentryPos
= { x: move.end
.x
, y: move.end
.y
};
844 if (this.subTurn
== 2 && move.vanish
[0].p
!= V
.PAWN
) {
845 // A piece is pushed: forbid array of squares between start and end
846 // of move, included (except if it's a pawn)
848 if ([V
.KNIGHT
,V
.KING
].includes(move.vanish
[0].p
))
849 // short-range pieces: just forbid initial square
850 squares
.push({ x: move.start
.x
, y: move.start
.y
});
852 const deltaX
= move.end
.x
- move.start
.x
;
853 const deltaY
= move.end
.y
- move.start
.y
;
855 deltaX
/ Math
.abs(deltaX
) || 0,
856 deltaY
/ Math
.abs(deltaY
) || 0
859 let sq
= {x: move.start
.x
, y: move.start
.y
};
860 sq
.x
!= move.end
.x
|| sq
.y
!= move.end
.y
;
861 sq
.x
+= step
[0], sq
.y
+= step
[1]
863 squares
.push({ x: sq
.x
, y: sq
.y
});
866 // Add end square as well, to know if I was pushed (useful for lancers)
867 squares
.push({ x: move.end
.x
, y: move.end
.y
});
868 this.sentryPush
.push(squares
);
869 } else this.sentryPush
.push(null);
874 move.flags
= JSON
.stringify(this.aggregateFlags());
875 this.epSquares
.push(this.getEpSquare(move));
876 V
.PlayOnBoard(this.board
, move);
877 // Is it a sentry push? (useful for undo)
878 move.sentryPush
= (this.subTurn
== 2);
879 if (this.subTurn
== 1) this.movesCount
++;
880 if (move.appear
.length
== 0 && move.vanish
.length
== 1) this.subTurn
= 2;
882 // Turn changes only if not a sentry "pre-push"
883 this.turn
= V
.GetOppCol(this.turn
);
890 if (move.vanish
.length
== 0 || this.subTurn
== 2)
891 // Special pass move of the king, or sentry pre-push: nothing to update
893 const c
= move.vanish
[0].c
;
894 const piece
= move.vanish
[0].p
;
895 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
897 if (piece
== V
.KING
) {
898 this.kingPos
[c
][0] = move.appear
[0].x
;
899 this.kingPos
[c
][1] = move.appear
[0].y
;
900 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
903 // Update castling flags if rooks are moved
904 const oppCol
= V
.GetOppCol(c
);
905 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
907 move.start
.x
== firstRank
&& //our rook moves?
908 this.castleFlags
[c
].includes(move.start
.y
)
910 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
911 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
913 move.end
.x
== oppFirstRank
&& //we took opponent rook?
914 this.castleFlags
[oppCol
].includes(move.end
.y
)
916 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
917 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
922 this.epSquares
.pop();
923 this.disaggregateFlags(JSON
.parse(move.flags
));
924 V
.UndoOnBoard(this.board
, move);
925 // Decrement movesCount except if the move is a sentry push
926 if (!move.sentryPush
) this.movesCount
--;
927 if (this.subTurn
== 2) this.subTurn
= 1;
929 this.turn
= V
.GetOppCol(this.turn
);
930 if (move.sentryPush
) this.subTurn
= 2;
936 super.postUndo(move);
937 this.sentryPush
.pop();
940 static get VALUES() {
941 return Object
.assign(
942 { l: 4.8, s: 2.8, j: 3.8 }, //Jeff K. estimations
948 const maxeval
= V
.INFINITY
;
949 const color
= this.turn
;
950 let moves1
= this.getAllValidMoves();
952 if (moves1
.length
== 0)
953 // TODO: this situation should not happen
956 const setEval
= (move, next
) => {
957 const score
= this.getCurrentScore();
958 const curEval
= move.eval
;
963 : (score
== "1-0" ? 1 : -1) * maxeval
;
964 } else move.eval
= this.evalPosition();
966 // "next" is defined after sentry pushes
969 color
== 'w' && move.eval
> curEval
||
970 color
== 'b' && move.eval
< curEval
977 // Just search_depth == 1 (because of sentries. TODO: can do better...)
978 moves1
.forEach(m1
=> {
980 if (this.subTurn
== 1) setEval(m1
);
982 // Need to play every pushes and count:
983 const moves2
= this.getAllValidMoves();
984 moves2
.forEach(m2
=> {
993 moves1
.sort((a
, b
) => {
994 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
996 let candidates
= [0];
997 for (let j
= 1; j
< moves1
.length
&& moves1
[j
].eval
== moves1
[0].eval
; j
++)
999 const choice
= moves1
[candidates
[randInt(candidates
.length
)]];
1000 return (!choice
.second
? choice : [choice
, choice
.second
]);
1003 // For moves notation:
1004 static get LANCER_DIRNAMES() {
1018 // Special case "king takes jailer" is a pass move
1019 if (move.appear
.length
== 0 && move.vanish
.length
== 0) return "pass";
1020 let notation
= undefined;
1021 if (this.subTurn
== 2) {
1022 // Do not consider appear[1] (sentry) for sentry pushes
1023 const simpleMove
= {
1024 appear: [move.appear
[0]],
1025 vanish: move.vanish
,
1029 notation
= super.getNotation(simpleMove
);
1030 } else notation
= super.getNotation(move);
1031 if (Object
.keys(V
.LANCER_DIRNAMES
).includes(move.vanish
[0].p
))
1032 // Lancer: add direction info
1033 notation
+= "=" + V
.LANCER_DIRNAMES
[move.appear
[0].p
];
1035 move.vanish
[0].p
== V
.PAWN
&&
1036 Object
.keys(V
.LANCER_DIRNAMES
).includes(move.appear
[0].p
)
1038 // Fix promotions in lancer:
1039 notation
= notation
.slice(0, -1) + "L:" + V
.LANCER_DIRNAMES
[move.appear
[0].p
];