let moves = this.getPotentialMovesOf(piece, [x, y]);
if (piece == "p" && this.hasEnpassant && this.epSquare)
Array.prototype.push.apply(moves, this.getEnpassantCaptures([x, y]));
- if (
- this.isKing(0, 0, piece) && this.hasCastle &&
- this.castleFlags[color || this.turn].some(v => v < this.size.y)
- ) {
+ if (this.isKing(0, 0, piece) && this.hasCastle)
Array.prototype.push.apply(moves, this.getCastleMoves([x, y]));
- }
return this.postProcessPotentialMoves(moves);
}
return [];
}
- getCastleMoves([x, y], finalSquares, castleWith) {
+ getCastleMoves([x, y], finalSquares, castleWith, castleFlags) {
const c = this.getColor(x, y);
+ castleFlags = castleFlags || this.castleFlags[c];
// Castling ?
const oppCols = this.getOppCols(c);
castleSide < 2;
castleSide++ //large, then small
) {
- if (this.castleFlags[c][castleSide] >= this.size.y)
+ if (castleFlags[castleSide] >= this.size.y)
continue;
// If this code is reached, rook and king are on initial position
// NOTE: in some variants this is not a rook
- const rookPos = this.castleFlags[c][castleSide];
+ const rookPos = castleFlags[castleSide];
const castlingPiece = this.getPiece(x, rookPos);
if (
this.board[x][rookPos] == "" ||
this.board[psq.x][psq.y] = psq.c + psq.p;
}
- updateCastleFlags(move) {
+ // NOTE: arg "castleFlags" for Coregal or Twokings
+ updateCastleFlags(move, castleFlags) {
+ castleFlags = castleFlags || this.castleFlags;
+ // If flags already off, no need to re-check:
+ if (
+ Object.values(castleFlags).every(cvals =>
+ cvals.every(val => val >= this.size.y))
+ ) {
+ return;
+ }
// Update castling flags if start or arrive from/at rook/king locations
move.appear.concat(move.vanish).forEach(psq => {
if (this.isKing(0, 0, psq.p))
- this.castleFlags[psq.c] = [this.size.y, this.size.y];
+ castleFlags[psq.c] = [this.size.y, this.size.y];
// NOTE: not "else if" because king can capture enemy rook...
let c = "";
if (psq.x == 0)
else if (psq.x == this.size.x - 1)
c = "w";
if (c != "") {
- const fidx = this.castleFlags[c].findIndex(f => f == psq.y);
+ const fidx = castleFlags[c].findIndex(f => f == psq.y);
if (fidx >= 0)
- this.castleFlags[c][fidx] = this.size.y;
+ castleFlags[c][fidx] = this.size.y;
}
});
}
prePlay(move) {
- if (
- this.hasCastle &&
- // If flags already off, no need to re-check:
- Object.values(this.castleFlags).some(cvals =>
- cvals.some(val => val < this.size.y))
- ) {
+ if (this.hasCastle)
this.updateCastleFlags(move);
- }
if (this.options["crazyhouse"]) {
move.vanish.forEach(v => {
const square = C.CoordsToSquare({x: v.x, y: v.y});
export class CoregalRules extends ChessRules {
-//TODO: CSS royal queen symbol
-
genRandInitBaseFen() {
const s = FenUtil.setupPieces(
['r', 'n', 'b', 'l', 'k', 'b', 'n', 'r'],
randomness: this.options["randomness"],
between: [{p1: 'k', p2: 'r'}, {p1: 'l', p2: 'r'}],
diffCol: ['b'],
- flags: ['r', 'k', 'l'] //TODO: add 'k' to all 'flags' calls ??!
+ // 'k' and 'l' useful only to get relative position
+ flags: ['r', 'k', 'l']
}
);
return {
fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
s.w.join("").toUpperCase(),
- o: {flags: s.flags}
+ // TODO: re-arrange flags, use another init variable "relPos" (in o)
+ // (maybe after FEN parsing, easier?)
+ o: {flags: s.flags + s.flags} //second set for royal queen
};
}
pieces() {
let res = super.pieces();
res['l'] = JSON.parse(JSON.stringify(res['q']));
+ // TODO: CSS royal queen symbol (with cross?)
res['l']["class"] = "royal_queen";
return res;
}
+ // TODO: something like that indeed (+ flags to FEN)
setFlags(fenflags) {
- // white pieces positions, then black pieces positions
- this.castleFlags = { w: [...Array(4)], b: [...Array(4)] };
+ this.castleFlags = {
+ 'k': { 'w': [...Array(4)], b: [...Array(4)] },
+ 'l': { 'w': [...Array(4)], b: [...Array(4)] }
+ };
for (let i = 0; i < 8; i++) {
- this.castleFlags[i < 4 ? "w" : "b"][i % 4] =
+ this.castleFlags[i < 4 ? "k" : "l"][i % 4 < 2 ? "w" : "b"] =
parseInt(fenflags.charAt(i), 10);
}
}
}
getCastleMoves([x, y]) {
+ const c = this.getColor(x, y),
+ p = this.getPiece(x, y);
// Relative position of the selected piece: left or right ?
// If left: small castle left, large castle right.
// If right: usual situation.
- const c = this.getColor(x, y);
- const relPos = (this.castleFlags[c][1] == y ? "left" : "right");
-
const finalSquares = [
- relPos == "left" ? [1, 2] : [2, 3],
- relPos == "right" ? [6, 5] : [5, 4]
+ this.relPos[c][p] == "left" ? [1, 2] : [2, 3],
+ this.relPos[c][p] == "right" ? [6, 5] : [5, 4]
];
- const saveFlags = JSON.stringify(this.castleFlags[c]);
- // Alter flags to follow base_rules semantic
- this.castleFlags[c] = [0, 3].map(i => this.castleFlags[c][i]);
- const moves = super.getCastleMoves([x, y], finalSquares);
- this.castleFlags[c] = JSON.parse(saveFlags);
+ const moves =
+ super.getCastleMoves([x, y], finalSquares, null, this.castleFlags[p]);
return moves;
}
+ // TODO: updateFlags (just pass castleFlags arg)
+
};