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