Slight change in Ultima rules. TODO: merge with removed code 'isAttacked'
[vchess.git] / public / javascripts / variants / Ultima.js
index d667643..c98822d 100644 (file)
@@ -53,13 +53,13 @@ class UltimaRules extends ChessRules
        getPotentialMovesFrom([x,y])
        {
                // Pre-check: is thing on this square immobilized?
-               // In this case add potential suicide as a move "taking the immobilizer"
                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,15 +67,23 @@ 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
                                }
                        }
                }
@@ -276,14 +284,17 @@ 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"));
                this.addPawnCaptures(moves, "asChameleon");
                this.addRookCaptures(moves, "asChameleon");
                this.addQueenCaptures(moves, "asChameleon");
+               // Add king capture if it's within range
+               const oppKp = this.kingPos[this.getOppCol(this.turn)];
+               if (Math.abs(x-oppKp[0]) <= 1 && Math.abs(y-oppKp[1]) <= 1)
+                       moves.push(this.getBasicMove([x,y],oppKp));
                // Post-processing: merge similar moves, concatenating vanish arrays
                let mergedMoves = {};
                const [sizeX,sizeY] = VariantRules.size;
@@ -365,46 +376,34 @@ class UltimaRules extends ChessRules
                        V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
        }
 
-       // isAttacked() is OK because the immobilizer doesn't take
-
-       isAttackedByPawn([x,y], colors)
+       atLeastOneMove()
        {
-               // Square (x,y) must be surrounded by two enemy pieces,
-               // and one of them at least should be a pawn
-               return false;
+               if (this.kingPos[this.turn][0] < 0)
+                       return false;
+               return super.atLeastOneMove();
        }
 
-       isAttackedByRook(sq, colors)
+       underCheck(move)
        {
-               // Enemy king must be on same file and a rook on same row (or reverse)
-               return false;
+               return false; //there is no check
        }
 
-       isAttackedByKnight(sq, colors)
+       getCheckSquares(move)
        {
-               // Square (x,y) must be on same line as a knight,
-               // and there must be empty square(s) behind.
-               return false;
-       }
-
-       isAttackedByBishop(sq, colors)
-       {
-               // switch on piece nature on square sq: a chameleon attack as this piece
-               // ==> call the appropriate isAttackedBy... (exception of immobilizers)
-               // Other exception: a chameleon cannot attack a chameleon (seemingly...)
-               return false;
-       }
-
-       isAttackedByQueen(sq, colors)
-       {
-               // Square (x,y) must be adjacent to a queen, and the queen must have
-               // some free space in the opposite direction from (x,y)
-               return false;
+               const c = this.getOppCol(this.turn); //opponent
+               const saveKingPos = this.kingPos[c]; //king might be taken
+               this.play(move);
+               // The only way to be "under check" is to have lost the king (thus game over)
+               let res = this.kingPos[c][0] < 0
+                       ? [ JSON.parse(JSON.stringify(saveKingPos)) ]
+                       : [ ];
+               this.undo(move);
+               return res;
        }
 
        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)
@@ -412,11 +411,41 @@ class UltimaRules extends ChessRules
                        this.kingPos[c][0] = move.appear[0].x;
                        this.kingPos[c][1] = move.appear[0].y;
                }
+               // Does this move takes opponent's king?
+               const oppCol = this.getOppCol(c);
+               for (let i=1; i<move.vanish.length; i++)
+               {
+                       if (move.vanish[i].p == VariantRules.KING)
+                       {
+                               this.kingPos[oppCol] = [-1,-1];
+                               break;
+                       }
+               }
+       }
+
+       unupdateVariables(move)
+       {
+               super.unupdateVariables(move);
+               const c = this.getColor(move.start.x,move.start.y);
+               const oppCol = this.getOppCol(c);
+               if (this.kingPos[oppCol][0] < 0)
+               {
+                       // Last move took opponent's king
+                       for (let i=1; i<move.vanish.length; i++)
+                       {
+                               const psq = move.vanish[i];
+                               if (psq.p == 'k')
+                               {
+                                       this.kingPos[oppCol] = [psq.x, psq.y];
+                                       break;
+                               }
+                       }
+               }
        }
 
        checkGameEnd()
        {
-               // No valid move: game is lost (stalemate is a win)
+               // Stalemate, or our king disappeared
                return this.turn == "w" ? "0-1" : "1-0";
        }
 
@@ -434,6 +463,10 @@ class UltimaRules extends ChessRules
 
        static get SEARCH_DEPTH() { return 2; } //TODO?
 
+       static get THRESHOLD_MATE() {
+               return 500; //checkmates evals may be slightly below 1000
+       }
+
        static GenRandInitFen()
        {
                let pieces = { "w": new Array(8), "b": new Array(8) };
@@ -491,15 +524,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);
-       }
 }