This commit is contained in:
clara 2025-05-13 11:42:47 +02:00
commit b56780571b
3 changed files with 62 additions and 20 deletions

View File

@ -315,9 +315,10 @@ public class Board {
if (king != null) { if (king != null) {
int kingX = king.getX(); int kingX = king.getX();
int kingY = king.getY(); int kingY = king.getY();
for (int i = 0; i < pieces.size(); i++) { for (int i = 0; i < pieces.size(); i++) {
Piece p = pieces.get(i); Piece p = pieces.get(i);
if (p.isWhite() != whiteKing) { if (p.isWhite() != whiteKing && p.getType() != PieceType.King) {
ArrayList<int[]> moves = getValidMoves(p); ArrayList<int[]> moves = getValidMoves(p);
for (int j = 0; j < moves.size(); j++) { for (int j = 0; j < moves.size(); j++) {
@ -342,29 +343,41 @@ public class Board {
Piece piece = pieces.get(i); Piece piece = pieces.get(i);
if (piece.isWhite() == whiteKing) { if (piece.isWhite() == whiteKing) {
ArrayList<int[]> moves = getValidMoves(piece); ArrayList<int[]> rawMoves = getValidMoves(piece);
for (int j = 0; j < moves.size(); j++) { for (int j = 0; j < rawMoves.size(); j++) {
int[] move = moves.get(j); int[] move = rawMoves.get(j);
int newX = move[0]; int newX = move[0];
int newY = move[1]; int newY = move[1];
// Simulate the board // Simulate the board
Board simBoard = new Board(this); Board simBoard = new Board(this);
Piece simPiece = simBoard.getPieceAt(piece.getX(), piece.getY());
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) { // Find the same piece in the simulated board
simBoard.getPieces().remove(captured); Piece simPiece = null;
for (int k = 0; k < simBoard.getPieces().size(); k++) {
Piece p = simBoard.getPieces().get(k);
if (p.getX() == piece.getX() && p.getY() == piece.getY()
&& p.getType() == piece.getType()
&& p.isWhite() == piece.isWhite()) {
simPiece = p;
}
} }
simPiece.setX(newX); if (simPiece != null) {
simPiece.setY(newY); Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
boolean stillInCheck = simBoard.isKingInCheck(whiteKing); simPiece.setX(newX);
simPiece.setY(newY);
if (!stillInCheck) { boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
hasEscape = true;
if (!stillInCheck) {
hasEscape = true;
}
} }
} }
} }

View File

@ -167,22 +167,51 @@ public class MoveConditions {
int[][] offsets = { int[][] offsets = {
{-1, -1}, {-1, 0}, {-1, 1}, {-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1}, {0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1} {1, -1}, {1, 0}, {1, 1}
}; };
for (int[] offset : offsets) { for (int i = 0; i < offsets.length; i++) {
int newX = x + offset[0]; int newX = x + offsets[i][0];
int newY = y + offset[1]; int newY = y + offsets[i][1];
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) { if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
Piece target = board.getPieceAt(newX, newY); Piece target = board.getPieceAt(newX, newY);
if (target == null || target.isWhite() != isWhite) { if (target == null || target.isWhite() != isWhite) {
moves.add(new int[]{newX, newY}); Board simBoard = new Board(board);
// Find this same King in the simulated board
Piece simKing = null;
for (int j = 0; j < simBoard.getPieces().size(); j++) {
Piece p = simBoard.getPieces().get(j);
if (p.getX() == x && p.getY() == y &&
p.getType() == PieceType.King &&
p.isWhite() == isWhite) {
simKing = p;
}
}
if (simKing != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
simKing.setX(newX);
simKing.setY(newY);
boolean stillInCheck = simBoard.isKingInCheck(isWhite);
if (!stillInCheck) {
moves.add(new int[]{newX, newY});
}
}
} }
} }
} }
return moves; return moves;
} }
} }

View File

@ -36,7 +36,7 @@ public class SpecialMoves {
public boolean isEnpassant() { public boolean isEnpassant() {
boolean isWhite = piece.isWhite(); boolean isWhite = piece.isWhite();
if (boolean isWhite == true|| PieceType getType() == PieceType.Pawn) { //no idea honestly if (isWhite == true || PieceType getType() == PieceType.Pawn) { //no idea honestly
if () if ()
} }