Some fixes. Loser implemented. Draft Extinction,Crazyhouse,Switching
[vchess.git] / public / javascripts / variants / Antiking.js
index 2770261..f908495 100644 (file)
@@ -15,23 +15,23 @@ class AntikingRules extends ChessRules
                const position = fen.split(" ")[0].split("/");
                for (let i=0; i<position.length; i++)
                {
-                       let j = 0;
-                       while (j < position[i].length)
+                       let k = 0;
+                       for (let j=0; j<position[i].length; j++)
                        {
                                switch (position[i].charAt(j))
                                {
                                        case 'a':
-                                               this.antikingPos['b'] = [i,j];
+                                               this.antikingPos['b'] = [i,k];
                                                break;
                                        case 'A':
-                                               this.antikingPos['w'] = [i,j];
+                                               this.antikingPos['w'] = [i,k];
                                                break;
                                        default:
                                                let num = parseInt(position[i].charAt(j));
                                                if (!isNaN(num))
-                                                       j += (num-1);
+                                                       k += (num-1);
                                }
-                               j++;
+                               k++;
                        }
                }
        }
@@ -42,7 +42,7 @@ class AntikingRules extends ChessRules
                const piece2 = this.getPiece(x2,y2);
                const color1 = this.getColor(x1,y1);
                const color2 = this.getColor(x2,y2);
-               return !["a","A"].includes(piece2) &&
+               return piece2 != "a" &&
                        ((piece1 != "a" && color1 != color2) || (piece1 == "a" && color1 == color2));
        }
 
@@ -59,7 +59,9 @@ class AntikingRules extends ChessRules
 
        getPotentialAntikingMoves(sq)
        {
-               return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.QUEEN], "oneStep");
+               const V = VariantRules;
+               return this.getSlideNJumpMoves(sq,
+                       V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
        }
 
        isAttacked(sq, colors)
@@ -67,13 +69,22 @@ class AntikingRules extends ChessRules
                return (super.isAttacked(sq, colors) || this.isAttackedByAntiking(sq, colors));
        }
 
+       isAttackedByKing([x,y], colors)
+       {
+               const V = VariantRules;
+               if (this.getPiece(x,y) == V.ANTIKING)
+                       return false; //antiking is not attacked by king
+               return this.isAttackedBySlideNJump([x,y], colors, V.KING,
+                       V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
+       }
+
        isAttackedByAntiking([x,y], colors)
        {
-               console.log(x + " " + y); //TODO: debug -1, -1 (wrong undo ?!)
-               if (this.getPiece(x,y) == VariantRules.KING)
+               const V = VariantRules;
+               if (this.getPiece(x,y) == V.KING)
                        return false; //king is not attacked by antiking
-               return super.isAttackedBySlideNJump([x,y], colors,
-                       VariantRules.ANTIKING, VariantRules.steps[VariantRules.QUEEN], "oneStep");
+               return this.isAttackedBySlideNJump([x,y], colors, V.ANTIKING,
+                       V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
        }
 
        underCheck(move)
@@ -81,8 +92,8 @@ class AntikingRules extends ChessRules
                const c = this.turn;
                const oppCol = this.getOppCol(c);
                this.play(move)
-               let res = this.isAttacked(this.kingPos[c], oppCol)
-                       || !this.isAttacked(this.antikingPos[c], oppCol);
+               let res = this.isAttacked(this.kingPos[c], [oppCol])
+                       || !this.isAttacked(this.antikingPos[c], [oppCol]);
                this.undo(move);
                return res;
        }
@@ -92,7 +103,7 @@ class AntikingRules extends ChessRules
                let res = super.getCheckSquares(move);
                this.play(move);
                const c = this.turn;
-               if (!this.isAttacked(this.antikingPos[c], this.getOppCol(c)))
+               if (!this.isAttacked(this.antikingPos[c], [this.getOppCol(c)]))
                        res.push(JSON.parse(JSON.stringify(this.antikingPos[c])));
                this.undo(move);
                return res;
@@ -123,8 +134,8 @@ class AntikingRules extends ChessRules
        {
                const color = this.turn;
                const oppCol = this.getOppCol(color);
-               if (!this.isAttacked(this.kingPos[color], oppCol)
-                       && this.isAttacked(this.antikingPos[color], oppCol))
+               if (!this.isAttacked(this.kingPos[color], [oppCol])
+                       && this.isAttacked(this.antikingPos[color], [oppCol]))
                {
                        return "1/2";
                }