1 import { ChessRules
} from "@/base_rules";
3 export const VariantRules
= class ZenRules
extends ChessRules
{
4 // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare
5 static get HasEnpassant() {
9 // TODO(?): some duplicated code in 2 next functions
10 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
12 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
13 const step
= steps
[loop
];
16 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
17 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
18 if (oneStep
) continue outerLoop
;
22 // No capture check: handled elsewhere (next method)
27 // follow steps from x,y until something is met.
28 // if met piece is opponent and same movement (asA): eat it!
29 findCaptures_aux([x
, y
], asA
) {
30 const color
= this.getColor(x
, y
);
35 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
46 const oneStep
= [V
.KNIGHT
,V
.PAWN
].includes(asA
); //we don't capture king
47 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
48 const promotionPieces
= [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
49 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
50 const step
= steps
[loop
];
53 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
54 if (oneStep
) continue outerLoop
;
60 this.getColor(i
, j
) == V
.GetOppCol(color
) &&
61 this.getPiece(i
, j
) == asA
64 if (this.getPiece(x
, y
) == V
.PAWN
&& i
== lastRank
) {
65 // Special case of promotion:
66 promotionPieces
.forEach(p
=> {
67 moves
.push(this.getBasicMove([x
, y
], [i
, j
], { c: color
, p: p
}));
71 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
78 // Find possible captures from a square: look in every direction!
82 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.PAWN
));
83 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.ROOK
));
84 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.KNIGHT
));
85 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.BISHOP
));
86 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.QUEEN
));
92 return false; //captures handled separately
95 getPotentialPawnMoves([x
, y
]) {
96 let moves
= super.getPotentialPawnMoves([x
, y
]);
98 Array
.prototype.push
.apply(moves
, this.findCaptures([x
, y
]));
102 getPotentialRookMoves(sq
) {
103 let noCaptures
= this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]);
104 let captures
= this.findCaptures(sq
);
105 return noCaptures
.concat(captures
);
108 getPotentialKnightMoves(sq
) {
109 let noCaptures
= this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep");
110 let captures
= this.findCaptures(sq
);
111 return noCaptures
.concat(captures
);
114 getPotentialBishopMoves(sq
) {
115 let noCaptures
= this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]);
116 let captures
= this.findCaptures(sq
);
117 return noCaptures
.concat(captures
);
120 getPotentialQueenMoves(sq
) {
121 let noCaptures
= this.getSlideNJumpMoves(
123 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
125 let captures
= this.findCaptures(sq
);
126 return noCaptures
.concat(captures
);
129 getPotentialKingMoves(sq
) {
130 // Initialize with normal moves
131 let noCaptures
= this.getSlideNJumpMoves(
133 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
136 let captures
= this.findCaptures(sq
);
137 return noCaptures
.concat(captures
).concat(this.getCastleMoves(sq
));
141 // Recognize special moves first
142 if (move.appear
.length
== 2) {
144 if (move.end
.y
< move.start
.y
) return "0-0-0";
148 // Translate initial square (because pieces may fly unusually in this variant!)
149 const initialSquare
= V
.CoordsToSquare(move.start
);
151 // Translate final square
152 const finalSquare
= V
.CoordsToSquare(move.end
);
155 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
156 if (piece
== V
.PAWN
) {
157 // pawn move (TODO: enPassant indication)
158 if (move.vanish
.length
> 1) {
160 notation
= initialSquare
+ "x" + finalSquare
;
162 else notation
= finalSquare
;
163 if (piece
!= move.appear
[0].p
)
165 notation
+= "=" + move.appear
[0].p
.toUpperCase();
168 notation
= piece
.toUpperCase();
169 if (move.vanish
.length
> 1) notation
+= initialSquare
+ "x";
170 notation
+= finalSquare
;
175 static get VALUES() {