1 class AtomicRules
extends ChessRules
3 getPotentialMovesFrom([x
,y
])
5 let moves
= super.getPotentialMovesFrom([x
,y
]);
9 if (m
.vanish
.length
> 1 && m
.appear
.length
<= 1) //avoid castles
11 // Explosion! TODO(?): drop moves which explode our king here
12 let steps
= [ [-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1] ];
13 for (let step
of steps
)
15 let x
= m
.end
.x
+ step
[0];
16 let y
= m
.end
.y
+ step
[1];
17 if (x
>=0 && x
<8 && y
>=0 && y
<8 && this.board
[x
][y
] != VariantRules
.EMPTY
18 && this.getPiece(x
,y
) != VariantRules
.PAWN
)
21 new PiPo({p:this.getPiece(x
,y
),c:this.getColor(x
,y
),x:x
,y:y
}));
24 m
.end
= {x:m
.appear
[0].x
, y:m
.appear
[0].y
};
25 m
.appear
.pop(); //Nothin appears in this case
32 getPotentialKingMoves([x
,y
])
34 const V
= VariantRules
;
35 // King cannot capture:
37 let [sizeX
,sizeY
] = V
.size
;
38 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
39 for (let step
of steps
)
43 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
] == VariantRules
.EMPTY
)
44 moves
.push(this.getBasicMove([x
,y
], [i
,j
]));
46 return moves
.concat(this.getCastleMoves([x
,y
]));
49 isAttacked(sq
, colors
)
51 if (this.getPiece(sq
[0],sq
[1]) == VariantRules
.KING
52 && this.isAttackedByKing(sq
, colors
))
54 return false; //king cannot take...
56 return (this.isAttackedByPawn(sq
, colors
)
57 || this.isAttackedByRook(sq
, colors
)
58 || this.isAttackedByKnight(sq
, colors
)
59 || this.isAttackedByBishop(sq
, colors
)
60 || this.isAttackedByQueen(sq
, colors
));
65 super.updateVariables(move);
66 const color
= this.getColor(move.start
.x
,move.start
.y
);
67 if (move.appear
.length
== 0) //capture
69 const firstRank
= {"w": 7, "b": 0};
70 for (let c
of ["w","b"])
72 // Did we explode king of color c ? (TODO: remove move earlier)
73 if (Math
.abs(this.kingPos
[c
][0]-move.end
.x
) <= 1
74 && Math
.abs(this.kingPos
[c
][1]-move.end
.y
) <= 1)
76 this.kingPos
[c
] = [-1,-1];
77 this.castleFlags
[c
] = [false,false];
81 // Now check if init rook(s) exploded
82 if (Math
.abs(move.end
.x
-firstRank
[c
]) <= 1)
84 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[c
][0]) <= 1)
85 this.castleFlags
[c
][0] = false;
86 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[c
][1]) <= 1)
87 this.castleFlags
[c
][1] = false;
94 unupdateVariables(move)
96 super.unupdateVariables(move);
97 const c
= this.getColor(move.start
.x
,move.start
.y
);
98 const oppCol
= this.getOppCol(c
);
99 if ([this.kingPos
[c
][0],this.kingPos
[oppCol
][0]].some(e
=> { return e
< 0; }))
101 // There is a chance that last move blowed some king away..
102 for (let psq
of move.vanish
)
105 this.kingPos
[psq
.c
==c
? c : oppCol
] = [psq
.x
, psq
.y
];
113 const oppCol
= this.getOppCol(c
);
116 // If our king disappeared, move is not valid
117 if (this.kingPos
[c
][0] < 0)
119 // If opponent king disappeared, move is valid
120 else if (this.kingPos
[oppCol
][0] < 0)
122 // Otherwise, if we remain under check, move is not valid
124 res
= this.isAttacked(this.kingPos
[c
], [oppCol
]);
129 getCheckSquares(move)
131 const c
= this.getOppCol(this.turn
);
132 // King might explode:
133 const saveKingPos
= JSON
.parse(JSON
.stringify(this.kingPos
[c
]));
136 if (this.kingPos
[c
][0] < 0)
138 else if (this.isAttacked(this.kingPos
[c
], [this.getOppCol(c
)]))
139 res
= [ JSON
.parse(JSON
.stringify(this.kingPos
[c
])) ]
146 const color
= this.turn
;
147 const kp
= this.kingPos
[color
];
148 if (kp
[0] < 0) //king disappeared
149 return color
== "w" ? "0-1" : "1-0";
150 if (!this.isAttacked(kp
, [this.getOppCol(color
)]))
152 return color
== "w" ? "0-1" : "1-0"; //checkmate