Knight rules
This commit is contained in:
parent
076adb0291
commit
9db0c04eee
|
|
@ -0,0 +1,41 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Knight extends Piece {
|
||||
public Knight(boolean isWhite, int x, int y) {
|
||||
super(isWhite, PieceType.Knight, x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PieceType getType() {
|
||||
return PieceType.Knight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Move> getLegalMoves(Board board, int row, int col) {
|
||||
List<Move> moves = new ArrayList<>();
|
||||
int[][] offsets = {
|
||||
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
|
||||
{1, -2}, {1, 2}, {2, -1}, {2, 1}
|
||||
};
|
||||
|
||||
for (int[] offset : offsets) {
|
||||
int r = row + offset[0];
|
||||
int c = col + offset[1];
|
||||
if (board.isInBounds(r, c)) {
|
||||
Piece p = board.getPieceAt(r, c);
|
||||
if (p == null || p.isWhite() != this.isWhite)
|
||||
moves.add(new Move(this, row, col, r, c));
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Piece clone() {
|
||||
return new Knight(this.isWhite, this.x, this.y);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue