e16d78f564ebf9e7c76d772a796f0972fd9ad523
[vchess.git] / client / src / variants / Madrasi.js
1 import { ChessRules } from "@/base_rules";
2
3 export class MadrasiRules extends ChessRules {
4
5 isImmobilized(sq) {
6 const oppCol = V.GetOppCol(this.getColor(sq[0], sq[1]));
7 const piece = this.getPiece(sq[0], sq[1]);
8 let steps = [];
9 switch (piece) {
10 // NOTE: cannot use super.isAttackedByXXX
11 // because it would call the redefined isAttackedBySlideNJump
12 // => Infinite recursive calls.
13 case V.PAWN: {
14 const forward = (oppCol == 'w' ? 1 : -1);
15 steps = [[forward, 1], [forward, -1]];
16 break;
17 }
18 case V.ROOK:
19 case V.BISHOP:
20 case V.KNIGHT:
21 steps = V.steps[piece];
22 break;
23 case V.KING:
24 case V.QUEEN:
25 steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
26 break;
27 }
28 const nbSteps = [V.KING, V.PAWN, V.KNIGHT].includes(piece) ? 1 : null;
29 return super.isAttackedBySlideNJump(sq, oppCol, piece, steps, nbSteps);
30 }
31
32 getPotentialMovesFrom([x, y]) {
33 return (
34 !this.isImmobilized([x, y])
35 ? super.getPotentialMovesFrom([x, y])
36 : []
37 );
38 }
39
40 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
41 for (let step of steps) {
42 let rx = x + step[0],
43 ry = y + step[1];
44 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
45 rx += step[0];
46 ry += step[1];
47 }
48 if (
49 V.OnBoard(rx, ry) &&
50 this.getPiece(rx, ry) == piece &&
51 this.getColor(rx, ry) == color &&
52 // If enemy is immobilized, it doesn't attack:
53 !this.isImmobilized([rx, ry])
54 ) {
55 return true;
56 }
57 }
58 return false;
59 }
60
61 isAttackedByKing() {
62 // Connected kings paralyze each other
63 return false;
64 }
65
66 };