Step toward a one-page application
[vchess.git] / public / javascripts / variants / Marseille.js
index 2eb4260..7d83bd5 100644 (file)
@@ -19,8 +19,6 @@ class MarseilleRules extends ChessRules
 
        getTurnFen()
        {
-               if (this.startAtFirstMove && this.moves.length==0)
-                       return "w";
                return this.turn + this.subTurn;
        }
 
@@ -56,9 +54,8 @@ class MarseilleRules extends ChessRules
                // Extract subTurn from turn indicator: "w" (first move), or
                // "w1" or "w2" white subturn 1 or 2, and same for black
                const fullTurn = V.ParseFen(fen).turn;
-               this.startAtFirstMove = (fullTurn == "w");
                this.turn = fullTurn[0];
-               this.subTurn = (fullTurn[1] || 1);
+               this.subTurn = (fullTurn[1] || 0); //"w0" = special code for first move in game
        }
 
        getPotentialPawnMoves([x,y])
@@ -70,7 +67,6 @@ class MarseilleRules extends ChessRules
                const firstRank = (color == 'w' ? sizeX-1 : 0);
                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];
@@ -81,7 +77,7 @@ class MarseilleRules extends ChessRules
                        for (let piece of finalPieces)
                        {
                                moves.push(this.getBasicMove([x,y], [x+shiftX,y],
-                                       {c:pawnColor,p:piece}));
+                                       {c:color,p:piece}));
                        }
                        // Next condition because pawns on 1st rank can generally jump
                        if ([startRank,firstRank].includes(x)
@@ -101,7 +97,7 @@ class MarseilleRules extends ChessRules
                                for (let piece of finalPieces)
                                {
                                        moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
-                                               {c:pawnColor,p:piece}));
+                                               {c:color,p:piece}));
                                }
                        }
                }
@@ -118,6 +114,7 @@ class MarseilleRules extends ChessRules
                });
                if (epSqs.length == 0)
                        return moves;
+               const oppCol = V.GetOppCol(color);
                for (let sq of epSqs)
                {
                        if (this.subTurn == 1 || (epSqs.length == 2 &&
@@ -125,14 +122,16 @@ class MarseilleRules extends ChessRules
                                // (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)
+                               if (sq.x == x+shiftX && Math.abs(sq.y - y) == 1
+                                       // Add condition "enemy pawn must be present"
+                                       && this.getPiece(x,sq.y) == V.PAWN && this.getColor(x,sq.y) == oppCol)
                                {
                                        let epMove = this.getBasicMove([x,y], [sq.x,sq.y]);
                                        epMove.vanish.push({
                                                x: x,
                                                y: sq.y,
                                                p: 'p',
-                                               c: this.getColor(x,sq.y)
+                                               c: oppCol
                                        });
                                        moves.push(epMove);
                                }
@@ -142,26 +141,21 @@ class MarseilleRules extends ChessRules
                return moves;
        }
 
-       play(move, ingame)
+       play(move)
        {
-               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());
+               move.turn = this.turn + this.subturn;
                V.PlayOnBoard(this.board, move);
                const epSq = this.getEpSquare(move);
-               if (this.startAtFirstMove && this.moves.length == 0)
+               if (this.subTurn == 0) //first move in game
                {
                        this.turn = "b";
                        this.epSquares.push([epSq]);
                }
                // Does this move give check on subturn 1? If yes, skip subturn 2
-               else if (this.subTurn==1 && this.underCheck(this.getOppCol(this.turn)))
+               else if (this.subTurn==1 && this.underCheck(V.GetOppCol(this.turn)))
                {
-                       this.turn = this.getOppCol(this.turn);
+                       this.turn = V.GetOppCol(this.turn);
                        this.epSquares.push([epSq]);
                        move.checkOnSubturn1 = true;
                }
@@ -169,7 +163,7 @@ class MarseilleRules extends ChessRules
                {
                        if (this.subTurn == 2)
                        {
-                               this.turn = this.getOppCol(this.turn);
+                               this.turn = V.GetOppCol(this.turn);
                                let lastEpsq = this.epSquares[this.epSquares.length-1];
                                lastEpsq.push(epSq);
                        }
@@ -177,40 +171,22 @@ class MarseilleRules extends ChessRules
                                this.epSquares.push([epSq]);
                        this.subTurn = 3 - this.subTurn;
                }
-               this.moves.push(move);
                this.updateVariables(move);
-               if (!!ingame)
-                       move.hash = hex_md5(this.getFen());
        }
 
        undo(move)
        {
                this.disaggregateFlags(JSON.parse(move.flags));
                V.UndoOnBoard(this.board, move);
-               if (this.startAtFirstMove && this.moves.length == 1)
-               {
-                       this.turn = "w";
+               if (move.turn[1] == '0' || move.checkOnSubturn1 || this.subTurn == 2)
                        this.epSquares.pop();
-               }
-               else if (move.checkOnSubturn1)
-               {
-                       this.turn = this.getOppCol(this.turn);
-                       this.subTurn = 1;
-                       this.epSquares.pop();
-               }
-               else
+               else //this.subTurn == 1
                {
-                       if (this.subTurn == 1)
-                       {
-                               this.turn = this.getOppCol(this.turn);
-                               let lastEpsq = this.epSquares[this.epSquares.length-1];
-                               lastEpsq.pop();
-                       }
-                       else
-                               this.epSquares.pop();
-                       this.subTurn = 3 - this.subTurn;
+                       let lastEpsq = this.epSquares[this.epSquares.length-1];
+                       lastEpsq.pop();
                }
-               this.moves.pop();
+               this.turn = move.turn[0];
+               this.subTurn = parseInt(move.turn[1]);
                this.unupdateVariables(move);
        }
 
@@ -237,7 +213,7 @@ class MarseilleRules extends ChessRules
 
                const maxeval = V.INFINITY;
                const color = this.turn;
-               const oppCol = this.getOppCol(this.turn);
+               const oppCol = V.GetOppCol(this.turn);
 
                // Search best (half) move for opponent turn
                const getBestMoveEval = () => {
@@ -316,54 +292,4 @@ class MarseilleRules extends ChessRules
                        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;