From: Benjamin Auder Date: Fri, 28 Jul 2023 09:38:31 +0000 (+0200) Subject: Start thinking about Coregal X-Git-Url: https://git.auder.net/doc/html/%7B%7B%20targetUrl%20%7D%7D?a=commitdiff_plain;h=52718c94d69b40e79f9ba4fd4ff70376bbf8a774;p=xogo.git Start thinking about Coregal --- diff --git a/base_rules.js b/base_rules.js index c0d9b60..0b71cf4 100644 --- a/base_rules.js +++ b/base_rules.js @@ -233,7 +233,7 @@ export default class ChessRules { ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], { randomness: this.options["randomness"], - between: {p1: 'k', p2: 'r'}, + between: [{p1: 'k', p2: 'r'}], diffCol: ['b'], flags: ['r'] } diff --git a/utils/setupPieces.js b/utils/setupPieces.js index 2c8ccee..94bbc73 100644 --- a/utils/setupPieces.js +++ b/utils/setupPieces.js @@ -38,17 +38,19 @@ export const FenUtil = { }); } if (o.between) { - // Locate p1. If appearing first, exchange with first p2. - // If appearing last, exchange with last p2. - const p1 = res.indexOf(o.between["p1"]); - const firstP2 = res.indexOf(o.between["p2"]), - lastP2 = res.lastIndexOf(o.between["p2"]); - if (p1 < firstP2 || p1 > lastP2) { - res[p1] = o.between["p2"]; - if (p1 < firstP2) - res[firstP2] = o.between["p1"]; - else //p1 > lastP2 - res[lastP2] = o.between["p1"]; + o.between.forEach(b => { + // Locate p1. If appearing first, exchange with first p2. + // If appearing last, exchange with last p2. + const p1 = res.indexOf(b["p1"]); + const firstP2 = res.indexOf(b["p2"]), + lastP2 = res.lastIndexOf(b["p2"]); + if (p1 < firstP2 || p1 > lastP2) { + res[p1] = b["p2"]; + if (p1 < firstP2) + res[firstP2] = b["p1"]; + else //p1 > lastP2 + res[lastP2] = b["p1"]; + } } } } diff --git a/variants/Capablanca/class.js b/variants/Capablanca/class.js index c141e1c..5b1c80e 100644 --- a/variants/Capablanca/class.js +++ b/variants/Capablanca/class.js @@ -60,7 +60,7 @@ export default class CapablancaRules extends ChessRules { ['r', 'n', 's', 'b', 'q', 'k', 'b', 'e', 'n', 'r'], { randomness: this.options["randomness"], - between: {p1: 'k', p2: 'r'}, + between: [{p1: 'k', p2: 'r'}], diffCol: ['b'], flags: ['r'] } diff --git a/variants/Coregal/Coregal.js b/variants/Coregal/Coregal.js new file mode 100644 index 0000000..e12a748 --- /dev/null +++ b/variants/Coregal/Coregal.js @@ -0,0 +1,121 @@ +import { ChessRules, Move, PiPo } from "@/base_rules"; +import { ArrayFun } from "@/utils/array"; +import { randInt, sample } from "@/utils/alea"; + +export class CoregalRules extends ChessRules { + +// TODO: special symbol (l) for royal queen... + + getPPpath(m) { + if ( + m.vanish.length == 2 && + m.appear.length == 2 && + m.vanish[0].p == V.QUEEN + ) { + // Large castle: show castle symbol + return "Coregal/castle"; + } + return super.getPPpath(m); + } + + 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'] + } + ); + return { + fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + + s.w.join("").toUpperCase(), + o: {flags: s.flags} + }; + } + + pieces() { + let res = super.pieces(); + res['l'] = {...}; + return res; + } + + setFlags(fenflags) { + // white pieces positions, then black pieces positions + this.castleFlags = { w: [...Array(4)], b: [...Array(4)] }; + for (let i = 0; i < 8; i++) { + this.castleFlags[i < 4 ? "w" : "b"][i % 4] = + parseInt(fenflags.charAt(i), 10); + } + } + + getPotentialQueenMoves([x, y]) { + let moves = super.getPotentialQueenMoves([x, y]); + const c = this.getColor(x, y); + if (this.castleFlags[c].slice(1, 3).includes(y)) + moves = moves.concat(this.getCastleMoves([x, y])); + return moves; + } + + getPotentialKingMoves([x, y]) { + let moves = this.getSlideNJumpMoves( + [x, y], V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1); + const c = this.getColor(x, y); + if (this.castleFlags[c].slice(1, 3).includes(y)) + moves = moves.concat(this.getCastleMoves([x, y])); + return moves; + } + + getCastleMoves([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] + ]; + 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); + return moves; + } + + // "twoKings" arg for the similar Twokings variant. + updateCastleFlags(move, piece, twoKings) { + const c = V.GetOppCol(this.turn); + const firstRank = (c == "w" ? V.size.x - 1 : 0); + // Update castling flags if castling pieces moved or were captured + const oppCol = V.GetOppCol(c); + const oppFirstRank = V.size.x - 1 - firstRank; + if (move.start.x == firstRank) { + if (piece == V.KING || (!twoKings && piece == V.QUEEN)) { + if (this.castleFlags[c][1] == move.start.y) + this.castleFlags[c][1] = 8; + else if (this.castleFlags[c][2] == move.start.y) + this.castleFlags[c][2] = 8; + // Else: the flag is already turned off + } + } + else if ( + move.start.x == firstRank && //our rook moves? + [this.castleFlags[c][0], this.castleFlags[c][3]].includes(move.start.y) + ) { + const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 3); + this.castleFlags[c][flagIdx] = 8; + } else if ( + move.end.x == oppFirstRank && //we took opponent rook? + [this.castleFlags[oppCol][0], this.castleFlags[oppCol][3]] + .includes(move.end.y) + ) { + const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 3); + this.castleFlags[oppCol][flagIdx] = 8; + } + } + +}; diff --git a/variants/Giveaway/class.js b/variants/Giveaway/class.js index 9665835..45f6a34 100644 --- a/variants/Giveaway/class.js +++ b/variants/Giveaway/class.js @@ -43,7 +43,7 @@ export default class GiveawayRules extends ChessRules { diffCol: ['b'] }; if (this.options["mode"] == "losers") { - setupOpts["between"] = ['k', 'r']; + setupOpts["between"] = [{p1: 'k', p2: 'r'}]; setupOpts["flags"] = ['r']; } const s = FenUtil.setupPieces(