Add traces to understand what happens in MarseilleChess about turn/subturn
[vchess.git] / public / javascripts / variants / Marseille.js
index 7ac928f..0d228b0 100644 (file)
@@ -73,40 +73,37 @@ class MarseilleRules extends ChessRules
                const startRank = (color == "w" ? sizeX-2 : 1);
                const lastRank = (color == "w" ? 0 : sizeX-1);
                const pawnColor = this.getColor(x,y); //can be different for checkered
+               const finalPieces = x + shiftX == lastRank
+                       ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
+                       : [V.PAWN];
 
-               if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true
+               // One square forward
+               if (this.board[x+shiftX][y] == V.EMPTY)
                {
-                       const finalPieces = x + shiftX == lastRank
-                               ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
-                               : [V.PAWN]
-                       // One square forward
-                       if (this.board[x+shiftX][y] == V.EMPTY)
+                       for (let piece of finalPieces)
                        {
-                               for (let piece of finalPieces)
-                               {
-                                       moves.push(this.getBasicMove([x,y], [x+shiftX,y],
-                                               {c:pawnColor,p:piece}));
-                               }
-                               // Next condition because pawns on 1st rank can generally jump
-                               if ([startRank,firstRank].includes(x)
-                                       && this.board[x+2*shiftX][y] == V.EMPTY)
-                               {
-                                       // Two squares jump
-                                       moves.push(this.getBasicMove([x,y], [x+2*shiftX,y]));
-                               }
+                               moves.push(this.getBasicMove([x,y], [x+shiftX,y],
+                                       {c:pawnColor,p:piece}));
                        }
-                       // Captures
-                       for (let shiftY of [-1,1])
+                       // Next condition because pawns on 1st rank can generally jump
+                       if ([startRank,firstRank].includes(x)
+                               && this.board[x+2*shiftX][y] == V.EMPTY)
                        {
-                               if (y + shiftY >= 0 && y + shiftY < sizeY
-                                       && this.board[x+shiftX][y+shiftY] != V.EMPTY
-                                       && this.canTake([x,y], [x+shiftX,y+shiftY]))
+                               // Two squares jump
+                               moves.push(this.getBasicMove([x,y], [x+2*shiftX,y]));
+                       }
+               }
+               // Captures
+               for (let shiftY of [-1,1])
+               {
+                       if (y + shiftY >= 0 && y + shiftY < sizeY
+                               && this.board[x+shiftX][y+shiftY] != V.EMPTY
+                               && this.canTake([x,y], [x+shiftX,y+shiftY]))
+                       {
+                               for (let piece of finalPieces)
                                {
-                                       for (let piece of finalPieces)
-                                       {
-                                               moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
-                                                       {c:pawnColor,p:piece}));
-                                       }
+                                       moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
+                                               {c:pawnColor,p:piece}));
                                }
                        }
                }
@@ -127,6 +124,7 @@ class MarseilleRules extends ChessRules
                {
                        if (this.subTurn == 1 || (epSqs.length == 2 &&
                                // Was this en-passant capture already played at subturn 1 ?
+                               // (Or maybe the opponent filled the en-passant square with a piece)
                                this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY))
                        {
                                if (sq.x == x+shiftX && Math.abs(sq.y - y) == 1)
@@ -148,10 +146,12 @@ class MarseilleRules extends ChessRules
 
        play(move, ingame)
        {
-//             console.log("play " + this.getNotation(move));
-//             console.log(this.turn + " "+ this.subTurn);
                if (!!ingame)
+               {
                        move.notation = [this.getNotation(move), this.getLongNotation(move)];
+                       // In this special case, we also need the "move color":
+                       move.color = this.turn;
+               }
                move.flags = JSON.stringify(this.aggregateFlags());
                let lastEpsq = this.epSquares[this.epSquares.length-1];
                const epSq = this.getEpSquare(move);
@@ -185,7 +185,6 @@ class MarseilleRules extends ChessRules
                this.updateVariables(move);
                if (!!ingame)
                        move.hash = hex_md5(this.getFen());
-               //console.log(move.checkOnSubturn1 + " " +this.turn + " "+ this.subTurn);
        }
 
        undo(move)
@@ -220,8 +219,6 @@ class MarseilleRules extends ChessRules
                }
                this.moves.pop();
                this.unupdateVariables(move);
-//             console.log("UNDO " + this.getNotation(move));
-//             console.log(this.turn + " "+ this.subTurn);
        }
 
        // NOTE:  GenRandInitFen() is OK,
@@ -230,10 +227,15 @@ class MarseilleRules extends ChessRules
        // No alpha-beta here, just adapted min-max at depth 2(+1)
        getComputerMove()
        {
+               if (this.subTurn == 2)
+                       return null; //TODO: imperfect interface setup
+
                const maxeval = V.INFINITY;
                const color = this.turn;
                const oppCol = this.getOppCol(this.turn);
-
+//if (this.moves.length == 25)
+//     debugger;
+console.log("turn before " + this.turn + " " + this.subTurn);
                // Search best (half) move for opponent turn
                const getBestMoveEval = () => {
                        let moves = this.getAllValidMoves();
@@ -271,11 +273,16 @@ class MarseilleRules extends ChessRules
                };
 
                let moves11 = this.getAllValidMoves();
+console.log("turn after " + this.turn + " " + this.subTurn);
+
+if (this.moves.length == 25)
+       debugger;
+
+
                let doubleMoves = [];
                // Rank moves using a min-max at depth 2
                for (let i=0; i<moves11.length; i++)
                {
-                       moves11[i].eval = (color=="w" ? -1 : 1) * maxeval;
                        this.play(moves11[i]);
                        if (this.turn != color)
                        {
@@ -292,10 +299,66 @@ class MarseilleRules extends ChessRules
                                                moves:[moves11[i],moves12[j]],
                                                eval:getBestMoveEval()});
                                        this.undo(moves12[j]);
+                       console.log("------ after undo of " + this.getNotation(moves12[j]) + " " + this.turn + " " + this.subTurn);
                                }
                        }
                        this.undo(moves11[i]);
