+
+ static get KING_CODE() {
+ return {
+ 'p': 's',
+ 'r': 'u',
+ 'n': 'o',
+ 'b': 'c',
+ 'q': 't'
+ };
+ }
+
+ static get KING_DECODE() {
+ return {
+ 's': 'p',
+ 'u': 'r',
+ 'o': 'n',
+ 'c': 'b',
+ 't': 'q'
+ };
+ }
+
+ // Kings may be disguised:
+ getPiece(x, y) {
+ const piece = this.board[x][y].charAt(1);
+ if (Object.keys(V.KING_DECODE).includes(piece))
+ return V.KING_DECODE[piece];
+ return piece;
+ }
+
+ getPpath(b) {
+ 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);
+ const rows = V.ParseFen(fen).position.split("/");
+ if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0) {
+ for (let i = 0; i < rows.length; i++) {
+ let k = 0; //column index on board
+ for (let j = 0; j < rows[i].length; j++) {
+ const piece = rows[i].charAt(j);
+ if (Object.keys(V.KING_DECODE).includes(piece.toLowerCase())) {
+ const color = (piece.charCodeAt(0) <= 90 ? 'w' : 'b');
+ this.kingPos[color] = [i, k];
+ } else {
+ const num = parseInt(rows[i].charAt(j), 10);
+ if (!isNaN(num)) k += num - 1;
+ }
+ k++;
+ }
+ }
+ }
+ }
+