X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FCheckered.js;h=9de46ff1bca14558312086bf1e22433a0fa23128;hb=b955c65b942d09d24b5c3bed0d755d4f2f8f71f1;hp=370c6896bc182dae440569050688a6e42887056e;hpb=c794dbb87592782913af0a09784ed25e019e4d10;p=vchess.git diff --git a/public/javascripts/variants/Checkered.js b/public/javascripts/variants/Checkered.js index 370c6896..9de46ff1 100644 --- a/public/javascripts/variants/Checkered.js +++ b/public/javascripts/variants/Checkered.js @@ -4,6 +4,7 @@ class CheckeredRules extends ChessRules { return b[0]=='c' ? "Checkered/"+b : b; } + static board2fen(b) { const checkered_codes = { @@ -17,6 +18,7 @@ class CheckeredRules extends ChessRules return checkered_codes[b[1]]; return ChessRules.board2fen(b); } + static fen2board(f) { // Tolerate upper-case versions of checkered pieces (why not?) @@ -37,7 +39,8 @@ class CheckeredRules extends ChessRules return ChessRules.fen2board(f); } - static get PIECES() { + static get PIECES() + { return ChessRules.PIECES.concat(['s','t','u','c','o']); } @@ -65,13 +68,12 @@ class CheckeredRules extends ChessRules } } - // Aggregates flags into one object - get flags() { + aggregateFlags() + { return [this.castleFlags, this.pawnFlags]; } - // Reverse operation - parseFlags(flags) + disaggregateFlags(flags) { this.castleFlags = flags[0]; this.pawnFlags = flags[1]; @@ -149,7 +151,10 @@ class CheckeredRules extends ChessRules const L = this.moves.length; if (L > 0 && this.oppositeMoves(this.moves[L-1], m)) return false; - return !this.underCheck(m); + this.play(m); + const res = !this.underCheck(color); + this.undo(m); + return res; }); } @@ -174,27 +179,21 @@ class CheckeredRules extends ChessRules return false; } - underCheck(move) + underCheck(color) { - const color = this.turn; - this.play(move); - let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c']); - this.undo(move); - return res; + return this.isAttacked(this.kingPos[color], [V.GetOppCol(color),'c']); } - getCheckSquares(move) + getCheckSquares(color) { - this.play(move); - const color = this.turn; - this.moves.push(move); //artifically change turn, for checkered pawns (TODO) + // Artifically change turn, for checkered pawns + this.turn = V.GetOppCol(color); const kingAttacked = this.isAttacked( - this.kingPos[color], [this.getOppCol(color),'c']); + this.kingPos[color], [V.GetOppCol(color),'c']); let res = kingAttacked - ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] //need to duplicate! - : [ ]; - this.moves.pop(); - this.undo(move); + ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate! + : []; + this.turn = color; return res; } @@ -211,11 +210,11 @@ class CheckeredRules extends ChessRules { const color = this.turn; // Artifically change turn, for checkered pawns - this.turn = this.getOppCol(this.turn); - const res = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c']) + this.turn = V.GetOppCol(this.turn); + const res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color),'c']) ? (color == "w" ? "0-1" : "1-0") : "1/2"; - this.turn = this.getOppCol(this.turn); + this.turn = V.GetOppCol(this.turn); return res; } @@ -242,7 +241,7 @@ class CheckeredRules extends ChessRules { const randFen = ChessRules.GenRandInitFen(); // Add 16 pawns flags: - return randFen.replace(" 1111 w", " 11111111111111111111 w"); + return randFen.replace(" w 1111", " w 11111111111111111111"); } getFlagsFen() @@ -269,10 +268,9 @@ class CheckeredRules extends ChessRules } // Translate final square - let finalSquare = - String.fromCharCode(97 + move.end.y) + (V.size.x-move.end.x); + const finalSquare = V.CoordsToSquare(move.end); - let piece = this.getPiece(move.start.x, move.start.y); + const piece = this.getPiece(move.start.x, move.start.y); if (piece == V.PAWN) { // Pawn move @@ -280,7 +278,7 @@ class CheckeredRules extends ChessRules if (move.vanish.length > 1) { // Capture - let startColumn = String.fromCharCode(97 + move.start.y); + const startColumn = V.CoordToColumn(move.start.y); notation = startColumn + "x" + finalSquare + "=" + move.appear[0].p.toUpperCase(); }