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
)
20 m
.vanish
.push(new PiPo({p:this.getPiece(x
,y
),c:this.getColor(x
,y
),x:x
,y:y
}));
23 m
.end
= {x:m
.appear
[0].x
, y:m
.appear
[0].y
};
24 m
.appear
.pop(); //Nothin appears in this case
31 getPotentialKingMoves([x
,y
])
33 const V
= VariantRules
;
34 // King cannot capture:
36 let [sizeX
,sizeY
] = V
.size
;
37 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
38 for (let step
of steps
)
42 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
] == VariantRules
.EMPTY
)
43 moves
.push(this.getBasicMove([x
,y
], [i
,j
]));
45 return moves
.concat(this.getCastleMoves([x
,y
]));
48 isAttacked(sq
, colors
)
50 if (this.getPiece(sq
[0],sq
[1]) == VariantRules
.KING
&& this.isAttackedByKing(sq
, colors
))
51 return false; //king cannot take...
52 return (this.isAttackedByPawn(sq
, colors
)
53 || this.isAttackedByRook(sq
, colors
)
54 || this.isAttackedByKnight(sq
, colors
)
55 || this.isAttackedByBishop(sq
, colors
)
56 || this.isAttackedByQueen(sq
, colors
));
61 super.updateVariables(move);
62 const color
= this.getColor(move.start
.x
,move.start
.y
);
63 // Next condition to avoid conflicts with harmless castle moves
64 if (color
!= this.getColor(move.end
.x
,move.end
.y
)
65 && this.board
[move.end
.x
][move.end
.y
] != VariantRules
.EMPTY
)
67 const firstRank
= {"w": 7, "b": 0};
68 for (let c
of ["w","b"])
70 // Did we explode king of color c ? (TODO: remove move earlier)
71 if (Math
.abs(this.kingPos
[c
][0]-move.end
.x
) <= 1
72 && Math
.abs(this.kingPos
[c
][1]-move.end
.y
) <= 1)
74 this.kingPos
[c
] = [-1,-1];
75 this.castleFlags
[c
] = [false,false];
79 // Now check if init rook(s) exploded
80 if (Math
.abs(move.end
.x
-firstRank
[c
]) <= 1)
82 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[c
][0]) <= 1)
83 this.castleFlags
[c
][0] = false;
84 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[c
][1]) <= 1)
85 this.castleFlags
[c
][1] = false;
92 unupdateVariables(move)
94 super.unupdateVariables(move);
95 const c
= this.getColor(move.start
.x
,move.start
.y
);
96 const oppCol
= this.getOppCol(c
);
97 if ([this.kingPos
[c
][0],this.kingPos
[oppCol
][0]].some(e
=> { return e
< 0; }))
99 // There is a chance that last move blowed some king away..
100 for (let psq
of move.vanish
)
103 this.kingPos
[psq
.c
==c
? c : oppCol
] = [psq
.x
, psq
.y
];
111 const oppCol
= this.getOppCol(c
);
114 // If our king disappeared, move is not valid
115 if (this.kingPos
[c
][0] < 0)
117 // If opponent king disappeared, move is valid
118 else if (this.kingPos
[oppCol
][0] < 0)
120 // Otherwise, if we remain under check, move is not valid
122 res
= this.isAttacked(this.kingPos
[c
], [oppCol
]);
127 getCheckSquares(move)
129 const c
= this.getOppCol(this.turn
);
130 // King might explode:
131 const saveKingPos
= JSON
.parse(JSON
.stringify(this.kingPos
[c
]));
134 if (this.kingPos
[c
][0] < 0)
136 else if (this.isAttacked(this.kingPos
[c
], [this.getOppCol(c
)]))
137 res
= [ JSON
.parse(JSON
.stringify(this.kingPos
[c
])) ]
144 const color
= this.turn
;
145 const kp
= this.kingPos
[color
];
146 if (kp
[0] < 0) //king disappeared
147 return color
== "w" ? "0-1" : "1-0";
148 if (!this.isAttacked(kp
, [this.getOppCol(color
)]))
151 return color
== "w" ? "0-1" : "1-0";