da4dd7af60454924f155a175ed5134c194220c37
1 class ZenRules
extends ChessRules
3 // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare
4 static get HasEnpassant
{ return false; }
6 // TODO(?): some duplicated code in 2 next functions
7 getSlideNJumpMoves([x
,y
], steps
, oneStep
)
9 const color
= this.getColor(x
,y
);
12 for (let loop
=0; loop
<steps
.length
; loop
++)
14 const step
= steps
[loop
];
17 while (V
.OnBoard(i
,j
) && this.board
[i
][j
] == V
.EMPTY
)
19 moves
.push(this.getBasicMove([x
,y
], [i
,j
]));
25 // No capture check: handled elsewhere (next method)
30 // follow steps from x,y until something is met.
31 // if met piece is opponent and same movement (asA): eat it!
32 findCaptures_aux([x
,y
], asA
)
34 const color
= this.getColor(x
,y
);
36 const steps
= asA
!= V
.PAWN
37 ? (asA
==V
.QUEEN
? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]) : V
.steps
[asA
])
38 : color
=='w' ? [[-1,-1],[-1,1]] : [[1,-1],[1,1]];
39 const oneStep
= (asA
==V
.KNIGHT
|| asA
==V
.PAWN
); //we don't capture king
40 const lastRank
= (color
== 'w' ? 0 : V
.size
.x
-1);
41 const promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
43 for (let loop
=0; loop
<steps
.length
; loop
++)
45 const step
= steps
[loop
];
48 while (V
.OnBoard(i
,j
) && this.board
[i
][j
] == V
.EMPTY
)
55 if (V
.OnBoard(i
,j
) && this.getColor(i
,j
) == this.getOppCol(color
)
56 && 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
], {c:color
,p:p
}));
69 moves
.push(this.getBasicMove([x
,y
], [i
,j
]));
76 // Find possible captures from a square: look in every direction!
81 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.PAWN
));
82 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.ROOK
));
83 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.KNIGHT
));
84 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.BISHOP
));
85 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.QUEEN
));
90 getPotentialPawnMoves([x
,y
])
92 const color
= this.getColor(x
,y
);
94 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
95 const shift
= (color
== 'w' ? -1 : 1);
96 const startRank
= (color
== 'w' ? sizeY
-2 : 1);
97 const firstRank
= (color
== 'w' ? sizeY
-1 : 0);
98 const lastRank
= (color
== "w" ? 0 : sizeY
-1);
100 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRank
)
103 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
105 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
106 if ([startRank
,firstRank
].includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
109 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
114 if (x
+shift
== lastRank
)
117 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
118 promotionPieces
.forEach(p
=> {
120 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
121 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
125 // No en passant here
127 // Add "zen" captures
128 Array
.prototype.push
.apply(moves
, this.findCaptures([x
,y
]));
133 getPotentialRookMoves(sq
)
135 let noCaptures
= this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]);
136 let captures
= this.findCaptures(sq
);
137 return noCaptures
.concat(captures
);
140 getPotentialKnightMoves(sq
)
142 let noCaptures
= this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep");
143 let captures
= this.findCaptures(sq
);
144 return noCaptures
.concat(captures
);
147 getPotentialBishopMoves(sq
)
149 let noCaptures
= this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]);
150 let captures
= this.findCaptures(sq
);
151 return noCaptures
.concat(captures
);
154 getPotentialQueenMoves(sq
)
156 let noCaptures
= this.getSlideNJumpMoves(
157 sq
, V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]));
158 let captures
= this.findCaptures(sq
);
159 return noCaptures
.concat(captures
);
162 getPotentialKingMoves(sq
)
164 // Initialize with normal moves
165 let noCaptures
= this.getSlideNJumpMoves(sq
,
166 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), "oneStep");
167 let captures
= this.findCaptures(sq
);
168 return noCaptures
.concat(captures
).concat(this.getCastleMoves(sq
));
173 // Recognize special moves first
174 if (move.appear
.length
== 2)
177 if (move.end
.y
< move.start
.y
)
183 // Translate initial square (because pieces may fly unusually in this variant!)
184 const initialSquare
= V
.CoordsToSquare(move.start
);
186 // Translate final square
187 const finalSquare
= V
.CoordsToSquare(move.end
);
190 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
193 // pawn move (TODO: enPassant indication)
194 if (move.vanish
.length
> 1)
197 notation
= initialSquare
+ "x" + finalSquare
;
200 notation
= finalSquare
;
201 if (piece
!= move.appear
[0].p
) //promotion
202 notation
+= "=" + move.appear
[0].p
.toUpperCase();
208 notation
= piece
.toUpperCase();
209 if (move.vanish
.length
> 1)
210 notation
+= initialSquare
+ "x";
211 notation
+= finalSquare
;
218 // TODO: experimental