Fix Alice rules (en passant)
[vchess.git] / public / javascripts / variants / Alice.js
index d457f5f..dd023bb 100644 (file)
@@ -95,8 +95,11 @@ class AliceRules extends ChessRules
                let moves = super.getPotentialMovesFrom([x,y]);
                this.board = saveBoard;
 
+               const pieces = Object.keys(VariantRules.ALICE_CODES);
+               const codes = Object.keys(VariantRules.ALICE_PIECES);
+
                // Finally filter impossible moves
-               const mirrorSide = (Object.keys(VariantRules.ALICE_CODES).includes(this.getPiece(x,y)) ? 1 : 2);
+               const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2);
                return moves.filter(m => {
                        if (m.appear.length == 2) //castle
                        {
@@ -113,8 +116,8 @@ class AliceRules extends ChessRules
                        {
                                // Attempt to capture
                                const piece = this.getPiece(m.end.x,m.end.y);
-                               if ((mirrorSide==1 && Object.keys(VariantRules.ALICE_PIECES).includes(piece))
-                                       || (mirrorSide==2 && Object.keys(VariantRules.ALICE_CODES).includes(piece)))
+                               if ((mirrorSide==1 && codes.includes(piece))
+                                       || (mirrorSide==2 && pieces.includes(piece)))
                                {
                                        return false;
                                }
@@ -132,6 +135,19 @@ class AliceRules extends ChessRules
                                        psq.p = VariantRules.ALICE_CODES[psq.p];
                                });
                        }
+                       // Fix en-passant captures
+                       if (m.vanish.length == 2 && this.board[m.end.x][m.end.y] == VariantRules.EMPTY)
+                       {
+                               m.vanish[1].c = this.getOppCol(this.getColor(x,y));
+                               // In the special case of en-passant, if
+                               //  - board1 takes board2 : vanish[1] --> Alice
+                               //  - board2 takes board1 : vanish[1] --> normal
+                               let van = m.vanish[1];
+                               if (mirrorSide==1 && codes.includes(this.getPiece(van.x,van.y)))
+                                       van.p = VariantRules.ALICE_CODES[van.p];
+                               else if (mirrorSide==2 && pieces.includes(this.getPiece(van.x,van.y)))
+                                       van.p = VariantRules.ALICE_PIECES[van.p];
+                       }
                        return true;
                });
        }
@@ -186,6 +202,38 @@ class AliceRules extends ChessRules
                        this.kingPos[c] = [move.start.x, move.start.y];
        }
 
+       checkGameEnd()
+       {
+               const color = this.turn;
+               let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
+               let saveBoard = this.board;
+               this.board = sideBoard;
+               let res = "*";
+               if (!this.isAttacked(this.kingPos[color], this.getOppCol(color)))
+                       res = "1/2";
+               else
+                       res = (color == "w" ? "0-1" : "1-0");
+               this.board = saveBoard;
+               return res;
+       }
+
+       static get VALUES() {
+               return {
+                       'p': 1,
+                       's': 1,
+                       'r': 5,
+                       'u': 5,
+                       'n': 3,
+                       'o': 3,
+                       'b': 3,
+                       'c': 3,
+                       'q': 9,
+                       't': 9,
+                       'k': 1000,
+                       'l': 1000
+               };
+       }
+
        getNotation(move)
        {
                if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING)
@@ -200,9 +248,13 @@ class AliceRules extends ChessRules
                        String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
                const piece = this.getPiece(move.start.x, move.start.y);
 
+               const captureMark = (move.vanish.length > move.appear.length ? "x" : "");
+               let pawnMark = "";
+               if (["p","s"].includes(piece) && captureMark.length == 1)
+                       pawnMark = String.fromCharCode(97 + move.start.y); //start column
+
                // Piece or pawn movement
-               let notation = piece.toUpperCase() +
-                       (move.vanish.length > move.appear.length ? "x" : "") + finalSquare;
+               let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare;
                if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p))
                {
                        // Promotion
@@ -210,19 +262,4 @@ class AliceRules extends ChessRules
                }
                return notation;
        }
-
-       checkGameEnd()
-       {
-               const color = this.turn;
-               let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
-               let saveBoard = this.board;
-               this.board = sideBoard;
-               let res = "*";
-               if (!this.isAttacked(this.kingPos[color], this.getOppCol(color)))
-                       res = "1/2";
-               else
-                       res = (color == "w" ? "0-1" : "1-0");
-               this.board = saveBoard;
-               return res;
-       }
 }