Fix two bugs in Ultima isAttackedBy...
[vchess.git] / public / javascripts / variants / Ultima.js
index c98822d..ac0cf95 100644 (file)
@@ -50,9 +50,9 @@ 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?
                const piece = this.getPiece(x,y);
                const color = this.getColor(x,y);
                const oppCol = this.getOppCol(color);
@@ -68,7 +68,7 @@ class UltimaRules extends ChessRules
                        {
                                const oppPiece = this.getPiece(i,j);
                                if (oppPiece == V.BISHOP && piece == V.IMMOBILIZER)
-                                       return [];
+                                       return true;
                                if (oppPiece == V.IMMOBILIZER && ![V.BISHOP,V.IMMOBILIZER].includes(piece))
                                {
                                        // Moving is impossible only if this immobilizer is not neutralized
@@ -78,15 +78,22 @@ class UltimaRules extends ChessRules
                                                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;
+                                                       if ([V.BISHOP,V.IMMOBILIZER].includes(this.getPiece(i2,j2)))
+                                                               return false;
                                                }
                                        }
-                                       return []; //immobilizer isn't neutralized
+                                       return true; //immobilizer isn't neutralized
                                }
                        }
                }
+               return false;
+       }
+
+       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:
@@ -182,8 +189,8 @@ class UltimaRules extends ChessRules
                        // Check piece-king rectangle (if any) corners for enemy pieces
                        if (m.end.x == kp[0] || m.end.y == kp[1])
                                return; //"flat rectangle"
-                       const corner1 = [Math.max(m.end.x,kp[0]), Math.min(m.end.y,kp[1])];
-                       const corner2 = [Math.min(m.end.x,kp[0]), Math.max(m.end.y,kp[1])];
+                       const corner1 = [m.end.x, kp[1]];
+                       const corner2 = [kp[0], m.end.y];
                        for (let [i,j] of [corner1,corner2])
                        {
                                if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) == oppCol)
@@ -288,13 +295,10 @@ class UltimaRules extends ChessRules
        {
                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");
-               // 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;
@@ -376,77 +380,188 @@ class UltimaRules extends ChessRules
                        V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
        }
 
-       atLeastOneMove()
-       {
-               if (this.kingPos[this.turn][0] < 0)
-                       return false;
-               return super.atLeastOneMove();
-       }
+       // isAttacked() is OK because the immobilizer doesn't take
 
-       underCheck(move)
+       isAttackedByPawn([x,y], colors)
        {
-               return false; //there is no check
+               // Square (x,y) must be surroundable by two enemy pieces,
+               // and one of them at least should be a pawn (moving).
+               const dirs = [ [1,0],[0,1] ];
+               const steps = VariantRules.steps[VariantRules.ROOK];
+               const [sizeX,sizeY] = VariantRules.size;
+               for (let dir of dirs)
+               {
+                       const [i1,j1] = [x-dir[0],y-dir[1]]; //"before"
+                       const [i2,j2] = [x+dir[0],y+dir[1]]; //"after"
+                       if (i1>=0 && i1<sizeX && i2>=0 && i2<sizeX
+                               && j1>=0 && j1<sizeY && j2>=0 && j2<sizeY)
+                       {
+                               if ((this.board[i1][j1]!=VariantRules.EMPTY
+                                       && colors.includes(this.getColor(i1,j1))
+                                       && this.board[i2][j2]==VariantRules.EMPTY)
+                                               ||
+                                       (this.board[i2][j2]!=VariantRules.EMPTY
+                                       && colors.includes(this.getColor(i2,j2))
+                                       && this.board[i1][j1]==VariantRules.EMPTY))
+                               {
+                                       // Search a movable enemy pawn landing on the empty square
+                                       for (let step of steps)
+                                       {
+                                               let [ii,jj] = (this.board[i1][j1]==VariantRules.EMPTY ? [i1,j1] : [i2,j2]);
+                                               let [i3,j3] = [ii+step[0],jj+step[1]];
+                                               while (i3>=0 && i3<sizeX && j3>=0 && j3<sizeY
+                                                       && this.board[i3][j3]==VariantRules.EMPTY)
+                                               {
+                                                       i3 += step[0];
+                                                       j3 += step[1];
+                                               }
+                                               if (i3>=0 && i3<sizeX && j3>=0 && j3<sizeY
+                                                       && colors.includes(this.getColor(i3,j3))
+                                                       && this.getPiece(i3,j3) == VariantRules.PAWN
+                                                       && !this.isImmobilized([i3,j3]))
+                                               {
+                                                       return true;
+                                               }
+                                       }
+                               }
+                       }
+               }
+               return false;
        }
 
-       getCheckSquares(move)
+       isAttackedByRook([x,y], colors)
        {
-               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;
+               // King must be on same column or row,
+               // and a rook should be able to reach a capturing square
+               const [sizeX,sizeY] = VariantRules.size;
+               // colors contains only one element, giving the oppCol and thus king position
+               const sameRow = (x == this.kingPos[colors[0]][0]);
+               const sameColumn = (y == this.kingPos[colors[0]][1]);
+               if (sameRow || sameColumn)
+               {
+                       // Look for the enemy rook (maximum 1)
+                       for (let i=0; i<sizeX; i++)
+                       {
+                               for (let j=0; j<sizeY; j++)
+                               {
+                                       if (this.board[i][j] != VariantRules.EMPTY
+                                               && colors.includes(this.getColor(i,j))
+                                               && this.getPiece(i,j) == VariantRules.ROOK)
+                                       {
+                                               if (this.isImmobilized([i,j]))
+                                                       return false; //because only one rook
+                                               // Can it reach a capturing square?
+                                               // Easy but quite suboptimal way (TODO): generate all moves (turn is OK)
+                                               const moves = this.getPotentialMovesFrom([i,j]);
+                                               for (let move of moves)
+                                               {
+                                                       if (sameRow && move.end.y == y || sameColumn && move.end.x == x)
+                                                               return true;
+                                               }
+                                       }
+                               }
+                       }
+               }
+               return false;
        }
 
