X-Git-Url: https://git.auder.net/doc/screen_pairings_scoring.png?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=3a0f1c6332d1c4d4b5b2dc48c5a4c96d0bae3b47;hb=f3802fcd1279e5d07cdff1341fc5e17c5296dc9c;hp=de1a0d0fa4ec4eab521532cafa123f3b5f71c113;hpb=762b7c9ca36aedb3e720b31c103be429446060d3;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index de1a0d0f..3a0f1c63 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -52,8 +52,8 @@ class ChessRules 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); } 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",...] @@ -517,6 +511,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) @@ -603,6 +623,7 @@ class ChessRules return false; } + // Is color c under check after move ? underCheck(move, c) { this.play(move); @@ -611,6 +632,17 @@ class ChessRules return res; } + // On which squares is color c under check (after move) ? + getCheckSquares(move, c) + { + this.play(move); + let res = this.isAttacked(this.kingPos[c], this.getOppCol(c)) + ? [ JSON.parse(JSON.stringify(this.kingPos[c])) ] //need to duplicate! + : [ ]; + this.undo(move); + return res; + } + // Apply a move on board static PlayOnBoard(board, move) { @@ -676,12 +708,15 @@ class ChessRules this.movesCount++; } - 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) @@ -709,8 +744,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 "*"; @@ -779,14 +813,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