1 class ZenRules
extends ChessRules
3 // TODO: some duplicated code in 2 next functions
4 getSlideNJumpMoves(x
, y
, color
, steps
, oneStep
)
7 let [sizeX
,sizeY
] = VariantRules
.size
;
9 for (var loop
=0; loop
<steps
.length
; loop
++)
11 var step
= steps
[loop
];
14 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
15 && this.board
[i
][j
] == VariantRules
.EMPTY
)
17 moves
.push(this.getBasicMove(x
, y
, i
, j
));
18 if (oneStep
!== undefined)
23 // No capture check: handled elsewhere (next method)
28 // follow steps from x,y until something is met.
29 // if met piece is opponent and same movement (asA): eat it!
30 findCaptures_aux(x
, y
, color
, asA
)
34 var steps
= asA
!= V
.PAWN
36 : color
=='w' ? [[-1,-1],[-1,1]] : [[1,-1],[1,1]];
37 var oneStep
= (asA
==V
.KNIGHT
|| asA
==V
.PAWN
); //we don't capture king
38 let [sizeX
,sizeY
] = V
.size
;
39 let lastRank
= (color
== 'w' ? 0 : sizeY
-1);
40 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
42 for (var loop
=0; loop
<steps
.length
; loop
++)
44 var step
= steps
[loop
];
47 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
48 && this.board
[i
][j
] == V
.EMPTY
)
55 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&&
56 this.getColor(i
,j
) == this.getOppCol(color
) && this.getPiece(i
,j
) == asA
)
59 if (this.getPiece(x
,y
) == V
.PAWN
&& i
== lastRank
)
61 // Special case of promotion:
62 promotionPieces
.forEach(p
=> {
63 moves
.push(this.getBasicMove(x
, y
, i
, j
, p
));
69 moves
.push(this.getBasicMove(x
, y
, i
, j
));
76 // Find possible captures from a square: look in every direction!
77 findCaptures(x
, y
, color
)
82 Array
.prototype.push
.apply(moves
,
83 this.findCaptures_aux(x
, y
, color
, VariantRules
.PAWN
));
86 Array
.prototype.push
.apply(moves
,
87 this.findCaptures_aux(x
, y
, color
, VariantRules
.ROOK
));
90 Array
.prototype.push
.apply(moves
,
91 this.findCaptures_aux(x
, y
, color
, VariantRules
.KNIGHT
));
94 Array
.prototype.push
.apply(moves
,
95 this.findCaptures_aux(x
, y
, color
, VariantRules
.BISHOP
));
98 Array
.prototype.push
.apply(moves
,
99 this.findCaptures_aux(x
, y
, color
, VariantRules
.QUEEN
));
104 getPotentialPawnMoves(x
, y
, color
)
107 var V
= VariantRules
;
108 let [sizeX
,sizeY
] = VariantRules
.size
;
109 let shift
= (color
== 'w' ? -1 : 1);
110 let startRank
= (color
== 'w' ? sizeY
-2 : 1);
111 let lastRank
= (color
== "w" ? 0 : sizeY
-1);
113 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRank
)
116 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
118 moves
.push(this.getBasicMove(x
, y
, x
+shift
, y
));
119 if (x
==startRank
&& this.board
[x
+2*shift
][y
] == V
.EMPTY
)
122 moves
.push(this.getBasicMove(x
, y
, x
+2*shift
, y
));
127 if (x
+shift
== lastRank
)
130 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
131 promotionPieces
.forEach(p
=> {
133 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
134 moves
.push(this.getBasicMove(x
, y
, x
+shift
, y
, p
));
139 const Lep
= this.epSquares
.length
;
140 const epSquare
= Lep
>0 ? this.epSquares
[Lep
-1] : undefined;
141 if (!!epSquare
&& epSquare
.x
== x
+shift
&& Math
.abs(epSquare
.y
- y
) == 1)
143 let epStep
= epSquare
.y
- y
;
144 var enpassantMove
= this.getBasicMove(x
, y
, x
+shift
, y
+epStep
);
145 enpassantMove
.vanish
.push({
149 c: this.getColor(x
,y
+epStep
)
151 moves
.push(enpassantMove
);
154 // Add "zen" captures
155 Array
.prototype.push
.apply(moves
, this.findCaptures(x
, y
, color
));
160 getPotentialRookMoves(x
, y
, color
)
162 let noCaptures
= this.getSlideNJumpMoves(
163 x
, y
, color
, VariantRules
.steps
[VariantRules
.ROOK
]);
164 let captures
= this.findCaptures(x
, y
, color
);
165 return noCaptures
.concat(captures
);
168 getPotentialKnightMoves(x
, y
, color
)
170 let noCaptures
= this.getSlideNJumpMoves(
171 x
, y
, color
, VariantRules
.steps
[VariantRules
.KNIGHT
], "oneStep");
172 let captures
= this.findCaptures(x
, y
, color
);
173 return noCaptures
.concat(captures
);
176 getPotentialBishopMoves(x
, y
, color
)
178 let noCaptures
= this.getSlideNJumpMoves(
179 x
, y
, color
, VariantRules
.steps
[VariantRules
.BISHOP
]);
180 let captures
= this.findCaptures(x
, y
, color
);
181 return noCaptures
.concat(captures
);
184 getPotentialQueenMoves(x
, y
, color
)
186 let noCaptures
= this.getSlideNJumpMoves(
187 x
, y
, color
, VariantRules
.steps
[VariantRules
.QUEEN
]);
188 let captures
= this.findCaptures(x
, y
, color
);
189 return noCaptures
.concat(captures
);
192 getPotentialKingMoves(x
, y
, c
)
194 // Initialize with normal moves
195 let noCaptures
= this.getSlideNJumpMoves(
196 x
, y
, c
, VariantRules
.steps
[VariantRules
.QUEEN
], "oneStep");
197 let captures
= this.findCaptures(x
, y
, c
);
199 let moves
= noCaptures
201 .concat(this.getCastleMoves(x
, y
, c
));
208 // Recognize special moves first
209 if (move.appear
.length
== 2)
212 if (move.end
.y
< move.start
.y
)
218 // Translate initial square (because pieces may fly unusually in this variant!)
220 String
.fromCharCode(97 + move.start
.y
) + (VariantRules
.size
[0]-move.start
.x
);
222 // Translate final square
224 String
.fromCharCode(97 + move.end
.y
) + (VariantRules
.size
[0]-move.end
.x
);
227 let piece
= this.rules
.getPiece(move.start
.x
, move.start
.y
);
228 if (piece
== VariantRules
.PAWN
)
230 // pawn move (TODO: enPassant indication)
231 if (move.vanish
.length
> 1)
234 notation
= initialSquare
+ "x" + finalSquare
;
237 notation
= finalSquare
;
238 if (piece
!= move.appear
[0].p
) //promotion
239 notation
+= "=" + move.appear
[0].p
.toUpperCase();
245 notation
= piece
.toUpperCase();
246 if (move.vanish
.length
> 1)
247 notation
+= initialSquare
+ "x";
248 notation
+= finalSquare
;
253 static get VALUES() { //TODO: experimental