Rook rules

This commit is contained in:
Tilman Crosetti 2025-05-13 14:52:08 +02:00
parent 6fb087cbc0
commit 8e3c7d9ada
1 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package backend;
import java.util.ArrayList;
import java.util.List;
public class Rook extends Piece {
public Rook(boolean isWhite, int x, int y) {
super(isWhite, PieceType.Rook, x, y);
}
@Override
public PieceType getType() {
return PieceType.Rook;
}
@Override
public List<Move> getLegalMoves(Board board, int row, int col) {
List<Move> moves = new ArrayList<>();
int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (int[] dir : directions) {
int r = row + dir[0], c = col + dir[1];
while (board.isInBounds(r, c)) {
Piece target = board.getPieceAt(r, c);
if (target == null) {
moves.add(new Move(this, row, col, r, c));
} else {
if (target.isWhite() != this.isWhite)
moves.add(new Move(this, row, col, r, c));
break;
}
r += dir[0];
c += dir[1];
}
}
return moves;
}
@Override
public Piece clone() {
return new Rook(this.isWhite, this.x, this.y);
}
}