X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FCannibal.js;h=a0da0fd6c341cfe41af4cd1721a3609fa7245b44;hb=ded43c88fad60fd8f9bb46aabd67f3f2092f65f3;hp=7860ab042faa465ef4377cc5458720efbb6ef451;hpb=bb688df52df0713aba7b2c1c068614544f5ae96d;p=vchess.git diff --git a/client/src/variants/Cannibal.js b/client/src/variants/Cannibal.js index 7860ab04..a0da0fd6 100644 --- a/client/src/variants/Cannibal.js +++ b/client/src/variants/Cannibal.js @@ -1,6 +1,7 @@ import { ChessRules, Move, PiPo } from "@/base_rules"; export class CannibalRules extends ChessRules { + static get KING_CODE() { return { 'p': 's', @@ -33,6 +34,34 @@ export class CannibalRules extends ChessRules { return (Object.keys(V.KING_DECODE).includes(b[1]) ? "Cannibal/" : "") + b; } + static IsGoodPosition(position) { + if (position.length == 0) return false; + const rows = position.split("/"); + if (rows.length != V.size.x) return false; + let kings = { "w": 0, "b": 0 }; + const allPiecesCodes = V.PIECES.concat(Object.keys(V.KING_DECODE)); + const kingBlackCodes = Object.keys(V.KING_DECODE).concat(['k']); + const kingWhiteCodes = + Object.keys(V.KING_DECODE).map(k => k.toUpperCase()).concat(['K']); + for (let row of rows) { + let sumElts = 0; + for (let i = 0; i < row.length; i++) { + if (kingBlackCodes.includes(row[i])) kings['b']++; + else if (kingWhiteCodes.includes(row[i])) kings['w']++; + if (allPiecesCodes.includes(row[i].toLowerCase())) sumElts++; + else { + const num = parseInt(row[i], 10); + if (isNaN(num)) return false; + sumElts += num; + } + } + if (sumElts != V.size.y) return false; + } + // Both kings should be on board, only one of each color: + if (Object.values(kings).some(v => v != 1)) return false; + return true; + } + // Kings may be disguised: setOtherVariables(fen) { super.setOtherVariables(fen); @@ -46,7 +75,7 @@ export class CannibalRules extends ChessRules { const color = (piece.charCodeAt(0) <= 90 ? 'w' : 'b'); this.kingPos[color] = [i, k]; } else { - const num = parseInt(rows[i].charAt(j)); + const num = parseInt(rows[i].charAt(j), 10); if (!isNaN(num)) k += num - 1; } k++; @@ -60,7 +89,7 @@ export class CannibalRules extends ChessRules { return moves.filter(m => m.vanish.length == 2 && m.appear.length == 1); } - // Stop at the first capture found (if any) + // Stop at the first capture found (if any) atLeastOneCapture() { const color = this.turn; const oppCol = V.GetOppCol(color); @@ -83,38 +112,9 @@ export class CannibalRules extends ChessRules { // Because of the disguised kings, getPiece() could be wrong: // use board[x][y][1] instead (always valid). getBasicMove([sx, sy], [ex, ey], tr) { - const initColor = this.getColor(sx, sy); - const initPiece = this.board[sx][sy].charAt(1); - let mv = new Move({ - appear: [ - new PiPo({ - x: ex, - y: ey, - c: tr ? tr.c : initColor, - p: tr ? tr.p : initPiece - }) - ], - vanish: [ - new PiPo({ - x: sx, - y: sy, - c: initColor, - p: initPiece - }) - ] - }); - - // The opponent piece disappears if we take it - if (this.board[ex][ey] != V.EMPTY) { - mv.vanish.push( - new PiPo({ - x: ex, - y: ey, - c: this.getColor(ex, ey), - p: this.board[ex][ey].charAt(1) - }) - ); + let mv = super.getBasicMove([sx, sy], [ex, ey], tr); + if (this.board[ex][ey] != V.EMPTY) { // If the captured piece has a different nature: take it as well if (mv.vanish[0].p != mv.vanish[1].p) { if ( @@ -122,7 +122,8 @@ export class CannibalRules extends ChessRules { Object.keys(V.KING_DECODE).includes(mv.vanish[0].p) ) { mv.appear[0].p = V.KING_CODE[mv.vanish[1].p]; - } else mv.appear[0].p = mv.vanish[1].p; + } + else mv.appear[0].p = mv.vanish[1].p; } } else if (!!tr && mv.vanish[0].p != V.PAWN) @@ -179,8 +180,9 @@ export class CannibalRules extends ChessRules { this.kingPos[c][0] = move.appear[0].x; this.kingPos[c][1] = move.appear[0].y; this.castleFlags[c] = [V.size.y, V.size.y]; - return; } + // Next call is still required because the king may eat an opponent's rook + // TODO: castleFlags will be turned off twice then. super.updateCastleFlags(move, piece); } @@ -203,7 +205,19 @@ export class CannibalRules extends ChessRules { }; } - static get SEARCH_DEPTH() { - return 4; + getNotation(move) { + let notation = super.getNotation(move); + const lastRank = (move.appear[0].c == "w" ? 0 : 7); + if ( + move.end.x != lastRank && + this.getPiece(move.start.x, move.start.y) == V.PAWN && + move.vanish.length == 2 && + move.appear[0].p != V.PAWN + ) { + // Fix "promotion" (transform indicator) from base_rules notation + notation = notation.slice(0, -2); + } + return notation; } + };