X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=11de1471fa398a5dc912f8a5fd80d67a01e05702;hb=cd4cad0468612cf00c6e6879554e3cee58d4b1f9;hp=b604e7970ffc357c4b05b0217836756485bde3c6;hpb=4b5fe3061829e184f9ad86a13d831eda22d95343;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index b604e797..11de1471 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -46,14 +46,14 @@ class ChessRules ///////////////// // INITIALIZATION - // fen = "position flags epSquare movesCount" + // fen == "position flags" constructor(fen, moves) { this.moves = moves; // Use fen string to initialize variables, flags and board - this.initVariables(fen); - this.flags = VariantRules.GetFlags(fen); this.board = VariantRules.GetBoard(fen); + this.flags = VariantRules.GetFlags(fen); + this.initVariables(fen); } initVariables(fen) @@ -98,14 +98,8 @@ class ChessRules j++; } } - let epSq = undefined; - if (fenParts[2] != "-") - { - const digits = fenParts[2].split(","); //3,2 ... - epSq = { x:Number.parseInt(digits[0]), y:Number.parseInt(digits[1]) }; - } + const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined; this.epSquares = [ epSq ]; - this.movesCount = Number.parseInt(fenParts[3]); } // Turn diagram fen into double array ["wb","wp","bk",...] @@ -158,7 +152,7 @@ class ChessRules return L>0 ? this.moves[L-1] : null; } get turn() { - return this.movesCount%2==0 ? 'w' : 'b'; + return this.moves.length%2==0 ? 'w' : 'b'; } // Pieces codes @@ -476,8 +470,8 @@ class ChessRules canIplay(color, sq) { - return ((color=='w' && this.movesCount%2==0) - || (color=='b' && this.movesCount%2==1)) + return ((color=='w' && this.moves.length%2==0) + || (color=='b' && this.moves.length%2==1)) && this.getColor(sq[0], sq[1]) == color; } @@ -666,7 +660,7 @@ class ChessRules board[psq.x][psq.y] = psq.c + psq.p; } - // Before move is played: + // Before move is played, update variables + flags updateVariables(move) { const piece = this.getPiece(move.start.x,move.start.y); @@ -697,37 +691,33 @@ class ChessRules } } - play(move, ingame) + unupdateVariables(move) { - // Save flags (for undo) - move.flags = JSON.stringify(this.flags); //TODO: less costly - this.updateVariables(move); + // (Potentially) Reset king position + const c = this.getColor(move.start.x,move.start.y); + if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING) + this.kingPos[c] = [move.start.x, move.start.y]; + } + play(move, ingame) + { if (!!ingame) - { move.notation = this.getNotation(move); - this.moves.push(move); - } + // Save flags (for undo) + move.flags = JSON.stringify(this.flags); //TODO: less costly? + this.updateVariables(move); + this.moves.push(move); this.epSquares.push( this.getEpSquare(move) ); VariantRules.PlayOnBoard(this.board, move); - this.movesCount++; } - undo(move, ingame) + undo(move) { VariantRules.UndoOnBoard(this.board, move); this.epSquares.pop(); - this.movesCount--; - - if (!!ingame) - this.moves.pop(); - - // Update king position, and reset stored/computed flags - const c = this.getColor(move.start.x,move.start.y); - if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING) - this.kingPos[c] = [move.start.x, move.start.y]; - + this.moves.pop(); + this.unupdateVariables(move); this.flags = JSON.parse(move.flags); } @@ -838,8 +828,7 @@ class ChessRules alphabeta(color, oppCol, depth, alpha, beta) { - const moves = this.getAllValidMoves(color); - if (moves.length == 0) + if (!this.atLeastOneMove(color)) { switch (this.checkGameEnd(color)) { @@ -849,6 +838,7 @@ class ChessRules } if (depth == 0) return this.evalPosition(); + const moves = this.getAllValidMoves(color); let v = color=="w" ? -1000 : 1000; if (color == "w") { @@ -949,19 +939,14 @@ class ChessRules let fen = pieces[0].join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + pieces[1].join("").toUpperCase() + - " 1111 - 0"; //flags + enPassant + movesCount + " 1111"; //add flags return fen; } // Return current fen according to pieces+colors state getFen() { - const L = this.epSquares.length; - const epSq = this.epSquares[L-1]===undefined - ? "-" - : this.epSquares[L-1].x+","+this.epSquares[L-1].y; - return this.getBaseFen() + " " + this.getFlagsFen() - + " " + epSq + " " + this.movesCount; + return this.getBaseFen() + " " + this.getFlagsFen(); } getBaseFen()