This commit is contained in:
parent
04d3fef8ac
commit
a16bcbf7fd
|
|
@ -13,10 +13,6 @@ public class Board {
|
|||
private ArrayList<int[]> highlightedSquares = new ArrayList<>();
|
||||
private ArrayList<Move> moveHistory = new ArrayList<>();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
this.height = lineNum;
|
||||
|
|
@ -250,9 +246,24 @@ public class Board {
|
|||
}
|
||||
|
||||
public Board(Board board) {
|
||||
//TODO
|
||||
this.width = board.width;
|
||||
this.height = board.height;
|
||||
this.turnNumber = board.turnNumber;
|
||||
this.selectedX = board.selectedX;
|
||||
this.selectedY = board.selectedY;
|
||||
|
||||
}
|
||||
this.pieces = new ArrayList<>();
|
||||
for (Piece p : board.pieces) {
|
||||
this.pieces.add(new Piece(p.getType(), p.isWhite(), p.getX(), p.getY()));
|
||||
}
|
||||
|
||||
this.highlightedSquares = new ArrayList<>();
|
||||
for (int[] pos : board.highlightedSquares) {
|
||||
this.highlightedSquares.add(new int[]{pos[0], pos[1]});
|
||||
}
|
||||
|
||||
this.moveHistory = new ArrayList<>(board.moveHistory);
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
if (move == null) return;
|
||||
|
|
@ -289,6 +300,31 @@ public class Board {
|
|||
selectedY = -1;
|
||||
highlightedSquares.clear();
|
||||
}
|
||||
public boolean isKingInCheck(boolean isWhite) {
|
||||
Piece king = null;
|
||||
for (Piece p : pieces) {
|
||||
if (p.getType() == PieceType.King && p.isWhite() == isWhite) {
|
||||
king = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (king == null) return true;
|
||||
|
||||
int kingX = king.getX();
|
||||
int kingY = king.getY();
|
||||
|
||||
for (Piece p : pieces) {
|
||||
if (p.isWhite() != isWhite) {
|
||||
ArrayList<int[]> moves = Move.getLegalMoves(this, p);
|
||||
for (int[] move : moves) {
|
||||
if (move[0] == kingX && move[1] == kingY) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public Piece getPieceAt(int x, int y) {
|
||||
|
|
|
|||
|
|
@ -107,6 +107,26 @@ public class Move {
|
|||
return moves;
|
||||
}
|
||||
|
||||
public static ArrayList<int[]> getLegalMoves(Board board, Piece piece) {
|
||||
ArrayList<int[]> legalMoves = new ArrayList<>();
|
||||
ArrayList<int[]> candidateMoves = getPossibleMoves(board, piece);
|
||||
|
||||
for (int[] move : candidateMoves) {
|
||||
Board clone = new Board(board); // Deep copy constructor
|
||||
Piece moved = clone.getPieceAt(piece.getX(), piece.getY());
|
||||
|
||||
// Play the simulated move
|
||||
Piece captured = clone.getPieceAt(move[0], move[1]);
|
||||
Move simulated = new Move(moved, piece.getX(), piece.getY(), move[0], move[1], captured);
|
||||
clone.playMove(simulated);
|
||||
|
||||
if (!clone.isKingInCheck(piece.isWhite())) {
|
||||
legalMoves.add(move);
|
||||
}
|
||||
}
|
||||
return legalMoves;
|
||||
}
|
||||
|
||||
private static boolean isInsideBoard(Board board, int x, int y) {
|
||||
return x >= 0 && x < board.getWidth() && y >= 0 && y < board.getHeight();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue