1 import { randInt
} from "@/utils/alea";
2 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
4 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(m
, orientation
) {
89 m
.appear
[0].c
+ m
.appear
[0].p
,
97 static ParseFen(fen
) {
98 const fenParts
= fen
.split(" ");
100 ChessRules
.ParseFen(fen
),
101 { sentrypush: fenParts
[5] }
105 static IsGoodFen(fen
) {
106 if (!ChessRules
.IsGoodFen(fen
)) return false;
107 const fenParsed
= V
.ParseFen(fen
);
108 // 5) Check sentry push (if any)
110 fenParsed
.sentrypush
!= "-" &&
111 !fenParsed
.sentrypush
.match(/^([a-h][1-8]){2,2}$/)
119 return super.getFen() + " " + this.getSentrypushFen();
123 return super.getFenForRepeat() + "_" + this.getSentrypushFen();
127 const L
= this.sentryPush
.length
;
128 if (!this.sentryPush
[L
-1]) return "-";
130 const spL
= this.sentryPush
[L
-1].length
;
131 // Condensate path: just need initial and final squares:
133 .map(i
=> V
.CoordsToSquare(this.sentryPush
[L
-1][i
]))
137 setOtherVariables(fen
) {
138 super.setOtherVariables(fen
);
139 // subTurn == 2 only when a sentry moved, and is about to push something
141 // Sentry position just after a "capture" (subTurn from 1 to 2)
142 this.sentryPos
= null;
143 // Stack pieces' forbidden squares after a sentry move at each turn
144 const parsedFen
= V
.ParseFen(fen
);
145 if (parsedFen
.sentrypush
== "-") this.sentryPush
= [null];
147 // Expand init + dest squares into a full path:
148 const init
= V
.SquareToCoords(parsedFen
.sentrypush
.substr(0, 2)),
149 dest
= V
.SquareToCoords(parsedFen
.sentrypush
.substr(2));
150 let newPath
= [init
];
151 const delta
= ['x', 'y'].map(i
=> Math
.abs(dest
[i
] - init
[i
]));
152 // Check that it's not a knight movement:
153 if (delta
[0] == 0 || delta
[1] == 0 || delta
[0] == delta
[1]) {
154 const step
= ['x', 'y'].map((i
, idx
) => {
155 return (dest
[i
] - init
[i
]) / delta
[idx
] || 0
157 let x
= init
.x
+ step
[0],
158 y
= init
.y
+ step
[1];
159 while (x
!= dest
.x
|| y
!= dest
.y
) {
160 newPath
.push({ x: x
, y: y
});
166 this.sentryPush
= [newPath
];
170 static GenRandInitFen(options
) {
171 if (options
.randomness
== 0)
172 return "jfsqkbnr/pppppppp/8/8/8/8/PPPPPPPP/JDSQKBNR w 0 ahah - -";
174 const baseFen
= ChessRules
.GenRandInitFen(options
);
175 const fenParts
= baseFen
.split(' ');
176 const posParts
= fenParts
[0].split('/');
178 // Replace one bishop by sentry, so that sentries on different colors
179 // Also replace one random rook by jailer,
180 // and one random knight by lancer (facing north/south)
182 // "replaced" array contains -2 initially, then either -1 if skipped,
183 // or (eventually) the index of replacement:
184 let newPos
= { 0: "", 7: "" };
185 let sentryOddity
= -1;
187 if (options
.randomness
== 1) replaced
= { 'b': -2, 'n': -2, 'r': -2 };
188 for (let rank
of [0, 7]) {
189 if (options
.randomness
== 2) replaced
= { 'b': -2, 'n': -2, 'r': -2 };
190 for (let i
= 0; i
< 8; i
++) {
191 const curChar
= posParts
[rank
].charAt(i
).toLowerCase();
192 if (['b', 'n', 'r'].includes(curChar
)) {
194 replaced
[curChar
] == -1 ||
195 (curChar
== 'b' && rank
== 7 && i
% 2 == sentryOddity
) ||
197 (curChar
!= 'b' || rank
== 0) &&
198 replaced
[curChar
] == -2 &&
202 replaced
[curChar
] = i
;
203 if (curChar
== 'b') {
204 if (sentryOddity
< 0) sentryOddity
= i
% 2;
207 else if (curChar
== 'r') newPos
[rank
] += 'j';
209 // Lancer: orientation depends on side
210 newPos
[rank
] += (rank
== 0 ? 'g' : 'c');
213 if (replaced
[curChar
] == -2) replaced
[curChar
]++;
214 newPos
[rank
] += curChar
;
217 else newPos
[rank
] += curChar
;
222 newPos
[0] + "/" + posParts
.slice(1, 7).join('/') + "/" +
223 newPos
[7].toUpperCase() + " " + fenParts
.slice(1, 5).join(' ') + " -"
227 canTake([x1
, y1
], [x2
, y2
]) {
228 if (this.subTurn
== 2)
229 // Only self captures on this subturn:
230 return this.getColor(x1
, y1
) == this.getColor(x2
, y2
);
231 return super.canTake([x1
, y1
], [x2
, y2
]);
234 // Is piece on square (x,y) immobilized?
235 isImmobilized([x
, y
]) {
236 const color
= this.getColor(x
, y
);
237 const oppCol
= V
.GetOppCol(color
);
238 for (let step
of V
.steps
[V
.ROOK
]) {
239 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
242 this.board
[i
][j
] != V
.EMPTY
&&
243 this.getColor(i
, j
) == oppCol
245 if (this.getPiece(i
, j
) == V
.JAILER
) return [i
, j
];
251 canIplay(side
, [x
, y
]) {
253 (this.subTurn
== 1 && this.turn
== side
&& this.getColor(x
, y
) == side
)
255 (this.subTurn
== 2 && x
== this.sentryPos
.x
&& y
== this.sentryPos
.y
)
259 getPotentialMovesFrom([x
, y
]) {
260 const piece
= this.getPiece(x
, y
);
261 const L
= this.sentryPush
.length
;
262 // At subTurn == 2, jailers aren't effective (Jeff K)
263 if (this.subTurn
== 1) {
264 const jsq
= this.isImmobilized([x
, y
]);
267 // Special pass move if king:
268 if (piece
== V
.KING
) {
273 start: { x: x
, y: y
},
274 end: { x: jsq
[0], y: jsq
[1] }
278 else if (piece
== V
.LANCER
&& !!this.sentryPush
[L
-1]) {
279 // A pushed lancer next to the jailer: reorient
280 const color
= this.getColor(x
, y
);
281 const curDir
= this.board
[x
][y
].charAt(1);
282 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
285 appear: [{ x: x
, y: y
, c: color
, p: k
}],
286 vanish: [{ x: x
, y: y
, c: color
, p: curDir
}],
287 start: { x: x
, y: y
},
288 end: { x: jsq
[0], y: jsq
[1] }
299 moves
= this.getPotentialJailerMoves([x
, y
]);
302 moves
= this.getPotentialSentryMoves([x
, y
]);
305 moves
= this.getPotentialLancerMoves([x
, y
]);
308 moves
= super.getPotentialMovesFrom([x
, y
]);
311 if (!!this.sentryPush
[L
-1]) {
312 // Delete moves walking back on sentry push path,
313 // only if not a pawn, and the piece is the pushed one.
314 const pl
= this.sentryPush
[L
-1].length
;
315 const finalPushedSq
= this.sentryPush
[L
-1][pl
-1];
316 moves
= moves
.filter(m
=> {
318 m
.vanish
[0].p
!= V
.PAWN
&&
319 m
.start
.x
== finalPushedSq
.x
&& m
.start
.y
== finalPushedSq
.y
&&
320 this.sentryPush
[L
-1].some(sq
=> sq
.x
== m
.end
.x
&& sq
.y
== m
.end
.y
)
327 else if (this.subTurn
== 2) {
328 // Put back the sentinel on board:
329 const color
= this.turn
;
331 m
.appear
.push({x: x
, y: y
, p: V
.SENTRY
, c: color
});
337 getPotentialPawnMoves([x
, y
]) {
338 const color
= this.getColor(x
, y
);
340 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
341 let shiftX
= (color
== "w" ? -1 : 1);
342 if (this.subTurn
== 2) shiftX
*= -1;
343 const firstRank
= color
== "w" ? sizeX
- 1 : 0;
344 const startRank
= color
== "w" ? sizeX
- 2 : 1;
345 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
347 // Pawns might be pushed on 1st rank and attempt to move again:
348 if (!V
.OnBoard(x
+ shiftX
, y
)) return [];
350 // A push cannot put a pawn on last rank (it goes backward)
351 let finalPieces
= [V
.PAWN
];
352 if (x
+ shiftX
== lastRank
) {
353 // Only allow direction facing inside board:
354 const allowedLancerDirs
=
356 ? ['e', 'f', 'g', 'h', 'm']
357 : ['c', 'd', 'e', 'm', 'o'];
360 .concat([V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.SENTRY
, V
.JAILER
]);
362 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
363 // One square forward
364 for (let piece
of finalPieces
) {
366 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
373 // 2-squares jumps forbidden if pawn push
375 [startRank
, firstRank
].includes(x
) &&
376 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
379 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
383 for (let shiftY
of [-1, 1]) {
386 y
+ shiftY
< sizeY
&&
387 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
388 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
390 for (let piece
of finalPieces
) {
392 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
401 // En passant: only on subTurn == 1
402 const Lep
= this.epSquares
.length
;
403 const epSquare
= this.epSquares
[Lep
- 1];
407 epSquare
.x
== x
+ shiftX
&&
408 Math
.abs(epSquare
.y
- y
) == 1
410 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
411 enpassantMove
.vanish
.push({
415 c: this.getColor(x
, epSquare
.y
)
417 moves
.push(enpassantMove
);
424 if (isNaN(square
[0])) return null;
425 const L
= this.sentryPush
.length
;
426 const [x
, y
] = [square
[0], square
[1]];
427 const color
= this.turn
;
430 this.board
[x
][y
] == V
.EMPTY
||
431 this.getPiece(x
, y
) != V
.LANCER
||
432 this.getColor(x
, y
) != color
||
433 !!this.sentryPush
[L
-1]
438 const orientation
= this.board
[x
][y
][1];
439 const step
= V
.LANCER_DIRS
[orientation
];
440 if (!V
.OnBoard(x
+ step
[0], y
+ step
[1])) {
442 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
443 const dir
= V
.LANCER_DIRS
[k
];
445 (dir
[0] != step
[0] || dir
[1] != step
[1]) &&
446 V
.OnBoard(x
+ dir
[0], y
+ dir
[1])
466 start: { x: x
, y : y
},
467 end: { x: -1, y: -1 }
477 // Obtain all lancer moves in "step" direction
478 getPotentialLancerMoves_aux([x
, y
], step
, tr
) {
480 // Add all moves to vacant squares until opponent is met:
481 const color
= this.getColor(x
, y
);
485 // at subTurn == 2, consider own pieces as opponent
487 let sq
= [x
+ step
[0], y
+ step
[1]];
488 while (V
.OnBoard(sq
[0], sq
[1]) && this.getColor(sq
[0], sq
[1]) != oppCol
) {
489 if (this.board
[sq
[0]][sq
[1]] == V
.EMPTY
)
490 moves
.push(this.getBasicMove([x
, y
], sq
, tr
));
494 if (V
.OnBoard(sq
[0], sq
[1]))
495 // Add capturing move
496 moves
.push(this.getBasicMove([x
, y
], sq
, tr
));
500 getPotentialLancerMoves([x
, y
]) {
502 // Add all lancer possible orientations, similar to pawn promotions.
503 // Except if just after a push: allow all movements from init square then
504 const L
= this.sentryPush
.length
;
505 const color
= this.getColor(x
, y
);
506 const dirCode
= this.board
[x
][y
][1];
507 const curDir
= V
.LANCER_DIRS
[dirCode
];
508 if (!!this.sentryPush
[L
-1]) {
509 // Maybe I was pushed
510 const pl
= this.sentryPush
[L
-1].length
;
512 this.sentryPush
[L
-1][pl
-1].x
== x
&&
513 this.sentryPush
[L
-1][pl
-1].y
== y
515 // I was pushed: allow all directions (for this move only), but
516 // do not change direction after moving, *except* if I keep the
517 // same orientation in which I was pushed.
518 // Also allow simple reorientation ("capturing king"):
519 if (!V
.OnBoard(x
+ curDir
[0], y
+ curDir
[1])) {
520 const kp
= this.kingPos
[color
];
521 let reorientMoves
= [];
522 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
523 const dir
= V
.LANCER_DIRS
[k
];
525 (dir
[0] != curDir
[0] || dir
[1] != curDir
[1]) &&
526 V
.OnBoard(x
+ dir
[0], y
+ dir
[1])
546 start: { x: x
, y : y
},
547 end: { x: kp
[0], y: kp
[1] }
552 Array
.prototype.push
.apply(moves
, reorientMoves
);
554 Object
.values(V
.LANCER_DIRS
).forEach(step
=> {
555 const dirCode
= Object
.keys(V
.LANCER_DIRS
).find(k
=> {
557 V
.LANCER_DIRS
[k
][0] == step
[0] &&
558 V
.LANCER_DIRS
[k
][1] == step
[1]
562 this.getPotentialLancerMoves_aux(
565 { p: dirCode
, c: color
}
567 if (curDir
[0] == step
[0] && curDir
[1] == step
[1]) {
568 // Keeping same orientation: can choose after
569 let chooseMoves
= [];
570 dirMoves
.forEach(m
=> {
571 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
572 const newDir
= V
.LANCER_DIRS
[k
];
573 // Prevent orientations toward outer board:
574 if (V
.OnBoard(m
.end
.x
+ newDir
[0], m
.end
.y
+ newDir
[1])) {
575 let mk
= JSON
.parse(JSON
.stringify(m
));
577 chooseMoves
.push(mk
);
581 Array
.prototype.push
.apply(moves
, chooseMoves
);
583 else Array
.prototype.push
.apply(moves
, dirMoves
);
588 // I wasn't pushed: standard lancer move
590 this.getPotentialLancerMoves_aux([x
, y
], V
.LANCER_DIRS
[dirCode
]);
591 // Add all possible orientations aftermove except if I'm being pushed
592 if (this.subTurn
== 1) {
593 monodirMoves
.forEach(m
=> {
594 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
595 const newDir
= V
.LANCER_DIRS
[k
];
596 // Prevent orientations toward outer board:
597 if (V
.OnBoard(m
.end
.x
+ newDir
[0], m
.end
.y
+ newDir
[1])) {
598 let mk
= JSON
.parse(JSON
.stringify(m
));
607 // I'm pushed: add potential nudges, except for current orientation
608 let potentialNudges
= [];
609 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
611 (step
[0] != curDir
[0] || step
[1] != curDir
[1]) &&
612 V
.OnBoard(x
+ step
[0], y
+ step
[1]) &&
613 this.board
[x
+ step
[0]][y
+ step
[1]] == V
.EMPTY
615 const newDirCode
= Object
.keys(V
.LANCER_DIRS
).find(k
=> {
616 const codeStep
= V
.LANCER_DIRS
[k
];
617 return (codeStep
[0] == step
[0] && codeStep
[1] == step
[1]);
619 potentialNudges
.push(
622 [x
+ step
[0], y
+ step
[1]],
623 { c: color
, p: newDirCode
}
628 return monodirMoves
.concat(potentialNudges
);
632 getPotentialSentryMoves([x
, y
]) {
633 // The sentry moves a priori like a bishop:
634 let moves
= super.getPotentialBishopMoves([x
, y
]);
635 // ...but captures are replaced by special move, if and only if
636 // "captured" piece can move now, considered as the capturer unit.
637 // --> except is subTurn == 2, in this case I don't push anything.
638 if (this.subTurn
== 2) return moves
.filter(m
=> m
.vanish
.length
== 1);
640 if (m
.vanish
.length
== 2) {
641 // Temporarily cancel the sentry capture:
646 const color
= this.getColor(x
, y
);
647 const fMoves
= moves
.filter(m
=> {
648 // Can the pushed unit make any move? ...resulting in a non-self-check?
649 if (m
.appear
.length
== 0) {
652 let moves2
= this.getPotentialMovesFrom([m
.end
.x
, m
.end
.y
]);
653 for (let m2
of moves2
) {
655 res
= !this.underCheck(color
);
667 getPotentialJailerMoves([x
, y
]) {
668 return super.getPotentialRookMoves([x
, y
]).filter(m
=> {
669 // Remove jailer captures
670 return m
.vanish
[0].p
!= V
.JAILER
|| m
.vanish
.length
== 1;
674 getPotentialKingMoves(sq
) {
675 const moves
= this.getSlideNJumpMoves(
676 sq
, V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), 1);
679 ? moves
.concat(this.getCastleMoves(sq
))
685 // If in second-half of a move, we already know that a move is possible
686 if (this.subTurn
== 2) return true;
687 return super.atLeastOneMove();
691 if (moves
.length
== 0) return [];
692 const basicFilter
= (m
, c
) => {
694 const res
= !this.underCheck(c
);
698 // Disable check tests for sentry pushes,
699 // because in this case the move isn't finished
700 let movesWithoutSentryPushes
= [];
701 let movesWithSentryPushes
= [];
703 // Second condition below for special king "pass" moves
704 if (m
.appear
.length
> 0 || m
.vanish
.length
== 0)
705 movesWithoutSentryPushes
.push(m
);
706 else movesWithSentryPushes
.push(m
);
708 const color
= this.turn
;
709 const oppCol
= V
.GetOppCol(color
);
710 const filteredMoves
=
711 movesWithoutSentryPushes
.filter(m
=> basicFilter(m
, color
));
712 // If at least one full move made, everything is allowed.
713 // Else: forbid checks and captures.
717 : filteredMoves
.filter(m
=> {
718 return (m
.vanish
.length
<= 1 && basicFilter(m
, oppCol
));
720 ).concat(movesWithSentryPushes
);
724 if (this.subTurn
== 1) return super.getAllValidMoves();
726 const sentrySq
= [this.sentryPos
.x
, this.sentryPos
.y
];
727 return this.filterValid(this.getPotentialMovesFrom(sentrySq
));
730 isAttacked(sq
, color
) {
732 super.isAttacked(sq
, color
) ||
733 this.isAttackedByLancer(sq
, color
) ||
734 this.isAttackedBySentry(sq
, color
)
735 // The jailer doesn't capture.
739 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
740 for (let step
of steps
) {
741 let rx
= x
+ step
[0],
743 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
749 this.getPiece(rx
, ry
) == piece
&&
750 this.getColor(rx
, ry
) == color
&&
751 !this.isImmobilized([rx
, ry
])
759 isAttackedByPawn([x
, y
], color
) {
760 const pawnShift
= (color
== "w" ? 1 : -1);
761 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< V
.size
.x
) {
762 for (let i
of [-1, 1]) {
766 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
767 this.getColor(x
+ pawnShift
, y
+ i
) == color
&&
768 !this.isImmobilized([x
+ pawnShift
, y
+ i
])
777 isAttackedByLancer([x
, y
], color
) {
778 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
779 // If in this direction there are only enemy pieces and empty squares,
780 // and we meet a lancer: can he reach us?
781 // NOTE: do not stop at first lancer, there might be several!
782 let coord
= { x: x
+ step
[0], y: y
+ step
[1] };
785 V
.OnBoard(coord
.x
, coord
.y
) &&
787 this.board
[coord
.x
][coord
.y
] == V
.EMPTY
||
788 this.getColor(coord
.x
, coord
.y
) == color
792 this.getPiece(coord
.x
, coord
.y
) == V
.LANCER
&&
793 !this.isImmobilized([coord
.x
, coord
.y
])
795 lancerPos
.push({x: coord
.x
, y: coord
.y
});
800 const L
= this.sentryPush
.length
;
801 const pl
= (!!this.sentryPush
[L
-1] ? this.sentryPush
[L
-1].length : 0);
802 for (let xy
of lancerPos
) {
803 const dir
= V
.LANCER_DIRS
[this.board
[xy
.x
][xy
.y
].charAt(1)];
805 (dir
[0] == -step
[0] && dir
[1] == -step
[1]) ||
806 // If the lancer was just pushed, this is an attack too:
808 !!this.sentryPush
[L
-1] &&
809 this.sentryPush
[L
-1][pl
-1].x
== xy
.x
&&
810 this.sentryPush
[L
-1][pl
-1].y
== xy
.y
820 // Helper to check sentries attacks:
821 selfAttack([x1
, y1
], [x2
, y2
]) {
822 const color
= this.getColor(x1
, y1
);
823 const oppCol
= V
.GetOppCol(color
);
824 const sliderAttack
= (allowedSteps
, lancer
) => {
825 const deltaX
= x2
- x1
,
827 const absDeltaX
= Math
.abs(deltaX
),
828 absDeltaY
= Math
.abs(deltaY
);
829 const step
= [ deltaX
/ absDeltaX
|| 0, deltaY
/ absDeltaY
|| 0 ];
831 // Check that the step is a priori valid:
832 (absDeltaX
!= absDeltaY
&& deltaX
!= 0 && deltaY
!= 0) ||
833 allowedSteps
.every(st
=> st
[0] != step
[0] || st
[1] != step
[1])
837 let sq
= [ x1
+ step
[0], y1
+ step
[1] ];
838 while (sq
[0] != x2
|| sq
[1] != y2
) {
839 // NOTE: no need to check OnBoard in this special case
840 if (this.board
[sq
[0]][sq
[1]] != V
.EMPTY
) {
841 const p
= this.getPiece(sq
[0], sq
[1]);
842 const pc
= this.getColor(sq
[0], sq
[1]);
844 // Enemy sentry on the way will be gone:
845 (p
!= V
.SENTRY
|| pc
!= oppCol
) &&
846 // Lancer temporarily "changed color":
847 (!lancer
|| pc
== color
)
857 switch (this.getPiece(x1
, y1
)) {
859 // Pushed pawns move as enemy pawns
860 const shift
= (color
== 'w' ? 1 : -1);
861 return (x1
+ shift
== x2
&& Math
.abs(y1
- y2
) == 1);
864 const deltaX
= Math
.abs(x1
- x2
);
865 const deltaY
= Math
.abs(y1
- y2
);
867 deltaX
+ deltaY
== 3 &&
868 [1, 2].includes(deltaX
) &&
869 [1, 2].includes(deltaY
)
873 return sliderAttack(V
.steps
[V
.ROOK
]);
875 return sliderAttack(V
.steps
[V
.BISHOP
]);
877 return sliderAttack(V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]));
879 // Special case: as long as no enemy units stands in-between,
880 // it attacks (if it points toward the king).
881 const allowedStep
= V
.LANCER_DIRS
[this.board
[x1
][y1
].charAt(1)];
882 return sliderAttack([allowedStep
], "lancer");
884 // No sentries or jailer tests: they cannot self-capture
889 isAttackedBySentry([x
, y
], color
) {
890 // Attacked by sentry means it can self-take our king.
891 // Just check diagonals of enemy sentry(ies), and if it reaches
892 // one of our pieces: can I self-take?
893 const myColor
= V
.GetOppCol(color
);
895 for (let i
=0; i
<V
.size
.x
; i
++) {
896 for (let j
=0; j
<V
.size
.y
; j
++) {
898 this.getPiece(i
,j
) == V
.SENTRY
&&
899 this.getColor(i
,j
) == color
&&
900 !this.isImmobilized([i
, j
])
902 for (let step
of V
.steps
[V
.BISHOP
]) {
903 let sq
= [ i
+ step
[0], j
+ step
[1] ];
905 V
.OnBoard(sq
[0], sq
[1]) &&
906 this.board
[sq
[0]][sq
[1]] == V
.EMPTY
912 V
.OnBoard(sq
[0], sq
[1]) &&
913 this.getColor(sq
[0], sq
[1]) == myColor
915 candidates
.push([ sq
[0], sq
[1] ]);
921 for (let c
of candidates
)
922 if (this.selfAttack(c
, [x
, y
])) return true;
926 // Jailer doesn't capture or give check
929 if (move.appear
.length
== 0 && move.vanish
.length
== 1)
930 // The sentry is about to push a piece: subTurn goes from 1 to 2
931 this.sentryPos
= { x: move.end
.x
, y: move.end
.y
};
932 if (this.subTurn
== 2 && move.vanish
[0].p
!= V
.PAWN
) {
933 // A piece is pushed: forbid array of squares between start and end
934 // of move, included (except if it's a pawn)
936 if ([V
.KNIGHT
,V
.KING
].includes(move.vanish
[0].p
))
937 // short-range pieces: just forbid initial square
938 squares
.push({ x: move.start
.x
, y: move.start
.y
});
940 const deltaX
= move.end
.x
- move.start
.x
;
941 const deltaY
= move.end
.y
- move.start
.y
;
943 deltaX
/ Math
.abs(deltaX
) || 0,
944 deltaY
/ Math
.abs(deltaY
) || 0
947 let sq
= {x: move.start
.x
, y: move.start
.y
};
948 sq
.x
!= move.end
.x
|| sq
.y
!= move.end
.y
;
949 sq
.x
+= step
[0], sq
.y
+= step
[1]
951 squares
.push({ x: sq
.x
, y: sq
.y
});
954 // Add end square as well, to know if I was pushed (useful for lancers)
955 squares
.push({ x: move.end
.x
, y: move.end
.y
});
956 this.sentryPush
.push(squares
);
957 } else this.sentryPush
.push(null);
962 move.flags
= JSON
.stringify(this.aggregateFlags());
963 this.epSquares
.push(this.getEpSquare(move));
964 V
.PlayOnBoard(this.board
, move);
965 // Is it a sentry push? (useful for undo)
966 move.sentryPush
= (this.subTurn
== 2);
967 if (this.subTurn
== 1) this.movesCount
++;
968 if (move.appear
.length
== 0 && move.vanish
.length
== 1) this.subTurn
= 2;
970 // Turn changes only if not a sentry "pre-push"
971 this.turn
= V
.GetOppCol(this.turn
);
978 if (move.vanish
.length
== 0 || this.subTurn
== 2)
979 // Special pass move of the king, or sentry pre-push: nothing to update
981 const c
= move.vanish
[0].c
;
982 const piece
= move.vanish
[0].p
;
983 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
985 if (piece
== V
.KING
) {
986 this.kingPos
[c
][0] = move.appear
[0].x
;
987 this.kingPos
[c
][1] = move.appear
[0].y
;
988 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
991 // Update castling flags if rooks are moved
992 const oppCol
= V
.GetOppCol(c
);
993 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
995 move.start
.x
== firstRank
&& //our rook moves?
996 this.castleFlags
[c
].includes(move.start
.y
)
998 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
999 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
1001 move.end
.x
== oppFirstRank
&& //we took opponent rook?
1002 this.castleFlags
[oppCol
].includes(move.end
.y
)
1004 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
1005 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
1010 this.epSquares
.pop();
1011 this.disaggregateFlags(JSON
.parse(move.flags
));
1012 V
.UndoOnBoard(this.board
, move);
1013 // Decrement movesCount except if the move is a sentry push
1014 if (!move.sentryPush
) this.movesCount
--;
1015 if (this.subTurn
== 2) this.subTurn
= 1;
1017 this.turn
= V
.GetOppCol(this.turn
);
1018 if (move.sentryPush
) this.subTurn
= 2;
1020 this.postUndo(move);
1024 super.postUndo(move);
1025 this.sentryPush
.pop();
1028 static get VALUES() {
1029 return Object
.assign(
1030 { l: 4.8, s: 2.8, j: 3.8 }, //Jeff K. estimations
1036 const maxeval
= V
.INFINITY
;
1037 const color
= this.turn
;
1038 let moves1
= this.getAllValidMoves();
1040 if (moves1
.length
== 0)
1041 // TODO: this situation should not happen
1044 const setEval
= (move, next
) => {
1045 const score
= this.getCurrentScore();
1046 const curEval
= move.eval
;
1051 : (score
== "1-0" ? 1 : -1) * maxeval
;
1052 } else move.eval
= this.evalPosition();
1054 // "next" is defined after sentry pushes
1057 color
== 'w' && move.eval
> curEval
||
1058 color
== 'b' && move.eval
< curEval
1065 // Just search_depth == 1 (because of sentries. TODO: can do better...)
1066 moves1
.forEach(m1
=> {
1068 if (this.subTurn
== 1) setEval(m1
);
1070 // Need to play every pushes and count:
1071 const moves2
= this.getAllValidMoves();
1072 moves2
.forEach(m2
=> {
1081 moves1
.sort((a
, b
) => {
1082 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
1084 let candidates
= [0];
1085 for (let j
= 1; j
< moves1
.length
&& moves1
[j
].eval
== moves1
[0].eval
; j
++)
1087 const choice
= moves1
[candidates
[randInt(candidates
.length
)]];
1088 return (!choice
.second
? choice : [choice
, choice
.second
]);
1091 // For moves notation:
1092 static get LANCER_DIRNAMES() {
1106 // Special case "king takes jailer" is a pass move
1107 if (move.appear
.length
== 0 && move.vanish
.length
== 0) return "pass";
1108 let notation
= undefined;
1109 if (this.subTurn
== 2) {
1110 // Do not consider appear[1] (sentry) for sentry pushes
1111 const simpleMove
= {
1112 appear: [move.appear
[0]],
1113 vanish: move.vanish
,
1117 notation
= super.getNotation(simpleMove
);
1120 move.appear
.length
> 0 &&
1121 move.vanish
[0].x
== move.appear
[0].x
&&
1122 move.vanish
[0].y
== move.appear
[0].y
1124 // Lancer in-place reorientation:
1125 notation
= "L" + V
.CoordsToSquare(move.start
) + ":R";
1127 else notation
= super.getNotation(move);
1128 if (Object
.keys(V
.LANCER_DIRNAMES
).includes(move.vanish
[0].p
))
1129 // Lancer: add direction info
1130 notation
+= "=" + V
.LANCER_DIRNAMES
[move.appear
[0].p
];
1132 move.vanish
[0].p
== V
.PAWN
&&
1133 Object
.keys(V
.LANCER_DIRNAMES
).includes(move.appear
[0].p
)
1135 // Fix promotions in lancer:
1136 notation
= notation
.slice(0, -1) +
1137 "L:" + V
.LANCER_DIRNAMES
[move.appear
[0].p
];