X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=variants%2FCoregal%2Fclass.js;h=23f67c757cef96331487ffbf4ed7fdd209f51bb5;hb=2c8375bb77dda7cbeaee983a09e202436be2191c;hp=4df32ad178cd1ece9bfa4f6e86e954b09469a779;hpb=dcfaab5dc62ccd9701ff314ad4e3d40c40223d44;p=xogo.git diff --git a/variants/Coregal/class.js b/variants/Coregal/class.js index 4df32ad..23f67c7 100644 --- a/variants/Coregal/class.js +++ b/variants/Coregal/class.js @@ -1,8 +1,7 @@ -import { ChessRules, Move, PiPo } from "@/base_rules"; -import { ArrayFun } from "@/utils/array"; -import { randInt, sample } from "@/utils/alea"; +import ChessRules from "/base_rules.js"; +import {FenUtil} from "/utils/setupPieces.js" -export class CoregalRules extends ChessRules { +export default class CoregalRules extends ChessRules { genRandInitBaseFen() { const s = FenUtil.setupPieces( @@ -15,39 +14,101 @@ export class CoregalRules extends ChessRules { flags: ['r', 'k', 'l'] } ); + // Re-arrange flags: king + royal queen positions are only + // useful to know ordering, and thus allowed castles. + let flags = ""; + let relPos = { 'w': {}, 'b': {} }; + for (let c of [0, 1]) { + const col = (c == 0 ? 'w' : 'b'); + let first = ""; + for (let i=4*c; i<4*(c+1); i++) { + const pos = parseInt(s.flags.charAt(i), 10); + const symb = s[col][pos]; + if (['k', 'l'].includes(symb)) { + if (!first) { + relPos[col][symb] = '0'; //left + first = symb; + } + else + relPos[col][symb] = '1'; //right + } + else + flags += s.flags.charAt(i); + } + } return { fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + s.w.join("").toUpperCase(), - // 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 + o: { + flags: flags + flags, //duplicate: one for each royal piece + relPos: this.getRelposFen(relPos) + } }; } - pieces() { - let res = super.pieces(); + getPartFen(o) { + return (Object.assign( + {"relpos": o.init ? o.relPos : this.getRelposFen()}, + super.getPartFen(o) + )); + } + + getRelposFen(relPos) { + relPos = relPos || this.relPos; + return ( + relPos['w']['k'] + relPos['w']['l'] + + relPos['b']['k'] + relPos['b']['l'] + ); + } + + setOtherVariables(fenParsed, pieceArray) { + super.setOtherVariables(fenParsed, pieceArray); + this.relPos = { + 'w': { + 'k': fenParsed.relpos[0], + 'l': fenParsed.relpos[1] + }, + 'b': { + 'k': fenParsed.relpos[2], + 'l': fenParsed.relpos[3] + } + }; + } + + pieces(color, x, y) { + let res = super.pieces(color, x, y); res['l'] = JSON.parse(JSON.stringify(res['q'])); // TODO: CSS royal queen symbol (with cross?) res['l']["class"] = "royal_queen"; + res['='] = {"class": "castle"}; //for castle display return res; } - // TODO: something like that indeed (+ flags to FEN) setFlags(fenflags) { this.castleFlags = { - 'k': { 'w': [...Array(4)], b: [...Array(4)] }, - 'l': { 'w': [...Array(4)], b: [...Array(4)] } + k: { + w: [0, 1].map(i => parseInt(fenflags.charAt(i), 10)), + b: [2, 3].map(i => parseInt(fenflags.charAt(i), 10)) + }, + l: { + w: [4, 5].map(i => parseInt(fenflags.charAt(i), 10)), + b: [6, 7].map(i => parseInt(fenflags.charAt(i), 10)) + } }; - for (let i = 0; i < 8; i++) { - this.castleFlags[i < 4 ? "k" : "l"][i % 4 < 2 ? "w" : "b"] = - parseInt(fenflags.charAt(i), 10); - } + } + + getFlagsFen() { + return ['k', 'l'].map(p => { + return ['w', 'b'].map(c => { + return this.castleFlags[p][c].map(x => x.toString(10)).join(""); + }).join("") + }).join(""); } isKing(x, y, p) { if (!p) p = this.getPiece(x, y); - ['k', 'l'].includes(p); //no cannibal mode + return ['k', 'l'].includes(p); //no cannibal mode } getCastleMoves([x, y]) { @@ -57,14 +118,19 @@ export class CoregalRules extends ChessRules { // If left: small castle left, large castle right. // If right: usual situation. const finalSquares = [ - this.relPos[c][p] == "left" ? [1, 2] : [2, 3], - this.relPos[c][p] == "right" ? [6, 5] : [5, 4] + this.relPos[c][p] == '0' ? [1, 2] : [2, 3], //0 == left + this.relPos[c][p] == '1' ? [6, 5] : [5, 4] //1 == right ]; - const moves = - super.getCastleMoves([x, y], finalSquares, null, this.castleFlags[p]); + let moves = + super.getCastleMoves([x, y], finalSquares, null, this.castleFlags[p][c]); + if (p == 'l') + moves.forEach(m => m.choice = '='); //required (for display) return moves; } - // TODO: updateFlags (just pass castleFlags arg) + updateCastleFlags(move) { + super.updateCastleFlags(move, this.castleFlags['k'], 'k'); + super.updateCastleFlags(move, this.castleFlags['l'], 'l'); + } };