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;
186 for (let rank
of [0, 7]) {
187 let replaced
= { 'b': -2, 'n': -2, 'r': -2 };
188 for (let i
= 0; i
< 8; i
++) {
189 const curChar
= posParts
[rank
].charAt(i
).toLowerCase();
190 if (['b', 'n', 'r'].includes(curChar
)) {
192 replaced
[curChar
] == -1 ||
193 (curChar
== 'b' && rank
== 7 && i
% 2 == sentryOddity
) ||
195 (curChar
!= 'b' || rank
== 0) &&
196 replaced
[curChar
] == -2 &&
200 replaced
[curChar
] = i
;
201 if (curChar
== 'b') {
202 if (sentryOddity
< 0) sentryOddity
= i
% 2;
205 else if (curChar
== 'r') newPos
[rank
] += 'j';
207 // Lancer: orientation depends on side
208 newPos
[rank
] += (rank
== 0 ? 'g' : 'c');
211 if (replaced
[curChar
] == -2) replaced
[curChar
]++;
212 newPos
[rank
] += curChar
;
215 else newPos
[rank
] += curChar
;
220 newPos
[0] + "/" + posParts
.slice(1, 7).join('/') + "/" +
221 newPos
[7].toUpperCase() + " " + fenParts
.slice(1, 5).join(' ') + " -"
225 canTake([x1
, y1
], [x2
, y2
]) {
226 if (this.subTurn
== 2)
227 // Only self captures on this subturn:
228 return this.getColor(x1
, y1
) == this.getColor(x2
, y2
);
229 return super.canTake([x1
, y1
], [x2
, y2
]);
232 // Is piece on square (x,y) immobilized?
233 isImmobilized([x
, y
]) {
234 const color
= this.getColor(x
, y
);
235 const oppCol
= V
.GetOppCol(color
);
236 for (let step
of V
.steps
[V
.ROOK
]) {
237 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
240 this.board
[i
][j
] != V
.EMPTY
&&
241 this.getColor(i
, j
) == oppCol
243 if (this.getPiece(i
, j
) == V
.JAILER
) return [i
, j
];
249 canIplay(side
, [x
, y
]) {
251 (this.subTurn
== 1 && this.turn
== side
&& this.getColor(x
, y
) == side
)
253 (this.subTurn
== 2 && x
== this.sentryPos
.x
&& y
== this.sentryPos
.y
)
257 getPotentialMovesFrom([x
, y
]) {
258 const piece
= this.getPiece(x
, y
);
259 const L
= this.sentryPush
.length
;
260 // At subTurn == 2, jailers aren't effective (Jeff K)
261 if (this.subTurn
== 1) {
262 const jsq
= this.isImmobilized([x
, y
]);
265 // Special pass move if king:
266 if (piece
== V
.KING
) {
271 start: { x: x
, y: y
},
272 end: { x: jsq
[0], y: jsq
[1] }
276 else if (piece
== V
.LANCER
&& !!this.sentryPush
[L
-1]) {
277 // A pushed lancer next to the jailer: reorient
278 const color
= this.getColor(x
, y
);
279 const curDir
= this.board
[x
][y
].charAt(1);
280 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
283 appear: [{ x: x
, y: y
, c: color
, p: k
}],
284 vanish: [{ x: x
, y: y
, c: color
, p: curDir
}],
285 start: { x: x
, y: y
},
286 end: { x: jsq
[0], y: jsq
[1] }
297 moves
= this.getPotentialJailerMoves([x
, y
]);
300 moves
= this.getPotentialSentryMoves([x
, y
]);
303 moves
= this.getPotentialLancerMoves([x
, y
]);
306 moves
= super.getPotentialMovesFrom([x
, y
]);
309 if (!!this.sentryPush
[L
-1]) {
310 // Delete moves walking back on sentry push path,
311 // only if not a pawn, and the piece is the pushed one.
312 const pl
= this.sentryPush
[L
-1].length
;
313 const finalPushedSq
= this.sentryPush
[L
-1][pl
-1];
314 moves
= moves
.filter(m
=> {
316 m
.vanish
[0].p
!= V
.PAWN
&&
317 m
.start
.x
== finalPushedSq
.x
&& m
.start
.y
== finalPushedSq
.y
&&
318 this.sentryPush
[L
-1].some(sq
=> sq
.x
== m
.end
.x
&& sq
.y
== m
.end
.y
)
325 else if (this.subTurn
== 2) {
326 // Put back the sentinel on board:
327 const color
= this.turn
;
329 m
.appear
.push({x: x
, y: y
, p: V
.SENTRY
, c: color
});
335 getPotentialPawnMoves([x
, y
]) {
336 const color
= this.getColor(x
, y
);
338 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
339 let shiftX
= (color
== "w" ? -1 : 1);
340 if (this.subTurn
== 2) shiftX
*= -1;
341 const firstRank
= color
== "w" ? sizeX
- 1 : 0;
342 const startRank
= color
== "w" ? sizeX
- 2 : 1;
343 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
345 // Pawns might be pushed on 1st rank and attempt to move again:
346 if (!V
.OnBoard(x
+ shiftX
, y
)) return [];
348 // A push cannot put a pawn on last rank (it goes backward)
349 let finalPieces
= [V
.PAWN
];
350 if (x
+ shiftX
== lastRank
) {
351 // Only allow direction facing inside board:
352 const allowedLancerDirs
=
354 ? ['e', 'f', 'g', 'h', 'm']
355 : ['c', 'd', 'e', 'm', 'o'];
358 .concat([V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.SENTRY
, V
.JAILER
]);
360 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
361 // One square forward
362 for (let piece
of finalPieces
) {
364 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
371 // 2-squares jumps forbidden if pawn push
373 [startRank
, firstRank
].includes(x
) &&
374 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
377 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
381 for (let shiftY
of [-1, 1]) {
384 y
+ shiftY
< sizeY
&&
385 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
386 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
388 for (let piece
of finalPieces
) {
390 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
399 // En passant: only on subTurn == 1
400 const Lep
= this.epSquares
.length
;
401 const epSquare
= this.epSquares
[Lep
- 1];
405 epSquare
.x
== x
+ shiftX
&&
406 Math
.abs(epSquare
.y
- y
) == 1
408 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
409 enpassantMove
.vanish
.push({
413 c: this.getColor(x
, epSquare
.y
)
415 moves
.push(enpassantMove
);
422 if (isNaN(square
[0])) return null;
423 const L
= this.sentryPush
.length
;
424 const [x
, y
] = [square
[0], square
[1]];
425 const color
= this.turn
;
428 this.board
[x
][y
] == V
.EMPTY
||
429 this.getPiece(x
, y
) != V
.LANCER
||
430 this.getColor(x
, y
) != color
||
431 !!this.sentryPush
[L
-1]
436 const orientation
= this.board
[x
][y
][1];
437 const step
= V
.LANCER_DIRS
[orientation
];
438 if (!V
.OnBoard(x
+ step
[0], y
+ step
[1])) {
440 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
441 const dir
= V
.LANCER_DIRS
[k
];
443 (dir
[0] != step
[0] || dir
[1] != step
[1]) &&
444 V
.OnBoard(x
+ dir
[0], y
+ dir
[1])
464 start: { x: x
, y : y
},
465 end: { x: -1, y: -1 }
475 // Obtain all lancer moves in "step" direction
476 getPotentialLancerMoves_aux([x
, y
], step
, tr
) {
478 // Add all moves to vacant squares until opponent is met:
479 const color
= this.getColor(x
, y
);
483 // at subTurn == 2, consider own pieces as opponent
485 let sq
= [x
+ step
[0], y
+ step
[1]];
486 while (V
.OnBoard(sq
[0], sq
[1]) && this.getColor(sq
[0], sq
[1]) != oppCol
) {
487 if (this.board
[sq
[0]][sq
[1]] == V
.EMPTY
)
488 moves
.push(this.getBasicMove([x
, y
], sq
, tr
));
492 if (V
.OnBoard(sq
[0], sq
[1]))
493 // Add capturing move
494 moves
.push(this.getBasicMove([x
, y
], sq
, tr
));
498 getPotentialLancerMoves([x
, y
]) {
500 // Add all lancer possible orientations, similar to pawn promotions.
501 // Except if just after a push: allow all movements from init square then
502 const L
= this.sentryPush
.length
;
503 const color
= this.getColor(x
, y
);
504 const dirCode
= this.board
[x
][y
][1];
505 const curDir
= V
.LANCER_DIRS
[dirCode
];
506 if (!!this.sentryPush
[L
-1]) {
507 // Maybe I was pushed
508 const pl
= this.sentryPush
[L
-1].length
;
510 this.sentryPush
[L
-1][pl
-1].x
== x
&&
511 this.sentryPush
[L
-1][pl
-1].y
== y
513 // I was pushed: allow all directions (for this move only), but
514 // do not change direction after moving, *except* if I keep the
515 // same orientation in which I was pushed.
516 // Also allow simple reorientation ("capturing king"):
517 if (!V
.OnBoard(x
+ curDir
[0], y
+ curDir
[1])) {
518 const kp
= this.kingPos
[color
];
519 let reorientMoves
= [];
520 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
521 const dir
= V
.LANCER_DIRS
[k
];
523 (dir
[0] != curDir
[0] || dir
[1] != curDir
[1]) &&
524 V
.OnBoard(x
+ dir
[0], y
+ dir
[1])
544 start: { x: x
, y : y
},
545 end: { x: kp
[0], y: kp
[1] }
550 Array
.prototype.push
.apply(moves
, reorientMoves
);
552 Object
.values(V
.LANCER_DIRS
).forEach(step
=> {
553 const dirCode
= Object
.keys(V
.LANCER_DIRS
).find(k
=> {
555 V
.LANCER_DIRS
[k
][0] == step
[0] &&
556 V
.LANCER_DIRS
[k
][1] == step
[1]
560 this.getPotentialLancerMoves_aux(
563 { p: dirCode
, c: color
}
565 if (curDir
[0] == step
[0] && curDir
[1] == step
[1]) {
566 // Keeping same orientation: can choose after
567 let chooseMoves
= [];
568 dirMoves
.forEach(m
=> {
569 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
570 const newDir
= V
.LANCER_DIRS
[k
];
571 // Prevent orientations toward outer board:
572 if (V
.OnBoard(m
.end
.x
+ newDir
[0], m
.end
.y
+ newDir
[1])) {
573 let mk
= JSON
.parse(JSON
.stringify(m
));
575 chooseMoves
.push(mk
);
579 Array
.prototype.push
.apply(moves
, chooseMoves
);
581 else Array
.prototype.push
.apply(moves
, dirMoves
);
586 // I wasn't pushed: standard lancer move
588 this.getPotentialLancerMoves_aux([x
, y
], V
.LANCER_DIRS
[dirCode
]);
589 // Add all possible orientations aftermove except if I'm being pushed
590 if (this.subTurn
== 1) {
591 monodirMoves
.forEach(m
=> {
592 Object
.keys(V
.LANCER_DIRS
).forEach(k
=> {
593 const newDir
= V
.LANCER_DIRS
[k
];
594 // Prevent orientations toward outer board:
595 if (V
.OnBoard(m
.end
.x
+ newDir
[0], m
.end
.y
+ newDir
[1])) {
596 let mk
= JSON
.parse(JSON
.stringify(m
));
605 // I'm pushed: add potential nudges, except for current orientation
606 let potentialNudges
= [];
607 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
609 (step
[0] != curDir
[0] || step
[1] != curDir
[1]) &&
610 V
.OnBoard(x
+ step
[0], y
+ step
[1]) &&
611 this.board
[x
+ step
[0]][y
+ step
[1]] == V
.EMPTY
613 const newDirCode
= Object
.keys(V
.LANCER_DIRS
).find(k
=> {
614 const codeStep
= V
.LANCER_DIRS
[k
];
615 return (codeStep
[0] == step
[0] && codeStep
[1] == step
[1]);
617 potentialNudges
.push(
620 [x
+ step
[0], y
+ step
[1]],
621 { c: color
, p: newDirCode
}
626 return monodirMoves
.concat(potentialNudges
);
630 getPotentialSentryMoves([x
, y
]) {
631 // The sentry moves a priori like a bishop:
632 let moves
= super.getPotentialBishopMoves([x
, y
]);
633 // ...but captures are replaced by special move, if and only if
634 // "captured" piece can move now, considered as the capturer unit.
635 // --> except is subTurn == 2, in this case I don't push anything.
636 if (this.subTurn
== 2) return moves
.filter(m
=> m
.vanish
.length
== 1);
638 if (m
.vanish
.length
== 2) {
639 // Temporarily cancel the sentry capture:
644 const color
= this.getColor(x
, y
);
645 const fMoves
= moves
.filter(m
=> {
646 // Can the pushed unit make any move? ...resulting in a non-self-check?
647 if (m
.appear
.length
== 0) {
650 let moves2
= this.getPotentialMovesFrom([m
.end
.x
, m
.end
.y
]);
651 for (let m2
of moves2
) {
653 res
= !this.underCheck(color
);
665 getPotentialJailerMoves([x
, y
]) {
666 return super.getPotentialRookMoves([x
, y
]).filter(m
=> {
667 // Remove jailer captures
668 return m
.vanish
[0].p
!= V
.JAILER
|| m
.vanish
.length
== 1;
672 getPotentialKingMoves(sq
) {
673 const moves
= this.getSlideNJumpMoves(
674 sq
, V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), 1);
677 ? moves
.concat(this.getCastleMoves(sq
))
683 // If in second-half of a move, we already know that a move is possible
684 if (this.subTurn
== 2) return true;
685 return super.atLeastOneMove();
689 if (moves
.length
== 0) return [];
690 const basicFilter
= (m
, c
) => {
692 const res
= !this.underCheck(c
);
696 // Disable check tests for sentry pushes,
697 // because in this case the move isn't finished
698 let movesWithoutSentryPushes
= [];
699 let movesWithSentryPushes
= [];
701 // Second condition below for special king "pass" moves
702 if (m
.appear
.length
> 0 || m
.vanish
.length
== 0)
703 movesWithoutSentryPushes
.push(m
);
704 else movesWithSentryPushes
.push(m
);
706 const color
= this.turn
;
707 const oppCol
= V
.GetOppCol(color
);
708 const filteredMoves
=
709 movesWithoutSentryPushes
.filter(m
=> basicFilter(m
, color
));
710 // If at least one full move made, everything is allowed.
711 // Else: forbid checks and captures.
715 : filteredMoves
.filter(m
=> {
716 return (m
.vanish
.length
<= 1 && basicFilter(m
, oppCol
));
718 ).concat(movesWithSentryPushes
);
722 if (this.subTurn
== 1) return super.getAllValidMoves();
724 const sentrySq
= [this.sentryPos
.x
, this.sentryPos
.y
];
725 return this.filterValid(this.getPotentialMovesFrom(sentrySq
));
728 isAttacked(sq
, color
) {
730 super.isAttacked(sq
, color
) ||
731 this.isAttackedByLancer(sq
, color
) ||
732 this.isAttackedBySentry(sq
, color
)
733 // The jailer doesn't capture.
737 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
738 for (let step
of steps
) {
739 let rx
= x
+ step
[0],
741 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
747 this.getPiece(rx
, ry
) == piece
&&
748 this.getColor(rx
, ry
) == color
&&
749 !this.isImmobilized([rx
, ry
])
757 isAttackedByPawn([x
, y
], color
) {
758 const pawnShift
= (color
== "w" ? 1 : -1);
759 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< V
.size
.x
) {
760 for (let i
of [-1, 1]) {
764 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
765 this.getColor(x
+ pawnShift
, y
+ i
) == color
&&
766 !this.isImmobilized([x
+ pawnShift
, y
+ i
])
775 isAttackedByLancer([x
, y
], color
) {
776 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
777 // If in this direction there are only enemy pieces and empty squares,
778 // and we meet a lancer: can he reach us?
779 // NOTE: do not stop at first lancer, there might be several!
780 let coord
= { x: x
+ step
[0], y: y
+ step
[1] };
783 V
.OnBoard(coord
.x
, coord
.y
) &&
785 this.board
[coord
.x
][coord
.y
] == V
.EMPTY
||
786 this.getColor(coord
.x
, coord
.y
) == color
790 this.getPiece(coord
.x
, coord
.y
) == V
.LANCER
&&
791 !this.isImmobilized([coord
.x
, coord
.y
])
793 lancerPos
.push({x: coord
.x
, y: coord
.y
});
798 const L
= this.sentryPush
.length
;
799 const pl
= (!!this.sentryPush
[L
-1] ? this.sentryPush
[L
-1].length : 0);
800 for (let xy
of lancerPos
) {
801 const dir
= V
.LANCER_DIRS
[this.board
[xy
.x
][xy
.y
].charAt(1)];
803 (dir
[0] == -step
[0] && dir
[1] == -step
[1]) ||
804 // If the lancer was just pushed, this is an attack too:
806 !!this.sentryPush
[L
-1] &&
807 this.sentryPush
[L
-1][pl
-1].x
== xy
.x
&&
808 this.sentryPush
[L
-1][pl
-1].y
== xy
.y
818 // Helper to check sentries attacks:
819 selfAttack([x1
, y1
], [x2
, y2
]) {
820 const color
= this.getColor(x1
, y1
);
821 const oppCol
= V
.GetOppCol(color
);
822 const sliderAttack
= (allowedSteps
, lancer
) => {
823 const deltaX
= x2
- x1
,
825 const absDeltaX
= Math
.abs(deltaX
),
826 absDeltaY
= Math
.abs(deltaY
);
827 const step
= [ deltaX
/ absDeltaX
|| 0, deltaY
/ absDeltaY
|| 0 ];
829 // Check that the step is a priori valid:
830 (absDeltaX
!= absDeltaY
&& deltaX
!= 0 && deltaY
!= 0) ||
831 allowedSteps
.every(st
=> st
[0] != step
[0] || st
[1] != step
[1])
835 let sq
= [ x1
+ step
[0], y1
+ step
[1] ];
836 while (sq
[0] != x2
|| sq
[1] != y2
) {
837 // NOTE: no need to check OnBoard in this special case
838 if (this.board
[sq
[0]][sq
[1]] != V
.EMPTY
) {
839 const p
= this.getPiece(sq
[0], sq
[1]);
840 const pc
= this.getColor(sq
[0], sq
[1]);
842 // Enemy sentry on the way will be gone:
843 (p
!= V
.SENTRY
|| pc
!= oppCol
) &&
844 // Lancer temporarily "changed color":
845 (!lancer
|| pc
== color
)
855 switch (this.getPiece(x1
, y1
)) {
857 // Pushed pawns move as enemy pawns
858 const shift
= (color
== 'w' ? 1 : -1);
859 return (x1
+ shift
== x2
&& Math
.abs(y1
- y2
) == 1);
862 const deltaX
= Math
.abs(x1
- x2
);
863 const deltaY
= Math
.abs(y1
- y2
);
865 deltaX
+ deltaY
== 3 &&
866 [1, 2].includes(deltaX
) &&
867 [1, 2].includes(deltaY
)
871 return sliderAttack(V
.steps
[V
.ROOK
]);
873 return sliderAttack(V
.steps
[V
.BISHOP
]);
875 return sliderAttack(V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]));
877 // Special case: as long as no enemy units stands in-between,
878 // it attacks (if it points toward the king).
879 const allowedStep
= V
.LANCER_DIRS
[this.board
[x1
][y1
].charAt(1)];
880 return sliderAttack([allowedStep
], "lancer");
882 // No sentries or jailer tests: they cannot self-capture
887 isAttackedBySentry([x
, y
], color
) {
888 // Attacked by sentry means it can self-take our king.
889 // Just check diagonals of enemy sentry(ies), and if it reaches
890 // one of our pieces: can I self-take?
891 const myColor
= V
.GetOppCol(color
);
893 for (let i
=0; i
<V
.size
.x
; i
++) {
894 for (let j
=0; j
<V
.size
.y
; j
++) {
896 this.getPiece(i
,j
) == V
.SENTRY
&&
897 this.getColor(i
,j
) == color
&&
898 !this.isImmobilized([i
, j
])
900 for (let step
of V
.steps
[V
.BISHOP
]) {
901 let sq
= [ i
+ step
[0], j
+ step
[1] ];
903 V
.OnBoard(sq
[0], sq
[1]) &&
904 this.board
[sq
[0]][sq
[1]] == V
.EMPTY
910 V
.OnBoard(sq
[0], sq
[1]) &&
911 this.getColor(sq
[0], sq
[1]) == myColor
913 candidates
.push([ sq
[0], sq
[1] ]);
919 for (let c
of candidates
)
920 if (this.selfAttack(c
, [x
, y
])) return true;
924 // Jailer doesn't capture or give check
927 if (move.appear
.length
== 0 && move.vanish
.length
== 1)
928 // The sentry is about to push a piece: subTurn goes from 1 to 2
929 this.sentryPos
= { x: move.end
.x
, y: move.end
.y
};
930 if (this.subTurn
== 2 && move.vanish
[0].p
!= V
.PAWN
) {
931 // A piece is pushed: forbid array of squares between start and end
932 // of move, included (except if it's a pawn)
934 if ([V
.KNIGHT
,V
.KING
].includes(move.vanish
[0].p
))
935 // short-range pieces: just forbid initial square
936 squares
.push({ x: move.start
.x
, y: move.start
.y
});
938 const deltaX
= move.end
.x
- move.start
.x
;
939 const deltaY
= move.end
.y
- move.start
.y
;
941 deltaX
/ Math
.abs(deltaX
) || 0,
942 deltaY
/ Math
.abs(deltaY
) || 0
945 let sq
= {x: move.start
.x
, y: move.start
.y
};
946 sq
.x
!= move.end
.x
|| sq
.y
!= move.end
.y
;
947 sq
.x
+= step
[0], sq
.y
+= step
[1]
949 squares
.push({ x: sq
.x
, y: sq
.y
});
952 // Add end square as well, to know if I was pushed (useful for lancers)
953 squares
.push({ x: move.end
.x
, y: move.end
.y
});
954 this.sentryPush
.push(squares
);
955 } else this.sentryPush
.push(null);
960 move.flags
= JSON
.stringify(this.aggregateFlags());
961 this.epSquares
.push(this.getEpSquare(move));
962 V
.PlayOnBoard(this.board
, move);
963 // Is it a sentry push? (useful for undo)
964 move.sentryPush
= (this.subTurn
== 2);
965 if (this.subTurn
== 1) this.movesCount
++;
966 if (move.appear
.length
== 0 && move.vanish
.length
== 1) this.subTurn
= 2;
968 // Turn changes only if not a sentry "pre-push"
969 this.turn
= V
.GetOppCol(this.turn
);
976 if (move.vanish
.length
== 0 || this.subTurn
== 2)
977 // Special pass move of the king, or sentry pre-push: nothing to update
979 const c
= move.vanish
[0].c
;
980 const piece
= move.vanish
[0].p
;
981 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
983 if (piece
== V
.KING
) {
984 this.kingPos
[c
][0] = move.appear
[0].x
;
985 this.kingPos
[c
][1] = move.appear
[0].y
;
986 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
989 // Update castling flags if rooks are moved
990 const oppCol
= V
.GetOppCol(c
);
991 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
993 move.start
.x
== firstRank
&& //our rook moves?
994 this.castleFlags
[c
].includes(move.start
.y
)
996 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
997 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
999 move.end
.x
== oppFirstRank
&& //we took opponent rook?
1000 this.castleFlags
[oppCol
].includes(move.end
.y
)
1002 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
1003 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
1008 this.epSquares
.pop();
1009 this.disaggregateFlags(JSON
.parse(move.flags
));
1010 V
.UndoOnBoard(this.board
, move);
1011 // Decrement movesCount except if the move is a sentry push
1012 if (!move.sentryPush
) this.movesCount
--;
1013 if (this.subTurn
== 2) this.subTurn
= 1;
1015 this.turn
= V
.GetOppCol(this.turn
);
1016 if (move.sentryPush
) this.subTurn
= 2;
1018 this.postUndo(move);
1022 super.postUndo(move);
1023 this.sentryPush
.pop();
1026 static get VALUES() {
1027 return Object
.assign(
1028 { l: 4.8, s: 2.8, j: 3.8 }, //Jeff K. estimations
1034 const maxeval
= V
.INFINITY
;
1035 const color
= this.turn
;
1036 let moves1
= this.getAllValidMoves();
1038 if (moves1
.length
== 0)
1039 // TODO: this situation should not happen
1042 const setEval
= (move, next
) => {
1043 const score
= this.getCurrentScore();
1044 const curEval
= move.eval
;
1049 : (score
== "1-0" ? 1 : -1) * maxeval
;
1050 } else move.eval
= this.evalPosition();
1052 // "next" is defined after sentry pushes
1055 color
== 'w' && move.eval
> curEval
||
1056 color
== 'b' && move.eval
< curEval
1063 // Just search_depth == 1 (because of sentries. TODO: can do better...)
1064 moves1
.forEach(m1
=> {
1066 if (this.subTurn
== 1) setEval(m1
);
1068 // Need to play every pushes and count:
1069 const moves2
= this.getAllValidMoves();
1070 moves2
.forEach(m2
=> {
1079 moves1
.sort((a
, b
) => {
1080 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
1082 let candidates
= [0];
1083 for (let j
= 1; j
< moves1
.length
&& moves1
[j
].eval
== moves1
[0].eval
; j
++)
1085 const choice
= moves1
[candidates
[randInt(candidates
.length
)]];
1086 return (!choice
.second
? choice : [choice
, choice
.second
]);
1089 // For moves notation:
1090 static get LANCER_DIRNAMES() {
1104 // Special case "king takes jailer" is a pass move
1105 if (move.appear
.length
== 0 && move.vanish
.length
== 0) return "pass";
1106 let notation
= undefined;
1107 if (this.subTurn
== 2) {
1108 // Do not consider appear[1] (sentry) for sentry pushes
1109 const simpleMove
= {
1110 appear: [move.appear
[0]],
1111 vanish: move.vanish
,
1115 notation
= super.getNotation(simpleMove
);
1118 move.appear
.length
> 0 &&
1119 move.vanish
[0].x
== move.appear
[0].x
&&
1120 move.vanish
[0].y
== move.appear
[0].y
1122 // Lancer in-place reorientation:
1123 notation
= "L" + V
.CoordsToSquare(move.start
) + ":R";
1125 else notation
= super.getNotation(move);
1126 if (Object
.keys(V
.LANCER_DIRNAMES
).includes(move.vanish
[0].p
))
1127 // Lancer: add direction info
1128 notation
+= "=" + V
.LANCER_DIRNAMES
[move.appear
[0].p
];
1130 move.vanish
[0].p
== V
.PAWN
&&
1131 Object
.keys(V
.LANCER_DIRNAMES
).includes(move.appear
[0].p
)
1133 // Fix promotions in lancer:
1134 notation
= notation
.slice(0, -1) +
1135 "L:" + V
.LANCER_DIRNAMES
[move.appear
[0].p
];