About to finish completed Ultima rules
[vchess.git] / public / javascripts / variants / Ultima.js
index b3679f7..57f3e25 100644 (file)
@@ -50,16 +50,46 @@ class UltimaRules extends ChessRules
        //  - a "bishop" is a chameleon, capturing as its prey
        //  - a "queen" is a withdrawer, capturing by moving away from pieces
 
-       getPotentialMovesFrom([x,y])
+       // Is piece on square (x,y) immobilized?
+       isImmobilized([x,y])
        {
-               // Pre-check: is thing on this square immobilized?
-               // In this case add potential suicide as a move "taking the immobilizer"
+                                       // Final check: is this knight immobilized?
+                                       let foundImmobilizer = false;
+                                       let neutralized = false;
+                                       outerLoop:
+                                       for (let step of steps)
+                                       {
+                                               const [i2,j2] = [i+step[0],j+step[1]];
+                                               if (i2>=0 && i2<sizeX && j2>=0 && j2<sizeY
+                                                       && this.board[i2][j2] != V.EMPTY
+                                                       && this.getColor(i2,j2) == oppCol
+                                                       && this.getPiece(i2,j2) == V.IMMOBILIZER)
+                                               {
+                                                       foundImmobilizer = true;
+                                                       // Moving is possible only if this immobilizer is neutralized
+                                                       for (let step2 of steps)
+                                                       {
+                                                               const [i3,j3] = [i2+step2[0],j2+step2[1]];
+                                                               if (i3>=0 && i3<sizeX && j3>=0 && j3<sizeY
+                                                                       && this.board[i3][j3] != V.EMPTY && this.getColor(i3,j3) == color
+                                                                       && [V.BISHOP,V.IMMOBILIZER].includes(this.getPiece(i3,j3)))
+                                                               {
+                                                                       neutralized = true;
+                                                                       break outerLoop;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                                       if (!foundImmobilizer || neutralized)
+                                               return false;
+               
                const piece = this.getPiece(x,y);
                const color = this.getColor(x,y);
                const oppCol = this.getOppCol(color);
                const V = VariantRules;
                const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
                const [sizeX,sizeY] = V.size;
+               outerLoop:
                for (let step of adjacentSteps)
                {
                        const [i,j] = [x+step[0],y+step[1]];
@@ -67,18 +97,33 @@ class UltimaRules extends ChessRules
                                && this.getColor(i,j) == oppCol)
                        {
                                const oppPiece = this.getPiece(i,j);
-                               if (oppPiece == V.IMMOBILIZER
-                                       || (oppPiece == V.BISHOP && piece == V.IMMOBILIZER))
+                               if (oppPiece == V.BISHOP && piece == V.IMMOBILIZER)
+                                       return [];
+                               if (oppPiece == V.IMMOBILIZER && ![V.BISHOP,V.IMMOBILIZER].includes(piece))
                                {
-                                       return [ new Move({
-                                               appear: [],
-                                               vanish: [new PiPo({x:x,y:y,p:piece,c:color})],
-                                               start: {x:x,y:y},
-                                               end: {x:i,y:j}
-                                       }) ];
+                                       // Moving is impossible only if this immobilizer is not neutralized
+                                       for (let step2 of adjacentSteps)
+                                       {
+                                               const [i2,j2] = [i+step2[0],j+step2[1]];
+                                               if (i2>=0 && i2<sizeX && j2>=0 && j2<sizeY
+                                                       && this.board[i2][j2] != V.EMPTY && this.getColor(i2,j2) == color)
+                                               {
+                                                       const friendlyPiece = this.getPiece(i2,j2);
+                                                       if ([V.BISHOP,V.IMMOBILIZER].includes(friendlyPiece))
+                                                               break outerLoop;
+                                               }
+                                       }
+                                       return []; //immobilizer isn't neutralized
                                }
                        }
                }
+       }
+
+       getPotentialMovesFrom([x,y])
+       {
+               // Pre-check: is thing on this square immobilized?
+               if (this.isImmobilized([x,y]))
+                       return [];
                switch (this.getPiece(x,y))
                {
                        case VariantRules.IMMOBILIZER:
@@ -276,11 +321,11 @@ class UltimaRules extends ChessRules
                return super.getPotentialQueenMoves(sq).concat(this.getKnightCaptures(sq));
        }
 
-       getPotentialBishopMoves(sq)
+       getPotentialBishopMoves([x,y])
        {
-               let moves = super.getPotentialQueenMoves(sq)
-                       .concat(this.getKnightCaptures(sq,"asChameleon"));
-               // NOTE: no "addKingCaptures" because the king isn't captured
+               let moves = super.getPotentialQueenMoves([x,y])
+                       .concat(this.getKnightCaptures([x,y],"asChameleon"));
+               // No "king capture" because king cannot remain under check
                this.addPawnCaptures(moves, "asChameleon");
                this.addRookCaptures(moves, "asChameleon");
                this.addQueenCaptures(moves, "asChameleon");
@@ -367,6 +412,7 @@ class UltimaRules extends ChessRules
 
        // isAttacked() is OK because the immobilizer doesn't take
 
+       // TODO: check if any pawn can reach capturing square + !immobilized
        isAttackedByPawn([x,y], colors)
        {
                // Square (x,y) must be surrounded by two enemy pieces,
@@ -391,6 +437,7 @@ class UltimaRules extends ChessRules
                return false;
        }
 
+       // TODO: check if enemy's rook can reach capturing squares + !immobilized
        isAttackedByRook([x,y], colors)
        {
                const [sizeX,sizeY] = VariantRules.size;
@@ -431,6 +478,7 @@ class UltimaRules extends ChessRules
                const V = VariantRules;
                const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
                const [sizeX,sizeY] = V.size;
+               outerLoop:
                for (let step of steps)
                {
                        const [i0,j0] = [x+step[0],y+step[1]];
@@ -446,7 +494,8 @@ class UltimaRules extends ChessRules
                                if (i>=0 && i<sizeX && j>=0 && j<sizeY && colors.includes(this.getColor(i,j))
                                        && this.getPiece(i,j) == V.KNIGHT)
                                {
-                                       return true;
+                                       if (!this.isImmobilized([i,j]))
+                                               return true;
                                }
                        }
                }
@@ -466,7 +515,7 @@ class UltimaRules extends ChessRules
                        if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j]!=V.EMPTY
                                && colors.includes(this.getColor(i,j)) && this.getPiece(i,j) == V.BISHOP)
                        {
-                               return true;
+                               return true; //bishops are never immobilized
                        }
                }
                return false;
@@ -488,7 +537,8 @@ class UltimaRules extends ChessRules
                                const sq1 = [x+step[0],y+step[1]];
                                if (this.board[sq1[0]][sq1[1]] != V.EMPTY
                                        && colors.includes(this.getColor(sq1[0],sq1[1]))
-                                       && this.getPiece(sq1[0],sq1[1]) == V.QUEEN)
+                                       && this.getPiece(sq1[0],sq1[1]) == V.QUEEN
+                                       && !this.isImmobilized(sq1))
                                {
                                        return true;
                                }
@@ -499,7 +549,7 @@ class UltimaRules extends ChessRules
 
        updateVariables(move)
        {
-               // Just update king position
+               // Just update king(s) position(s)
                const piece = this.getPiece(move.start.x,move.start.y);
                const c = this.getColor(move.start.x,move.start.y);
                if (piece == VariantRules.KING && move.appear.length > 0)
@@ -509,12 +559,6 @@ class UltimaRules extends ChessRules
                }
        }
 
-       checkGameEnd()
-       {
-               // No valid move: game is lost (stalemate is a win)
-               return this.turn == "w" ? "0-1" : "1-0";
-       }
-
        static get VALUES() { //TODO: totally experimental!
                return {
                        'p': 1,
@@ -586,15 +630,4 @@ class UltimaRules extends ChessRules
        {
                return "0000"; //TODO: or "-" ?
        }
-
-       getNotation(move)
-       {
-               if (move.appear.length == 0)
-               {
-                       const startSquare =
-                               String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x);
-                       return "^" + startSquare; //suicide
-               }
-               return super.getNotation(move);
-       }
 }