X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=a10a43ca270f405ff5dde22e0c7ba04170f577c6;hb=331fc58c932d6d7055b202d0c6dc0d77212a89f8;hp=860495a4236c3109bf77874172f353b7b878be2b;hpb=098e8468ae7a52a55850c09f90506f52b8133567;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index 860495a4..a10a43ca 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]} @@ -51,6 +54,7 @@ class ChessRules constructor(fen, moves) { this.moves = moves; + this.hashStates = {}; //for repetitions detection // Use fen string to initialize variables, flags and board this.board = VariantRules.GetBoard(fen); this.setFlags(fen); @@ -61,7 +65,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= 8) - { - const L = this.moves.length; - if (_.isEqual(this.moves[L-1], this.moves[L-5]) && - _.isEqual(this.moves[L-2], this.moves[L-6]) && - _.isEqual(this.moves[L-3], this.moves[L-7]) && - _.isEqual(this.moves[L-4], this.moves[L-8])) - { - return true; - } - } - return false; + return Object.values(this.hashStates).some(elt => { return (elt >= 3); }); } // Is game over ? And if yes, what is the score ? @@ -819,7 +838,6 @@ class ChessRules // NOTE: works also for extinction chess because depth is 3... getComputerMove() { - this.shouldReturn = false; const maxeval = VariantRules.INFINITY; const color = this.turn; // Some variants may show a bigger moves list to the human (Switching), @@ -869,8 +887,11 @@ class ChessRules 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); }); @@ -881,14 +902,17 @@ class ChessRules candidates.push(j); let currentBest = moves1[_.sample(candidates, 1)]; + // From here, depth >= 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) { 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 + @@ -978,9 +1002,9 @@ class ChessRules // Setup the initial random (assymetric) position static GenRandInitFen() { - let pieces = [new Array(8), new Array(8)]; + let pieces = { "w": new Array(8), "b": new Array(8) }; // Shuffle pieces on first and last rank - for (let c = 0; c <= 1; c++) + for (let c of ["w","b"]) { let positions = _.range(8); @@ -1022,11 +1046,10 @@ class ChessRules pieces[c][knight2Pos] = 'n'; pieces[c][rook2Pos] = 'r'; } - let fen = pieces[0].join("") + + return pieces["b"].join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + - pieces[1].join("").toUpperCase() + + pieces["w"].join("").toUpperCase() + " 1111"; //add flags - return fen; } // Return current fen according to pieces+colors state @@ -1137,7 +1160,8 @@ class ChessRules const d = new Date(); const opponent = mode=="human" ? "Anonymous" : "Computer"; pgn += '[Variant "' + variant + '"]
'; - pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + zeroPad(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 += '[FenStart "' + fenStart + '"]
';