Update translations and TODO
[vchess.git] / public / javascripts / variants / Marseille.js
index 618e833..2eb4260 100644 (file)
@@ -43,15 +43,13 @@ class MarseilleRules extends ChessRules
                const parsedFen = V.ParseFen(fen);
                this.setFlags(parsedFen.flags);
                if (parsedFen.enpassant == "-")
-                       this.epSquares = [ [undefined,undefined] ];
+                       this.epSquares = [ [undefined] ];
                else
                {
                        let res = [];
                        const squares = parsedFen.enpassant.split(",");
                        for (let sq of squares)
                                res.push(V.SquareToCoords(sq));
-                       if (res.length == 1)
-                               res.push(undefined); //always 2 slots in epSquares[i]
                        this.epSquares = [ res ];
                }
                this.scanKingsRooks(fen);
@@ -153,32 +151,30 @@ class MarseilleRules extends ChessRules
                        move.color = this.turn;
                }
                move.flags = JSON.stringify(this.aggregateFlags());
-               let lastEpsq = this.epSquares[this.epSquares.length-1];
-               const epSq = this.getEpSquare(move);
-               if (lastEpsq.length == 1)
-                       lastEpsq.push(epSq);
-               else
-               {
-                       // New turn
-                       let newEpsqs = [epSq];
-                       if (this.startAtFirstMove && this.moves.length == 0)
-                               newEpsqs.push(undefined); //at first move, to force length==2 (TODO)
-                       this.epSquares.push(newEpsqs);
-               }
                V.PlayOnBoard(this.board, move);
+               const epSq = this.getEpSquare(move);
                if (this.startAtFirstMove && this.moves.length == 0)
+               {
                        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)))
                {
-                       this.epSquares[this.epSquares.length-1].push(undefined);
                        this.turn = this.getOppCol(this.turn);
+                       this.epSquares.push([epSq]);
                        move.checkOnSubturn1 = true;
                }
                else
                {
                        if (this.subTurn == 2)
+                       {
                                this.turn = this.getOppCol(this.turn);
+                               let lastEpsq = this.epSquares[this.epSquares.length-1];
+                               lastEpsq.push(epSq);
+                       }
+                       else
+                               this.epSquares.push([epSq]);
                        this.subTurn = 3 - this.subTurn;
                }
                this.moves.push(move);
@@ -190,31 +186,28 @@ class MarseilleRules extends ChessRules
        undo(move)
        {
                this.disaggregateFlags(JSON.parse(move.flags));
-               let lastEpsq = this.epSquares[this.epSquares.length-1];
-               if (lastEpsq.length == 2)
-               {
-                       if (!!move.checkOnSubturn1 ||
-                               (this.startAtFirstMove && this.moves.length == 1))
-                       {
-                               this.epSquares.pop(); //remove real + artificial e.p. squares
-                       }
-                       else
-                               lastEpsq.pop();
-               }
-               else
-                       this.epSquares.pop();
                V.UndoOnBoard(this.board, move);
                if (this.startAtFirstMove && this.moves.length == 1)
+               {
                        this.turn = "w";
+                       this.epSquares.pop();
+               }
                else if (move.checkOnSubturn1)
                {
                        this.turn = this.getOppCol(this.turn);
                        this.subTurn = 1;
+                       this.epSquares.pop();
                }
                else
                {
                        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;
                }
                this.moves.pop();
@@ -224,6 +217,18 @@ class MarseilleRules extends ChessRules
        // NOTE:  GenRandInitFen() is OK,
        // since at first move turn indicator is just "w"
 
+       static get VALUES()
+       {
+               return {
+                       'p': 1,
+                       'r': 5,
+                       'n': 3,
+                       'b': 3,
+                       'q': 7, //slightly less than in orthodox game
+                       'k': 1000
+               };
+       }
+
        // No alpha-beta here, just adapted min-max at depth 2(+1)
        getComputerMove()
        {
@@ -236,6 +241,7 @@ class MarseilleRules extends ChessRules
 
                // Search best (half) move for opponent turn
                const getBestMoveEval = () => {
+                       const turnBefore = this.turn + this.subTurn;
                        let moves = this.getAllValidMoves();
                        if (moves.length == 0)
                        {
@@ -248,7 +254,8 @@ class MarseilleRules extends ChessRules
                        for (let m of moves)
                        {
                                this.play(m);
-                               this.turn = color; //very artificial...
+                               // Now turn is oppCol,2 if m doesn't give check
+                               // Otherwise it's color,1. In both cases the next test makes sense
                                if (!this.atLeastOneMove())
                                {
                                        const score = this.checkGameEnd();
@@ -257,14 +264,12 @@ class MarseilleRules extends ChessRules
                                        else
                                        {
                                                // Found a mate
-                                               this.turn = oppCol;
                                                this.undo(m);
                                                return maxeval * (score == "1-0" ? 1 : -1);
                                        }
                                }
                                const evalPos = this.evalPosition();
                                res = (oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos));
-                               this.turn = oppCol;
                                this.undo(m);
                        }
                        return res;
@@ -275,7 +280,6 @@ class MarseilleRules extends ChessRules
                // 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)
                        {