Implementation of pieces movements + captures in Ultima
[vchess.git] / public / javascripts / base_rules.js
index 5c1ef97..5020094 100644 (file)
@@ -729,6 +729,10 @@ class ChessRules
 
        play(move, ingame)
        {
+               // DEBUG:
+//             if (!this.states) this.states = [];
+//             if (!ingame) this.states.push(JSON.stringify(this.board));
+
                if (!!ingame)
                        move.notation = [this.getNotation(move), this.getLongNotation(move)];
 
@@ -746,12 +750,18 @@ class ChessRules
                this.moves.pop();
                this.unupdateVariables(move);
                this.parseFlags(JSON.parse(move.flags));
+
+               // DEBUG:
+//             if (JSON.stringify(this.board) != this.states[this.states.length-1])
+//                     debugger;
+//             this.states.pop();
        }
 
        //////////////
        // END OF GAME
 
        // Basic check for 3 repetitions (in the last moves only)
+       // TODO: extend to usual 3-repetition recognition (storing FEN with move?)
        checkRepetition()
        {
                if (this.moves.length >= 8)
@@ -824,7 +834,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),
@@ -889,14 +898,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<moves1.length; i++)
                        {
-                               if (this.shouldReturn)
-                                       return currentBest; //depth-2, minimum
+                               if (Date.now()-timeStart >= 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 +
@@ -986,9 +998,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);
 
@@ -1030,11 +1042,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