class ZenRules extends ChessRules
{
- // TODO: some duplicated code in 2 next functions
- getSlideNJumpMoves(x, y, color, steps, oneStep)
+ // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare
+ static get HasEnpassant() { return false; }
+
+ // TODO(?): some duplicated code in 2 next functions
+ getSlideNJumpMoves([x,y], steps, oneStep)
{
- var moves = [];
- let [sizeX,sizeY] = VariantRules.size;
+ const color = this.getColor(x,y);
+ let moves = [];
outerLoop:
- for (var loop=0; loop<steps.length; loop++)
+ for (let loop=0; loop<steps.length; loop++)
{
- var step = steps[loop];
- var i = x + step[0];
- var j = y + step[1];
- while (i>=0 && i<sizeX && j>=0 && j<sizeY
- && this.board[i][j] == VariantRules.EMPTY)
+ const step = steps[loop];
+ let i = x + step[0];
+ let j = y + step[1];
+ while (V.OnBoard(i,j) && this.board[i][j] == V.EMPTY)
{
- moves.push(this.getBasicMove(x, y, i, j));
- if (oneStep !== undefined)
+ moves.push(this.getBasicMove([x,y], [i,j]));
+ if (!!oneStep)
continue outerLoop;
i += step[0];
j += step[1];
// follow steps from x,y until something is met.
// if met piece is opponent and same movement (asA): eat it!
- findCaptures_aux(x, y, color, asA)
+ findCaptures_aux([x,y], asA)
{
+ const color = this.getColor(x,y);
var moves = [];
- var V = VariantRules;
- var steps = asA != V.PAWN
- ? V.steps[asA]
+ const steps = asA != V.PAWN
+ ? (asA==V.QUEEN ? V.steps[V.ROOK].concat(V.steps[V.BISHOP]) : V.steps[asA])
: color=='w' ? [[-1,-1],[-1,1]] : [[1,-1],[1,1]];
- var oneStep = (asA==V.KNIGHT || asA==V.PAWN); //we don't capture king
- let [sizeX,sizeY] = V.size;
- let lastRank = (color == 'w' ? 0 : sizeY-1);
- let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
+ const oneStep = (asA==V.KNIGHT || asA==V.PAWN); //we don't capture king
+ const lastRank = (color == 'w' ? 0 : V.size.x-1);
+ const promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
outerLoop:
- for (var loop=0; loop<steps.length; loop++)
+ for (let loop=0; loop<steps.length; loop++)
{
- var step = steps[loop];
- var i = x + step[0];
- var j = y + step[1];
- while (i>=0 && i<sizeX && j>=0 && j<sizeY
- && this.board[i][j] == V.EMPTY)
+ const step = steps[loop];
+ let i = x + step[0];
+ let j = y + step[1];
+ while (V.OnBoard(i,j) && this.board[i][j] == V.EMPTY)
{
if (oneStep)
continue outerLoop;
i += step[0];
j += step[1];
}
- if (i>=0 && i<sizeX && j>=0 && j<sizeY &&
- this.getColor(i,j) == this.getOppCol(color) && this.getPiece(i,j) == asA)
+ if (V.OnBoard(i,j) && this.getColor(i,j) == V.GetOppCol(color)
+ && this.getPiece(i,j) == asA)
{
// eat!
if (this.getPiece(x,y) == V.PAWN && i == lastRank)
{
// Special case of promotion:
promotionPieces.forEach(p => {
- moves.push(this.getBasicMove(x, y, i, j, p));
+ moves.push(this.getBasicMove([x,y], [i,j], {c:color,p:p}));
});
}
else
{
// All other cases
- moves.push(this.getBasicMove(x, y, i, j));
+ moves.push(this.getBasicMove([x,y], [i,j]));
}
}
}
}
// Find possible captures from a square: look in every direction!
- findCaptures(x, y, color)
+ findCaptures(sq)
{
- var moves = [];
-
- // PAWN
- Array.prototype.push.apply(moves,
- this.findCaptures_aux(x, y, color, VariantRules.PAWN));
-
- // ROOK
- Array.prototype.push.apply(moves,
- this.findCaptures_aux(x, y, color, VariantRules.ROOK));
-
- // KNIGHT
- Array.prototype.push.apply(moves,
- this.findCaptures_aux(x, y, color, VariantRules.KNIGHT));
+ let moves = [];
- // BISHOP
- Array.prototype.push.apply(moves,
- this.findCaptures_aux(x, y, color, VariantRules.BISHOP));
-
- // QUEEN
- Array.prototype.push.apply(moves,
- this.findCaptures_aux(x, y, color, VariantRules.QUEEN));
+ Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.PAWN));
+ Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.ROOK));
+ Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.KNIGHT));
+ Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.BISHOP));
+ Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.QUEEN));
return moves;
}
- getPotentialPawnMoves(x, y, color)
+ getPotentialPawnMoves([x,y])
{
- var moves = [];
- var V = VariantRules;
- let [sizeX,sizeY] = VariantRules.size;
- let shift = (color == 'w' ? -1 : 1);
- let startRank = (color == 'w' ? sizeY-2 : 1);
- let lastRank = (color == "w" ? 0 : sizeY-1);
-
- if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank)
+ const color = this.getColor(x,y);
+ let moves = [];
+ const [sizeX,sizeY] = [V.size.x,V.size.y];
+ const shift = (color == 'w' ? -1 : 1);
+ const startRank = (color == 'w' ? sizeY-2 : 1);
+ const firstRank = (color == 'w' ? sizeY-1 : 0);
+ const lastRank = (color == "w" ? 0 : sizeY-1);
+
+ if (x+shift != lastRank)
{
// Normal moves
if (this.board[x+shift][y] == V.EMPTY)
{
- moves.push(this.getBasicMove(x, y, x+shift, y));
- if (x==startRank && this.board[x+2*shift][y] == V.EMPTY)
+ moves.push(this.getBasicMove([x,y], [x+shift,y]));
+ if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY)
{
//two squares jump
- moves.push(this.getBasicMove(x, y, x+2*shift, y));
+ moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
}
}
}
- if (x+shift == lastRank)
+ else //promotion
{
- // Promotion
let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
promotionPieces.forEach(p => {
// Normal move
if (this.board[x+shift][y] == V.EMPTY)
- moves.push(this.getBasicMove(x, y, x+shift, y, p));
+ moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
});
}
- // En passant
- const Lep = this.epSquares.length;
- const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined;
- if (!!epSquare && epSquare.x == x+shift && Math.abs(epSquare.y - y) == 1)
- {
- let epStep = epSquare.y - y;
- var enpassantMove = this.getBasicMove(x, y, x+shift, y+epStep);
- enpassantMove.vanish.push({
- x: x,
- y: y+epStep,
- p: 'p',
- c: this.getColor(x,y+epStep)
- });
- moves.push(enpassantMove);
- }
+ // No en passant here
// Add "zen" captures
- Array.prototype.push.apply(moves, this.findCaptures(x, y, color));
+ Array.prototype.push.apply(moves, this.findCaptures([x,y]));
return moves;
}
- getPotentialRookMoves(x, y, color)
+ getPotentialRookMoves(sq)
{
- let noCaptures = this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.ROOK]);
- let captures = this.findCaptures(x, y, color);
+ let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.ROOK]);
+ let captures = this.findCaptures(sq);
return noCaptures.concat(captures);
}
- getPotentialKnightMoves(x, y, color)
+ getPotentialKnightMoves(sq)
{
- let noCaptures = this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
- let captures = this.findCaptures(x, y, color);
+ let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep");
+ let captures = this.findCaptures(sq);
return noCaptures.concat(captures);
}
- getPotentialBishopMoves(x, y, color)
+ getPotentialBishopMoves(sq)
{
- let noCaptures = this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.BISHOP]);
- let captures = this.findCaptures(x, y, color);
+ let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]);
+ let captures = this.findCaptures(sq);
return noCaptures.concat(captures);
}
- getPotentialQueenMoves(x, y, color)
+ getPotentialQueenMoves(sq)
{
let noCaptures = this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.QUEEN]);
- let captures = this.findCaptures(x, y, color);
+ sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]));
+ let captures = this.findCaptures(sq);
return noCaptures.concat(captures);
}
- getPotentialKingMoves(x, y, c)
+ getPotentialKingMoves(sq)
{
// Initialize with normal moves
- let noCaptures = this.getSlideNJumpMoves(
- x, y, c, VariantRules.steps[VariantRules.QUEEN], "oneStep");
- let captures = this.findCaptures(x, y, c);
-
- let moves = noCaptures
- .concat(captures)
- .concat(this.getCastleMoves(x, y, c));
-
- return moves;
+ let noCaptures = this.getSlideNJumpMoves(sq,
+ V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
+ let captures = this.findCaptures(sq);
+ return noCaptures.concat(captures).concat(this.getCastleMoves(sq));
}
getNotation(move)
}
// Translate initial square (because pieces may fly unusually in this variant!)
- let initialSquare =
- String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x);
+ const initialSquare = V.CoordsToSquare(move.start);
// Translate final square
- let finalSquare =
- String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
+ const finalSquare = V.CoordsToSquare(move.end);
let notation = "";
- let piece = this.getPiece(move.start.x, move.start.y);
- if (piece == VariantRules.PAWN)
+ const piece = this.getPiece(move.start.x, move.start.y);
+ if (piece == V.PAWN)
{
// pawn move (TODO: enPassant indication)
if (move.vanish.length > 1)
return notation;
}
- static get VALUES() { //TODO: experimental
+ static get VALUES()
+ {
+ // TODO: experimental
return {
'p': 1,
'r': 3,
}
}
}
+
+const VariantRules = ZenRules;