From dcfaab5dc62ccd9701ff314ad4e3d40c40223d44 Mon Sep 17 00:00:00 2001 From: Benjamin Auder <benjamin.auder@somewhere> Date: Mon, 4 Dec 2023 18:14:37 +0100 Subject: [PATCH] Some thoughts about Coregal --- base_rules.js | 38 +++++++++++++++++++------------------- variants/Copycat/class.js | 2 ++ variants/Coregal/class.js | 37 ++++++++++++++++++++----------------- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/base_rules.js b/base_rules.js index 6078da8..8c7d549 100644 --- a/base_rules.js +++ b/base_rules.js @@ -1520,12 +1520,8 @@ export default class ChessRules { 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); } @@ -2015,8 +2011,9 @@ export default class ChessRules { 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); @@ -2030,12 +2027,12 @@ export default class ChessRules { 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] == "" || @@ -2238,11 +2235,20 @@ export default class ChessRules { 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) @@ -2250,22 +2256,16 @@ export default class ChessRules { 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}); diff --git a/variants/Copycat/class.js b/variants/Copycat/class.js index e19367c..f996be5 100644 --- a/variants/Copycat/class.js +++ b/variants/Copycat/class.js @@ -1,5 +1,7 @@ import ChessRules from "/base_rules.js"; +// TODO: there was an issue: I forgot which.. TOFIND and TOFIX :) + export default class CopycatRules extends ChessRules { static get Options() { diff --git a/variants/Coregal/class.js b/variants/Coregal/class.js index b563fec..4df32ad 100644 --- a/variants/Coregal/class.js +++ b/variants/Coregal/class.js @@ -4,8 +4,6 @@ import { randInt, sample } from "@/utils/alea"; export class CoregalRules extends ChessRules { -//TODO: CSS royal queen symbol - genRandInitBaseFen() { const s = FenUtil.setupPieces( ['r', 'n', 'b', 'l', 'k', 'b', 'n', 'r'], @@ -13,28 +11,35 @@ export class CoregalRules extends ChessRules { 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); } } @@ -46,22 +51,20 @@ export class CoregalRules extends ChessRules { } 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) + }; -- 2.44.0