1 class ZenRules
extends ChessRules
3 // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare
9 // TODO: some duplicated code in 2 next functions
10 getSlideNJumpMoves(x
, y
, color
, steps
, oneStep
)
13 let [sizeX
,sizeY
] = VariantRules
.size
;
15 for (var loop
=0; loop
<steps
.length
; loop
++)
17 var step
= steps
[loop
];
20 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
21 && this.board
[i
][j
] == VariantRules
.EMPTY
)
23 moves
.push(this.getBasicMove(x
, y
, i
, j
));
24 if (oneStep
!== undefined)
29 // No capture check: handled elsewhere (next method)
34 // follow steps from x,y until something is met.
35 // if met piece is opponent and same movement (asA): eat it!
36 findCaptures_aux(x
, y
, color
, asA
)
40 var steps
= asA
!= V
.PAWN
42 : color
=='w' ? [[-1,-1],[-1,1]] : [[1,-1],[1,1]];
43 var oneStep
= (asA
==V
.KNIGHT
|| asA
==V
.PAWN
); //we don't capture king
44 let [sizeX
,sizeY
] = V
.size
;
45 let lastRank
= (color
== 'w' ? 0 : sizeY
-1);
46 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
48 for (var loop
=0; loop
<steps
.length
; loop
++)
50 var step
= steps
[loop
];
53 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
54 && this.board
[i
][j
] == V
.EMPTY
)
61 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&&
62 this.getColor(i
,j
) == this.getOppCol(color
) && this.getPiece(i
,j
) == asA
)
65 if (this.getPiece(x
,y
) == V
.PAWN
&& i
== lastRank
)
67 // Special case of promotion:
68 promotionPieces
.forEach(p
=> {
69 moves
.push(this.getBasicMove(x
, y
, i
, j
, p
));
75 moves
.push(this.getBasicMove(x
, y
, i
, j
));
82 // Find possible captures from a square: look in every direction!
83 findCaptures(x
, y
, color
)
88 Array
.prototype.push
.apply(moves
,
89 this.findCaptures_aux(x
, y
, color
, VariantRules
.PAWN
));
92 Array
.prototype.push
.apply(moves
,
93 this.findCaptures_aux(x
, y
, color
, VariantRules
.ROOK
));
96 Array
.prototype.push
.apply(moves
,
97 this.findCaptures_aux(x
, y
, color
, VariantRules
.KNIGHT
));
100 Array
.prototype.push
.apply(moves
,
101 this.findCaptures_aux(x
, y
, color
, VariantRules
.BISHOP
));
104 Array
.prototype.push
.apply(moves
,
105 this.findCaptures_aux(x
, y
, color
, VariantRules
.QUEEN
));
110 getPotentialPawnMoves(x
, y
, color
)
113 var V
= VariantRules
;
114 let [sizeX
,sizeY
] = VariantRules
.size
;
115 let shift
= (color
== 'w' ? -1 : 1);
116 let startRank
= (color
== 'w' ? sizeY
-2 : 1);
117 let lastRank
= (color
== "w" ? 0 : sizeY
-1);
119 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRank
)
122 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
124 moves
.push(this.getBasicMove(x
, y
, x
+shift
, y
));
125 if (x
==startRank
&& this.board
[x
+2*shift
][y
] == V
.EMPTY
)
128 moves
.push(this.getBasicMove(x
, y
, x
+2*shift
, y
));
133 if (x
+shift
== lastRank
)
136 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
137 promotionPieces
.forEach(p
=> {
139 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
140 moves
.push(this.getBasicMove(x
, y
, x
+shift
, y
, p
));
144 // No en passant here
146 // Add "zen" captures
147 Array
.prototype.push
.apply(moves
, this.findCaptures(x
, y
, color
));
152 getPotentialRookMoves(x
, y
, color
)
154 let noCaptures
= this.getSlideNJumpMoves(
155 x
, y
, color
, VariantRules
.steps
[VariantRules
.ROOK
]);
156 let captures
= this.findCaptures(x
, y
, color
);
157 return noCaptures
.concat(captures
);
160 getPotentialKnightMoves(x
, y
, color
)
162 let noCaptures
= this.getSlideNJumpMoves(
163 x
, y
, color
, VariantRules
.steps
[VariantRules
.KNIGHT
], "oneStep");
164 let captures
= this.findCaptures(x
, y
, color
);
165 return noCaptures
.concat(captures
);
168 getPotentialBishopMoves(x
, y
, color
)
170 let noCaptures
= this.getSlideNJumpMoves(
171 x
, y
, color
, VariantRules
.steps
[VariantRules
.BISHOP
]);
172 let captures
= this.findCaptures(x
, y
, color
);
173 return noCaptures
.concat(captures
);
176 getPotentialQueenMoves(x
, y
, color
)
178 let noCaptures
= this.getSlideNJumpMoves(
179 x
, y
, color
, VariantRules
.steps
[VariantRules
.QUEEN
]);
180 let captures
= this.findCaptures(x
, y
, color
);
181 return noCaptures
.concat(captures
);
184 getPotentialKingMoves(x
, y
, c
)
186 // Initialize with normal moves
187 let noCaptures
= this.getSlideNJumpMoves(
188 x
, y
, c
, VariantRules
.steps
[VariantRules
.QUEEN
], "oneStep");
189 let captures
= this.findCaptures(x
, y
, c
);
191 let moves
= noCaptures
193 .concat(this.getCastleMoves(x
, y
, c
));
200 // Recognize special moves first
201 if (move.appear
.length
== 2)
204 if (move.end
.y
< move.start
.y
)
210 // Translate initial square (because pieces may fly unusually in this variant!)
212 String
.fromCharCode(97 + move.start
.y
) + (VariantRules
.size
[0]-move.start
.x
);
214 // Translate final square
216 String
.fromCharCode(97 + move.end
.y
) + (VariantRules
.size
[0]-move.end
.x
);
219 let piece
= this.getPiece(move.start
.x
, move.start
.y
);
220 if (piece
== VariantRules
.PAWN
)
222 // pawn move (TODO: enPassant indication)
223 if (move.vanish
.length
> 1)
226 notation
= initialSquare
+ "x" + finalSquare
;
229 notation
= finalSquare
;
230 if (piece
!= move.appear
[0].p
) //promotion
231 notation
+= "=" + move.appear
[0].p
.toUpperCase();
237 notation
= piece
.toUpperCase();
238 if (move.vanish
.length
> 1)
239 notation
+= initialSquare
+ "x";
240 notation
+= finalSquare
;
245 static get VALUES() { //TODO: experimental