Attempt to not throw exception on server socket error
[vchess.git] / public / javascripts / base_rules.js
index fdfcda3..860495a 100644 (file)
@@ -10,6 +10,7 @@ class PiPo //Piece+Position
        }
 }
 
+// TODO: for animation, moves should contains "moving" and "fading" maybe...
 class Move
 {
        // o: {appear, vanish, [start,] [end,]}
@@ -331,21 +332,22 @@ class ChessRules
                if (x+shift == lastRank)
                {
                        // Promotion
+                       const pawnColor = this.getColor(x,y); //can be different for checkered
                        let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
                        promotionPieces.forEach(p => {
                                // 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}));
+                                       moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p}));
                                }
                                if (y<sizeY-1 && 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}));
+                                       moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:pawnColor,p:p}));
                                }
                        });
                }
@@ -820,7 +822,9 @@ class ChessRules
                this.shouldReturn = false;
                const maxeval = VariantRules.INFINITY;
                const color = this.turn;
-               let 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)))
@@ -836,23 +840,34 @@ class ChessRules
                for (let i=0; i<moves1.length; i++)
                {
                        moves1[i].eval = (color=="w" ? -1 : 1) * maxeval; //very low, I'm checkmated
-                       let eval2 = (color=="w" ? 1 : -1) * maxeval; //initialized with checkmate value
                        this.play(moves1[i]);
-                       // Second half-move:
-                       let moves2 = this.getAllValidMoves();
-                       // If no possible moves AND underCheck, eval2 is correct.
-                       // If !underCheck, eval2 is 0 (stalemate).
-                       if (moves2.length == 0 && this.checkGameEnd() == "1/2")
-                               eval2 = 0;
-                       for (let j=0; j<moves2.length; j++)
+                       let eval2 = undefined;
+                       if (this.atLeastOneMove())
                        {
-                               this.play(moves2[j]);
-                               let evalPos = this.atLeastOneMove()
-                                       ? this.evalPosition("yes")
-                                       : (this.checkGameEnd()=="1/2" ? 0 : (this.turn=="w"?-maxeval:maxeval));
-                               if ((color == "w" && evalPos < eval2) || (color=="b" && evalPos > 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<moves2.length; j++)
+                               {
+                                       this.play(moves2[j]);
+                                       let evalPos = undefined;
+                                       if (this.atLeastOneMove())
+                                               evalPos = this.evalPosition()
+                                       else
+                                       {
+                                               // Work with scores for Loser variant
+                                               const score = this.checkGameEnd();
+                                               evalPos = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);
+                                       }
+                                       if ((color == "w" && evalPos < eval2) || (color=="b" && evalPos > 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))
                                moves1[i].eval = eval2;
@@ -884,7 +899,7 @@ class ChessRules
                }
                else
                        return currentBest;
-               //console.log(moves1.map(m => { return [this.getNotation(m)[0], m.eval]; }));
+               //console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));
 
                candidates = [0];
                for (let j=1; j<moves1.length && moves1[j].eval == moves1[0].eval; j++)
@@ -900,13 +915,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")
                {
@@ -1059,7 +1077,7 @@ class ChessRules
                for (let i of ['w','b'])
                {
                        for (let j=0; j<2; j++)
-                               fen += this.castleFlags[i][j] ? '1' : '0';
+                               fen += (this.castleFlags[i][j] ? '1' : '0');
                }
                return fen;
        }
@@ -1067,14 +1085,8 @@ class ChessRules
        // Context: just before move is played, turn hasn't changed
        getNotation(move)
        {
-               if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING)
-               {
-                       // Castle
-                       if (move.end.y < move.start.y)
-                               return "0-0-0";
-                       else
-                               return "0-0";
-               }
+               if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING) //castle
+                       return (move.end.y < move.start.y ? "0-0-0" : "0-0");
 
                // Translate final square
                const finalSquare =
@@ -1119,15 +1131,17 @@ class ChessRules
        // The score is already computed when calling this function
        getPGN(mycolor, score, fenStart, mode)
        {
+               const zeroPad = x => { return (x<10 ? "0" : "") + x; };
                let pgn = "";
                pgn += '[Site "vchess.club"]<br>';
                const d = new Date();
                const opponent = mode=="human" ? "Anonymous" : "Computer";
                pgn += '[Variant "' + variant + '"]<br>';
-               pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() + '"]<br>';
+               pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + zeroPad(d.getDate()) + '"]<br>';
                pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]<br>';
                pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]<br>';
-               pgn += '[Fen "' + fenStart + '"]<br>';
+               pgn += '[FenStart "' + fenStart + '"]<br>';
+               pgn += '[Fen "' + this.getFen() + '"]<br>';
                pgn += '[Result "' + score + '"]<br><br>';
 
                // Standard PGN
@@ -1137,7 +1151,7 @@ class ChessRules
                                pgn += ((i/2)+1) + ".";
                        pgn += this.moves[i].notation[0] + " ";
                }
-               pgn += score + "<br><br>";
+               pgn += "<br><br>";
 
                // "Complete moves" PGN (helping in ambiguous cases)
                for (let i=0; i<this.moves.length; i++)
@@ -1146,7 +1160,6 @@ class ChessRules
                                pgn += ((i/2)+1) + ".";
                        pgn += this.moves[i].notation[1] + " ";
                }
-               pgn += score;
 
                return pgn;
        }