- canMove([x, y], piece) {
- const color = this.getColor(x, y);
- const oppCol = V.GetOppCol(color);
- piece = piece || this.getPiece(x, y);
- if (piece == V.PAWN) {
- const forward = (color == 'w' ? -1 : 1);
- return (
- V.OnBoard(x + forward, y) &&
- (
- this.board[x + forward][y] == V.EMPTY ||
- (
- V.OnBoard(x + forward, y + 1) &&
- this.board[x + forward][y + 1] != V.EMPTY &&
- this.getColor[x + forward, y + 1] == oppCol
- ) ||
- (
- V.OnBoard(x + forward, y - 1) &&
- this.board[x + forward][y - 1] != V.EMPTY &&
- this.getColor[x + forward, y - 1] == oppCol
- )
- )
- );
- }
- // Checking one step is enough:
- const steps =
- [V.KING, V.QUEEN].includes(piece)
- ? V.steps[V.ROOK].concat(V.steps[V.BISHOP])
- : V.steps[piece];
- for (let step of steps) {
- const [i, j] = [x + step[0], y + step[1]];
- if (
- V.OnBoard(i, j) &&
- (this.board[i][j] == V.EMPTY || this.getColor(i, j) != color)
- ) {
- return true;
- }
- }
- return false;
- }
-