X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=750cd2d4ab72313fc4e1149dd76103ff4926147a;hb=32cfcea44bf00b0c6c4d172cca715823076ff490;hp=42775dab1553cae4d181eb522f28353cada25f1b;hpb=efb20746c30ac26f258f9c5de6dfb70b91980de4;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index 42775dab..750cd2d4 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -1,3 +1,6 @@ +// (Orthodox) Chess rules are defined in ChessRules class. +// Variants generally inherit from it, and modify some parts. + class PiPo //Piece+Position { // o: {piece[p], color[c], posX[x], posY[y]} @@ -10,6 +13,7 @@ class PiPo //Piece+Position } } +// TODO: for animation, moves should contains "moving" and "fading" maybe... class Move { // o: {appear, vanish, [start,] [end,]} @@ -60,7 +64,7 @@ class ChessRules { this.INIT_COL_KING = {'w':-1, 'b':-1}; this.INIT_COL_ROOK = {'w':[-1,-1], 'b':[-1,-1]}; - this.kingPos = {'w':[-1,-1], 'b':[-1,-1]}; //respective squares of white and black king + this.kingPos = {'w':[-1,-1], 'b':[-1,-1]}; //squares of white and black king const fenParts = fen.split(" "); const position = fenParts[0].split("/"); for (let i=0; i=0 && i=0 && j=0 && i=0 && j= 0 && x+shift < sizeX && x+shift != lastRank) { @@ -308,7 +312,7 @@ class ChessRules if (this.board[x+shift][y] == V.EMPTY) { moves.push(this.getBasicMove([x,y], [x+shift,y])); - // Next condition because variants with pawns on 1st rank generally allow them to jump + // 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) { // Two squares jump @@ -316,25 +320,38 @@ class ChessRules } } // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) + if (y>0 && this.canTake([x,y], [x+shift,y-1]) + && this.board[x+shift][y-1] != V.EMPTY) + { 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:color,p:p})); + moves.push(this.getBasicMove([x,y], [x+shift,y], {c:pawnColor,p:p})); // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p})); - if (y0 && this.canTake([x,y], [x+shift,y-1]) + && this.board[x+shift][y-1] != V.EMPTY) + { + moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p})); + } + if (y= 8) { - // NOTE: crude detection, only moves repetition const L = this.moves.length; if (_.isEqual(this.moves[L-1], this.moves[L-5]) && _.isEqual(this.moves[L-2], this.moves[L-6]) && @@ -756,6 +769,7 @@ class ChessRules return false; } + // Is game over ? And if yes, what is the score ? checkGameOver() { if (this.checkRepetition()) @@ -773,7 +787,7 @@ class ChessRules { const color = this.turn; // No valid move: stalemate or checkmate? - if (!this.isAttacked(this.kingPos[color], this.getOppCol(color))) + if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)])) return "1/2"; // OK, checkmate return color == "w" ? "0-1" : "1-0"; @@ -808,54 +822,84 @@ class ChessRules } // Assumption: at least one legal move - getComputerMove(moves1) //moves1 might be precomputed (Magnetic chess) + // NOTE: works also for extinction chess because depth is 3... + getComputerMove() { - this.shouldReturn = false; const maxeval = VariantRules.INFINITY; const color = this.turn; - if (!moves1) - moves1 = this.getAllValidMoves(); + // Some variants may show a bigger moves list to the human (Switching), + // thus the argument "computer" below (which is generally ignored) + let moves1 = this.getAllValidMoves("computer"); + + // Can I mate in 1 ? (for Magnetic & Extinction) + for (let i of _.shuffle(_.range(moves1.length))) + { + this.play(moves1[i]); + const finish = (Math.abs(this.evalPosition()) >= VariantRules.THRESHOLD_MATE); + this.undo(moves1[i]); + if (finish) + return moves1[i]; + } // Rank moves using a min-max at depth 2 for (let i=0; i eval2)) - eval2 = evalPos; - this.undo(moves2[j]); + eval2 = (color=="w" ? 1 : -1) * maxeval; //initialized with checkmate value + // Second half-move: + let moves2 = this.getAllValidMoves("computer"); + for (let j=0; j eval2)) + eval2 = evalPos; + this.undo(moves2[j]); + } + } + else + { + const score = this.checkGameEnd(); + eval2 = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval); } - if ((color=="w" && eval2 > moves1[i].eval) || (color=="b" && eval2 < moves1[i].eval)) + if ((color=="w" && eval2 > moves1[i].eval) + || (color=="b" && eval2 < moves1[i].eval)) + { moves1[i].eval = eval2; + } this.undo(moves1[i]); } moves1.sort( (a,b) => { return (color=="w" ? 1 : -1) * (b.eval - a.eval); }); + //console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; })); let candidates = [0]; //indices of candidates moves for (let j=1; j= 3: may take a while, so we control time + const timeStart = Date.now(); + + // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...) if (VariantRules.SEARCH_DEPTH >= 3 && Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE) { - // TODO: show current analyzed move for depth 3, allow stopping eval (return moves1[0]) for (let i=0; i= 5000) //more than 5 seconds + return currentBest; //depth 2 at least this.play(moves1[i]); // 0.1 * oldEval : heuristic to avoid some bad moves (not all...) moves1[i].eval = 0.1*moves1[i].eval + @@ -866,11 +910,11 @@ class ChessRules } else return currentBest; + //console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; })); candidates = [0]; for (let j=1; j { return [this.getNotation(m), m.eval]; })); return moves1[_.sample(candidates, 1)]; } @@ -882,13 +926,16 @@ class ChessRules { switch (this.checkGameEnd()) { - case "1/2": return 0; - default: return color=="w" ? -maxeval : maxeval; + case "1/2": + return 0; + default: + const score = this.checkGameEnd(); + return (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval); } } if (depth == 0) return this.evalPosition(); - const moves = this.getAllValidMoves(); + const moves = this.getAllValidMoves("computer"); let v = color=="w" ? -maxeval : maxeval; if (color == "w") { @@ -921,7 +968,7 @@ class ChessRules { const [sizeX,sizeY] = VariantRules.size; let evaluation = 0; - //Just count material for now + // Just count material for now for (let i=0; i { return (x<10 ? "0" : "") + x; }; let pgn = ""; pgn += '[Site "vchess.club"]
'; const d = new Date(); const opponent = mode=="human" ? "Anonymous" : "Computer"; pgn += '[Variant "' + variant + '"]
'; - pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() + '"]
'; + pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) + + '-' + zeroPad(d.getDate()) + '"]
'; pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]
'; pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]
'; - pgn += '[Fen "' + fenStart + '"]
'; + pgn += '[FenStart "' + fenStart + '"]
'; + pgn += '[Fen "' + this.getFen() + '"]
'; pgn += '[Result "' + score + '"]

'; + // Standard PGN + for (let i=0; i