X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=9a4ef9120ca8463a9e4e36d4197aead688c94299;hb=e64a4effa6b357addc3253504870cf1a6fc29977;hp=576bdd9b0eb1b47cb4287ab977529a4531f85279;hpb=1d184b4c016a645228251ce984d4c980e60420b0;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index 576bdd9b..9a4ef912 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -47,9 +47,9 @@ class ChessRules // INITIALIZATION // fen = "position flags epSquare movesCount" - constructor(fen) + constructor(fen, moves) { - this.moves = []; + this.moves = moves; // Use fen string to initialize variables, flags and board this.initVariables(fen); this.flags = VariantRules.GetFlags(fen); @@ -517,6 +517,32 @@ class ChessRules // No: if happen on last 1/2 move, could lead to forbidden moves, wrong evals return this.filterValid(potentialMoves); } + + // Stop at the first move found + atLeastOneMove(color) + { + const oppCol = this.getOppCol(color); + let [sizeX,sizeY] = VariantRules.size; + for (var i=0; i 0) + { + for (let i=0; i 0) + return true; + } + } + } + } + } + return false; + } // Check if pieces of color 'color' are attacking square x,y isAttacked(sq, color) @@ -665,20 +691,26 @@ class ChessRules move.flags = JSON.stringify(this.flags); //TODO: less costly this.updateVariables(move); + if (!!ingame) + { + move.notation = this.getNotation(move); + this.moves.push(move); + } + this.epSquares.push( this.getEpSquare(move) ); VariantRules.PlayOnBoard(this.board, move); this.movesCount++; - - if (!!ingame) - this.moves.push(move); } - undo(move) + undo(move, ingame) { 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) @@ -706,8 +738,7 @@ class ChessRules } } - // TODO: not required to generate ALL: just need one (callback ? hook ? ...) - if (this.getAllValidMoves(color).length > 0) + if (this.atLeastOneMove(color)) { // game not over return "*"; @@ -776,14 +807,14 @@ class ChessRules moves1.sort( (a,b) => { return (color=="w" ? 1 : -1) * (b.eval - a.eval); }); // TODO: show current analyzed move for depth 3, allow stopping eval (return moves1[0]) -// for (let i=0; i { return (color=="w" ? 1 : -1) * (b.eval - a.eval); }); + for (let i=0; i { return (color=="w" ? 1 : -1) * (b.eval - a.eval); }); let candidates = [0]; //indices of candidates moves for (let j=1; j 1 ? "x" : "") + finalSquare; } } + + // The score is already computed when calling this function + getPGN(mycolor, score, fenStart) + { + let pgn = ""; + pgn += '[Site "vchess.club"]
'; + const d = new Date(); + pgn += '[Date "' + d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() + '"]
'; + pgn += '[White "' + (mycolor=='w'?'Myself':'Anonymous') + '"]
'; + pgn += '[Black "' + (mycolor=='b'?'Myself':'Anonymous') + '"]
'; + pgn += '[Fen "' + fenStart + '"]
'; + pgn += '[Result "' + score + '"]

'; + + for (let i=0; i