Fix bugs in Ultima
[vchess.git] / public / javascripts / variants / Ultima.js
index d5b9f88..6883c6f 100644 (file)
@@ -189,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)
@@ -445,6 +445,7 @@ class UltimaRules extends ChessRules
                                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]))
@@ -479,16 +480,25 @@ class UltimaRules extends ChessRules
                        {
                                // Try in opposite direction:
                                let [i,j] = [x-step[0],y-step[1]];
-                               while (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] == V.EMPTY)
+                               while (i>=0 && i<sizeX && j>=0 && j<sizeY)
                                {
-                                       i -= step[0];
-                                       j -= step[1];
-                               }
-                               if (i>=0 && i<sizeX && j>=0 && j<sizeY && colors.includes(this.getColor(i,j))
-                                       && this.getPiece(i,j) == V.KNIGHT)
-                               {
-                                       if (!this.isImmobilized([i,j]))
-                                               return true;
+                                       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
+                                               i -= step[0];
+                                               j -= step[1];
+                                       }
                                }
                        }
                }
@@ -623,4 +633,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;
+       }
 }