+
+                       console.log("after undo of " + this.getNotation(moves11[i]) + " " + this.turn + " " + this.subTurn);
+
                }
+console.log("turn interm " + this.turn + " " + this.subTurn);
+
+               for (let i=0; i<doubleMoves.length; i++)
+               {
+                       if (doubleMoves[i].moves.length == 2)
+                       {
+                               if (this.moves.length == 1
+                                       && this.getNotation(doubleMoves[i].moves[0])=="a5"
+                                       && this.getNotation(doubleMoves[i].moves[1])=="h6")
+                               {
+                                       return doubleMoves[i].moves;
+                               }
+                               if (this.moves.length == 5
+                                       && this.getNotation(doubleMoves[i].moves[0])=="c6"
+                                       && this.getNotation(doubleMoves[i].moves[1])=="Kc7")
+                               {
+                                       return doubleMoves[i].moves;
+                               }
+                               if (this.moves.length == 9
+                                       && this.getNotation(doubleMoves[i].moves[0])=="d6"
+                                       && this.getNotation(doubleMoves[i].moves[1])=="dxe5")
+                               {
+                                       return doubleMoves[i].moves;
+                               }
+                               if (this.moves.length == 13
+                                       && this.getNotation(doubleMoves[i].moves[0])=="fxe6"
+                                       && this.getNotation(doubleMoves[i].moves[1])=="Rxf1")
+                               {
+                                       return doubleMoves[i].moves;
+                               }
+                               if (this.moves.length == 17
+                                       && this.getNotation(doubleMoves[i].moves[0])=="Nb6"
+                                       && this.getNotation(doubleMoves[i].moves[1])=="Bg6")
+                               {
+                                       return doubleMoves[i].moves;
+                               }
+                               if (this.moves.length == 21
+                                       && this.getNotation(doubleMoves[i].moves[0])=="Bxe4"
+                                       && this.getNotation(doubleMoves[i].moves[1])=="Nxd3") //Bxd3
+                               {
+                                       return doubleMoves[i].moves;
+                               }
+                               if (this.moves.length == 25
+                                       && this.getNotation(doubleMoves[i].moves[0])=="Na4"
+                                       && this.getNotation(doubleMoves[i].moves[1])=="xb6") //Nxb6
+                               {
+                                       debugger;
+                                       return doubleMoves[i].moves;
+                               }
+                       }
+               }
+console.log("turn intermBIS " + this.turn + " " + this.subTurn);
 
                doubleMoves.sort( (a,b) => {
                        return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
@@ -306,11 +369,61 @@ class MarseilleRules extends ChessRules
                {
                        candidates.push(i);
                }
+console.log("turn END " + this.turn + " " + this.subTurn);
+
                const selected = doubleMoves[_.sample(candidates, 1)].moves;
                if (selected.length == 1)
                        return selected[0];
                return selected;
        }
+
+       getPGN(mycolor, score, fenStart, mode)
+       {
+               let pgn = "";
+               pgn += '[Site "vchess.club"]<br>';
+               const opponent = mode=="human" ? "Anonymous" : "Computer";
+               pgn += '[Variant "' + variant + '"]<br>';
+               pgn += '[Date "' + getDate(new Date()) + '"]<br>';
+               pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]<br>';
+               pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]<br>';
+               pgn += '[FenStart "' + fenStart + '"]<br>';
+               pgn += '[Fen "' + this.getFen() + '"]<br>';
+               pgn += '[Result "' + score + '"]<br><br>';
+
+               let counter = 1;
+               let i = 0;
+               while (i < this.moves.length)
+               {
+                       pgn += (counter++) + ".";
+                       for (let color of ["w","b"])
+                       {
+                               let move = "";
+                               while (i < this.moves.length && this.moves[i].color == color)
+                                       move += this.moves[i++].notation[0] + ",";
+                               move = move.slice(0,-1); //remove last comma
+                               pgn += move + (i < this.moves.length-1 ? " " : "");
+                       }
+               }
+               pgn += "<br><br>";
+
+               // "Complete moves" PGN (helping in ambiguous cases)
+               counter = 1;
+               i = 0;
+               while (i < this.moves.length)
+               {
+                       pgn += (counter++) + ".";
+                       for (let color of ["w","b"])
+                       {
+                               let move = "";
+                               while (i < this.moves.length && this.moves[i].color == color)
+                                       move += this.moves[i++].notation[1] + ",";
+                               move = move.slice(0,-1); //remove last comma
+                               pgn += move + (i < this.moves.length-1 ? " " : "");
+                       }
+               }
+
+               return pgn;
+       }
 }
 
 const VariantRules = MarseilleRules;