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 (V
.OnBoard(x
,y
) && this.board
[x
][y
] != V
.EMPTY
18 && this.getPiece(x
,y
) != V
.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 // King cannot capture:
36 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
37 for (let step
of steps
)
39 const i
= x
+ step
[0];
40 const j
= y
+ step
[1];
41 if (V
.OnBoard(i
,j
) && this.board
[i
][j
] == V
.EMPTY
)
42 moves
.push(this.getBasicMove([x
,y
], [i
,j
]));
44 return moves
.concat(this.getCastleMoves([x
,y
]));
47 isAttacked(sq
, colors
)
49 if (this.getPiece(sq
[0],sq
[1]) == V
.KING
&& this.isAttackedByKing(sq
, colors
))
50 return false; //king cannot take...
51 return (this.isAttackedByPawn(sq
, colors
)
52 || this.isAttackedByRook(sq
, colors
)
53 || this.isAttackedByKnight(sq
, colors
)
54 || this.isAttackedByBishop(sq
, colors
)
55 || this.isAttackedByQueen(sq
, colors
));
60 super.updateVariables(move);
61 const color
= move.vanish
[0].c
;
62 if (move.appear
.length
== 0) //capture
64 const firstRank
= {"w": 7, "b": 0};
65 for (let c
of ["w","b"])
67 // Did we explode king of color c ? (TODO: remove move earlier)
68 if (Math
.abs(this.kingPos
[c
][0]-move.end
.x
) <= 1
69 && Math
.abs(this.kingPos
[c
][1]-move.end
.y
) <= 1)
71 this.kingPos
[c
] = [-1,-1];
72 this.castleFlags
[c
] = [false,false];
76 // Now check if init rook(s) exploded
77 if (Math
.abs(move.end
.x
-firstRank
[c
]) <= 1)
79 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[c
][0]) <= 1)
80 this.castleFlags
[c
][0] = false;
81 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[c
][1]) <= 1)
82 this.castleFlags
[c
][1] = false;
89 unupdateVariables(move)
91 super.unupdateVariables(move);
92 const c
= move.vanish
[0].c
;
93 const oppCol
= V
.GetOppCol(c
);
94 if ([this.kingPos
[c
][0],this.kingPos
[oppCol
][0]].some(e
=> { return e
< 0; }))
96 // There is a chance that last move blowed some king away..
97 for (let psq
of move.vanish
)
100 this.kingPos
[psq
.c
==c
? c : oppCol
] = [psq
.x
, psq
.y
];
107 const oppCol
= V
.GetOppCol(color
);
109 // If our king disappeared, move is not valid
110 if (this.kingPos
[color
][0] < 0)
112 // If opponent king disappeared, move is valid
113 else if (this.kingPos
[oppCol
][0] < 0)
115 // Otherwise, if we remain under check, move is not valid
117 res
= this.isAttacked(this.kingPos
[color
], [oppCol
]);
121 getCheckSquares(color
)
124 if (this.kingPos
[color
][0] >= 0 //king might have exploded
125 && this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)]))
127 res
= [ JSON
.parse(JSON
.stringify(this.kingPos
[color
])) ]
134 const color
= this.turn
;
135 const kp
= this.kingPos
[color
];
136 if (kp
[0] < 0) //king disappeared
137 return color
== "w" ? "0-1" : "1-0";
138 if (!this.isAttacked(kp
, [V
.GetOppCol(color
)]))
140 return color
== "w" ? "0-1" : "1-0"; //checkmate