-       updateVariables(move)
+       isAttackedByKnight([x,y], colors)
        {
-               // 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)
+               // Square (x,y) must be on same line as a knight,
+               // and there must be empty square(s) behind.
+               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)
                {
-                       this.kingPos[c][0] = move.appear[0].x;
-                       this.kingPos[c][1] = move.appear[0].y;
+                       const [i0,j0] = [x+step[0],y+step[1]];
+                       if (i0>=0 && i0<sizeX && j0>=0 && j0<sizeY && this.board[i0][j0] == V.EMPTY)
+                       {
+                               // Try in opposite direction:
+                               let [i,j] = [x-step[0],y-step[1]];
+                               while (i>=0 && i<sizeX && j>=0 && j<sizeY)
+                               {
+                                       while (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] == V.EMPTY)
+                                       {
+                                               i -= step[0];
+                                               j -= step[1];
+                                       }
+                                       if (i>=0 && i<sizeX && j>=0 && j<sizeY)
+                                       {
+                                               if (colors.includes(this.getColor(i,j)))
+                                               {
+                                                       if (this.getPiece(i,j) == V.KNIGHT && !this.isImmobilized([i,j]))
+                                                               return true;
+                                                       continue outerLoop;
+                                               }
+                                               // [else] Our color, could be captured *if there was an empty space*
+                                               if (this.board[i+step[0]][j+step[1]] != V.EMPTY)
+                                                       continue outerLoop;
+                                               i -= step[0];
+                                               j -= step[1];
+                                       }
+                               }
+                       }
                }
-               // Does this move takes opponent's king?
-               const oppCol = this.getOppCol(c);
-               for (let i=1; i<move.vanish.length; i++)
+               return false;
+       }
+
+       isAttackedByBishop([x,y], colors)
+       {
+               // We cheat a little here: since this function is used exclusively for king,
+               // it's enough to check the immediate surrounding of the square.
+               const V = VariantRules;
+               const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
+               const [sizeX,sizeY] = V.size;
+               for (let step of adjacentSteps)
                {
-                       if (move.vanish[i].p == VariantRules.KING)
+                       const [i,j] = [x+step[0],y+step[1]];
+                       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)
                        {
-                               this.kingPos[oppCol] = [-1,-1];
-                               break;
+                               return true; //bishops are never immobilized
                        }
                }
+               return false;
        }
 
-       unupdateVariables(move)
+       isAttackedByQueen([x,y], colors)
        {
-               super.unupdateVariables(move);
-               const c = this.getColor(move.start.x,move.start.y);
-               const oppCol = this.getOppCol(c);
-               if (this.kingPos[oppCol][0] < 0)
+               // Square (x,y) must be adjacent to a queen, and the queen must have
+               // some free space in the opposite direction from (x,y)
+               const V = VariantRules;
+               const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
+               const [sizeX,sizeY] = V.size;
+               for (let step of adjacentSteps)
                {
-                       // Last move took opponent's king
-                       for (let i=1; i<move.vanish.length; i++)
+                       const sq2 = [x+2*step[0],y+2*step[1]];
+                       if (sq2[0]>=0 && sq2[0]<sizeX && sq2[1]>=0 && sq2[1]<sizeY
+                               && this.board[sq2[0]][sq2[1]] == V.EMPTY)
                        {
-                               const psq = move.vanish[i];
-                               if (psq.p == 'k')
+                               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.isImmobilized(sq1))
                                {
-                                       this.kingPos[oppCol] = [psq.x, psq.y];
-                                       break;
+                                       return true;
                                }
                        }
                }
+               return false;
        }
 
-       checkGameEnd()
+       updateVariables(move)
        {
-               // Stalemate, or our king disappeared
-               return this.turn == "w" ? "0-1" : "1-0";
+               // 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)
+               {
+                       this.kingPos[c][0] = move.appear[0].x;
+                       this.kingPos[c][1] = move.appear[0].y;
+               }
        }
 
        static get VALUES() { //TODO: totally experimental!
@@ -463,10 +578,6 @@ 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) };
@@ -524,4 +635,25 @@ class UltimaRules extends ChessRules
        {
                return "0000"; //TODO: or "-" ?
        }
+
+       getNotation(move)
+       {
+               const initialSquare =
+                       String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x);
+               const finalSquare =
+                       String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
+               let notation = undefined;
+               if (move.appear[0].p == VariantRules.PAWN)
+               {
+                       // Pawn: generally ambiguous short notation, so we use full description
+                       notation = "P" + initialSquare + finalSquare;
+               }
+               else if (move.appear[0].p == VariantRules.KING)
+                       notation = "K" + (move.vanish.length>1 ? "x" : "") + finalSquare;
+               else
+                       notation = move.appear[0].p.toUpperCase() + finalSquare;
+               if (move.vanish.length > 1 && move.appear[0].p != VariantRules.KING)
+                       notation += "X"; //capture mark (not describing what is captured...)
+               return notation;
+       }
 }