X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FCannibal.js;h=a08f197547d88ded964ede5635136b3d463df19a;hb=6f2f94374f1e73c375edf732d9425e575e81fff7;hp=1508f672deb32ce36ffacbb3c995a8cac2340bc1;hpb=1c58eb76b86d89b9aad29920240b12451f77ab95;p=vchess.git diff --git a/client/src/variants/Cannibal.js b/client/src/variants/Cannibal.js index 1508f672..a08f1975 100644 --- a/client/src/variants/Cannibal.js +++ b/client/src/variants/Cannibal.js @@ -33,6 +33,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]); + 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); @@ -166,7 +194,8 @@ export class CannibalRules extends ChessRules { getAllValidMoves() { const moves = super.getAllValidMoves(); - if (moves.some(m => m.vanish.length == 2)) return V.KeepCaptures(moves); + if (moves.some(m => m.vanish.length == 2 && m.appear.length == 1)) + return V.KeepCaptures(moves); return moves; } @@ -180,7 +209,7 @@ export class CannibalRules extends ChessRules { this.castleFlags[c] = [V.size.y, V.size.y]; return; } - super.updateCastleFlags(move); + super.updateCastleFlags(move, piece); } postUndo(move) {