X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=variants%2FCheckered%2Fclass.js;h=f73ed0ea27e7d120c57b1a6ba51cc23db76ac2d0;hb=7fbcb53de45ba7b7aed99d6087928779713810b1;hp=9c7824307f8aa844343db56f57bbe726d3f7bdfe;hpb=ceeac4e82346ffba2d87d763df3cffcddaec912a;p=xogo.git diff --git a/variants/Checkered/class.js b/variants/Checkered/class.js index 9c78243..f73ed0e 100644 --- a/variants/Checkered/class.js +++ b/variants/Checkered/class.js @@ -12,7 +12,7 @@ export default class CheckeredRules extends ChessRules { label: "Allow switching", variable: "withswitch", type: "checkbox", - defaut: false + defaut: true } ], styles: [ @@ -28,7 +28,13 @@ export default class CheckeredRules extends ChessRules { }; } - static board2fen(b) { + static GetColorClass(c) { + if (c == 'c') + return "checkered"; + return C.GetColorClass(c); + } + + board2fen(b) { const checkered_codes = { p: "s", q: "t", @@ -41,7 +47,7 @@ export default class CheckeredRules extends ChessRules { return super.board2fen(b); } - static fen2board(f) { + fen2board(f) { // Tolerate upper-case versions of checkered pieces (why not?) const checkered_pieces = { s: "p", @@ -60,6 +66,12 @@ export default class CheckeredRules extends ChessRules { return super.fen2board(f); } + genRandInitBaseFen() { + let res = super.genRandInitBaseFen(); + res.o.flags += "1".repeat(16); //pawns flags + return res; + } + getPartFen(o) { return Object.assign( { @@ -107,24 +119,33 @@ export default class CheckeredRules extends ChessRules { pieces(color, x, y) { let baseRes = super.pieces(color, x, y); - if ( - this.getPiece(x, y) == 'p' && - this.stage == 2 && - this.getColor(x, y) == 'c' - ) { + if (this.getPiece(x, y) == 'p' && color == 'c') { + const pawnShift = this.getPawnShift(this.turn); //cannot trust color + const initRank = ( + (this.stage == 2 && [1, 6].includes(x)) || + (this.stage == 1 && + ((x == 1 && this.turn == 'b') || (x == 6 && this.turn == 'w')) + ) + ); // Checkered pawns on stage 2 are bidirectional - const initRank = ((color == 'w' && x >= 6) || (color == 'b' && x <= 1)); + let moveSteps = [[pawnShift, 0]], + attackSteps = [[pawnShift, 1], [pawnShift, -1]]; + if (this.stage == 2) { + moveSteps.push([-pawnShift, 0]); + Array.prototype.push.apply(attackSteps, + [[-pawnShift, 1], [-pawnShift, -1]]); + } baseRes['p'] = { "class": "pawn", moves: [ { - steps: [[1, 0], [-1, 0]], + steps: moveSteps, range: (initRank ? 2 : 1) } ], attack: [ { - steps: [[1, 1], [1, -1], [-1, 1], [-1, -1]], + steps: attackSteps, range: 1 } ] @@ -206,6 +227,7 @@ export default class CheckeredRules extends ChessRules { } postProcessPotentialMoves(moves) { + moves = super.postProcessPotentialMoves(moves); if (this.stage == 2 || moves.length == 0) return moves; const color = this.turn; @@ -243,7 +265,7 @@ export default class CheckeredRules extends ChessRules { return false; //forbidden 2-squares jumps } if ( - this.board[m.end.x][m.end.y] == V.EMPTY && + this.board[m.end.x][m.end.y] == "" && m.vanish.length == 2 && this.getColor(m.start.x, m.start.y) == "c" ) { @@ -304,24 +326,31 @@ export default class CheckeredRules extends ChessRules { filterValid(moves) { const color = this.turn; - if (stage == 2 && this.sideCheckered == color) + if (this.stage == 2 && this.sideCheckered == color) // Checkered cannot be under check (no king) return moves; let kingPos = super.searchKingPos(color); if (this.stage == 2) // Must consider both kings (attacked by checkered side) - kingPos = [kingPos, super.searchKingPos(C.GetOppTurn(this.turn))]; + kingPos.push(super.searchKingPos(C.GetOppTurn(this.turn))[0]); const oppCols = this.getOppCols(color); - return moves.filter(m => { + const filteredMoves = moves.filter(m => { + if (m.vanish.length == 0 && m.appear.length == 0) + return true; //switch move this.playOnBoard(m); + if (m.vanish[0].p == 'k') + kingPos[0] = [m.appear[0].x, m.appear[0].y]; let res = true; - if (stage == 1) + if (this.stage == 1) res = !this.oppositeMoves(this.cmove, m); if (res && m.appear.length > 0) res = !this.underCheck(kingPos, oppCols); + if (m.vanish[0].p == 'k') + kingPos[0] = [m.vanish[0].x, m.vanish[0].y]; this.undoOnBoard(m); return res; }); + return filteredMoves; } atLeastOneMove(color) { @@ -351,9 +380,14 @@ export default class CheckeredRules extends ChessRules { } underCheck(square_s, oppCols) { - if (this.stage == 2 && oppCol != this.sideCheckered) + if (this.stage == 2 && this.turn == this.sideCheckered) return false; //checkered pieces is me, I'm not under check - return super.underAttack(square_s, oppCols); + // Artificial turn change required, because canTake uses turn info. + // canTake is called from underCheck --> ... --> findDestSquares + this.turn = C.GetOppTurn(this.turn); + const res = square_s.some(sq => super.underAttack(sq, oppCols)); + this.turn = C.GetOppTurn(this.turn); + return res; } prePlay(move) { @@ -361,7 +395,7 @@ export default class CheckeredRules extends ChessRules { super.prePlay(move); if ( [1, 6].includes(move.start.x) && - move.vanish[0].p == V.PAWN && + move.vanish[0].p == 'p' && Math.abs(move.end.x - move.start.x) == 2 ) { // This move turns off a 2-squares pawn flag @@ -374,6 +408,8 @@ export default class CheckeredRules extends ChessRules { if (move.appear.length == 0 && move.vanish.length == 0) { this.stage = 2; this.sideCheckered = this.turn; + if (this.playerColor != this.turn) + super.displayMessage(null, "Autonomous checkered!", "info-text", 2000); } else super.postPlay(move);