X-Git-Url: https://git.auder.net/pieces/Cwda/r_black_rook.svg?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=0bf5114aca6f5d3dd1ae6e51879abab428ca9930;hb=f6dbe8e31a3260487664f1e0b50710b3f3efaf5f;hp=7f27e8559cae2a8b64af91b8297384d244d5cd1f;hpb=45109880413a50dec3a07298b987fb07d60630b2;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index 7f27e855..0bf5114a 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -195,16 +195,12 @@ class ChessRules return (this.turn == side && this.getColor(x,y) == side); } - // On which squares is opponent under check after our move ? (for interface) - getCheckSquares(move) + // On which squares is color under check ? (for interface) + getCheckSquares(color) { - this.play(move); - const color = this.turn; //opponent - let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]) + return this.isAttacked(this.kingPos[color], [this.getOppCol(color)]) ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate! : []; - this.undo(move); - return res; } ///////////// @@ -599,74 +595,65 @@ class ChessRules const color = this.turn; let moves = []; const [sizeX,sizeY] = [V.size.x,V.size.y]; - const shift = (color == "w" ? -1 : 1); + const shiftX = (color == "w" ? -1 : 1); const firstRank = (color == 'w' ? sizeX-1 : 0); const startRank = (color == "w" ? sizeX-2 : 1); const lastRank = (color == "w" ? 0 : sizeX-1); + const pawnColor = this.getColor(x,y); //can be different for checkered - if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank) + if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true { - // Normal moves - if (this.board[x+shift][y] == V.EMPTY) + const finalPieces = x + shiftX == lastRank + ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN] + : [V.PAWN] + // One square forward + if (this.board[x+shiftX][y] == V.EMPTY) { - moves.push(this.getBasicMove([x,y], [x+shift,y])); - // Next condition because variants with pawns on 1st rank allow them to jump - if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY) + for (let piece of finalPieces) + { + moves.push(this.getBasicMove([x,y], [x+shiftX,y], + {c:pawnColor,p:piece})); + } + // Next condition because pawns on 1st rank can generally jump + if ([startRank,firstRank].includes(x) + && this.board[x+2*shiftX][y] == V.EMPTY) { // Two squares jump - moves.push(this.getBasicMove([x,y], [x+2*shift,y])); + moves.push(this.getBasicMove([x,y], [x+2*shiftX,y])); } } // Captures - if (y>0 && this.board[x+shift][y-1] != V.EMPTY - && this.canTake([x,y], [x+shift,y-1])) - { - moves.push(this.getBasicMove([x,y], [x+shift,y-1])); - } - if (y { - // Normal move - if (this.board[x+shift][y] == V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y], {c:pawnColor,p:p})); - // Captures - if (y>0 && this.board[x+shift][y-1] != V.EMPTY - && this.canTake([x,y], [x+shift,y-1])) + if (y + shiftY >= 0 && y + shiftY < sizeY + && this.board[x+shiftX][y+shiftY] != V.EMPTY + && this.canTake([x,y], [x+shiftX,y+shiftY])) { - moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p})); - } - if (y { return !this.underCheck(m); }); + const color = this.turn; + return moves.filter(m => { + this.play(m); + const res = !this.underCheck(color); + this.undo(m); + return res; + }); } // Search for all valid moves considering current turn (for engine and game end) @@ -921,14 +914,10 @@ class ChessRules return false; } - // Is current player under check after his move ? - underCheck(move) + // Is color under check after his move ? + underCheck(color) { - const color = this.turn; - this.play(move); - let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]); - this.undo(move); - return res; + return this.isAttacked(this.kingPos[color], [this.getOppCol(color)]); } ///////////////// @@ -954,8 +943,8 @@ class ChessRules // Before move is played, update variables + flags updateVariables(move) { - const piece = this.getPiece(move.start.x,move.start.y); - const c = this.turn; + const piece = move.vanish[0].p; + const c = move.vanish[0].c; const firstRank = (c == "w" ? V.size.x-1 : 0); // Update king position + flags @@ -995,6 +984,7 @@ class ChessRules play(move, ingame) { // DEBUG: +// console.log("DO"); // if (!this.states) this.states = []; // if (!ingame) this.states.push(this.getFen()); @@ -1003,12 +993,12 @@ class ChessRules if (V.HasFlags) move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo) - this.updateVariables(move); - this.moves.push(move); if (V.HasEnpassant) this.epSquares.push( this.getEpSquare(move) ); - this.turn = this.getOppCol(this.turn); V.PlayOnBoard(this.board, move); + this.turn = this.getOppCol(this.turn); + this.moves.push(move); + this.updateVariables(move); if (!!ingame) { @@ -1019,16 +1009,17 @@ class ChessRules undo(move) { - V.UndoOnBoard(this.board, move); - this.turn = this.getOppCol(this.turn); if (V.HasEnpassant) this.epSquares.pop(); - this.moves.pop(); - this.unupdateVariables(move); if (V.HasFlags) this.disaggregateFlags(JSON.parse(move.flags)); + V.UndoOnBoard(this.board, move); + this.turn = this.getOppCol(this.turn); + this.moves.pop(); + this.unupdateVariables(move); // DEBUG: +// console.log("UNDO "+this.getNotation(move)); // if (this.getFen() != this.states[this.states.length-1]) // debugger; // this.states.pop();