This commit is contained in:
cleme 2025-05-23 17:05:06 +02:00
parent bf15b4da2c
commit b08bd6477f
3 changed files with 104 additions and 63 deletions

View File

@ -1 +1,2 @@
/backend/ /backend/
/windowInterface/

View File

@ -12,6 +12,11 @@ public class Move {
private Piece capturedPiece; private Piece capturedPiece;
private Board board; private Board board;
// Castling-related fields
private CastlingHandler castlingHandler;
private CastlingHandler.CastlingMove castlingMove;
private boolean isCastling;
public Move(int fromX, int fromY, int toX, int toY, Board board) { public Move(int fromX, int fromY, int toX, int toY, Board board) {
this.fromX = fromX; this.fromX = fromX;
this.fromY = fromY; this.fromY = fromY;
@ -20,6 +25,11 @@ public class Move {
this.board = board; this.board = board;
this.movingPiece = board.getPieceAt(fromX, fromY); this.movingPiece = board.getPieceAt(fromX, fromY);
this.capturedPiece = board.getPieceAt(toX, toY); this.capturedPiece = board.getPieceAt(toX, toY);
// Initialize castling handler and check if this is a castling move
this.castlingHandler = new CastlingHandler(board);
this.castlingMove = castlingHandler.getCastlingMove(fromX, fromY, toX, toY);
this.isCastling = (castlingMove != null);
} }
public boolean isValid() { public boolean isValid() {
@ -28,21 +38,31 @@ public class Move {
return false; return false;
} }
// Handle castling validation
if (isCastling) {
return castlingHandler.isCastlingValid(castlingMove);
}
// Check if the move is in the list of valid moves for this piece // Check if the move is in the list of valid moves for this piece
List<Position> validMoves = getValidDestinations(movingPiece, board); List<Position> validMoves = getValidDestinations(movingPiece, board);
return validMoves.contains(new Position(toX, toY)); return containsPosition(validMoves, new Position(toX, toY));
} }
public void execute() { public void execute() {
// Remove any piece at the destination (normal capture) if (isCastling) {
if (capturedPiece != null) { // Execute castling move
board.removePiece(toX, toY); castlingHandler.executeCastling(castlingMove);
} else {
// Remove any piece at the destination (normal capture)
if (capturedPiece != null) {
board.removePiece(toX, toY);
}
// Move the piece (remove from original position, add to new position)
board.removePiece(fromX, fromY);
board.setPiece(movingPiece.isWhite(), movingPiece.getType(), toX, toY);
} }
// Move the piece (remove from original position, add to new position)
board.removePiece(fromX, fromY);
board.setPiece(movingPiece.isWhite(), movingPiece.getType(), toX, toY);
// Advance turn // Advance turn
board.advanceTurn(); board.advanceTurn();
} }
@ -55,15 +75,25 @@ public class Move {
Piece tempPiece = tempBoard.getPieceAt(fromX, fromY); Piece tempPiece = tempBoard.getPieceAt(fromX, fromY);
if (tempPiece == null) return true; // Safety check if (tempPiece == null) return true; // Safety check
// Remove any piece at destination (normal capture) if (isCastling) {
tempBoard.removePiece(toX, toY); // For castling, we need to check the castling move specifically
CastlingHandler tempHandler = new CastlingHandler(tempBoard);
// Move the piece CastlingHandler.CastlingMove tempCastling = tempHandler.getCastlingMove(fromX, fromY, toX, toY);
tempBoard.removePiece(fromX, fromY); if (tempCastling != null) {
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY); tempHandler.executeCastling(tempCastling);
}
} else {
// Remove any piece at destination (normal capture)
tempBoard.removePiece(toX, toY);
// Move the piece
tempBoard.removePiece(fromX, fromY);
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY);
}
// Check if king is in check after move // Check if king is in check after move
return isKingInCheck(tempBoard, tempPiece.isWhite()); CastlingHandler tempHandler = new CastlingHandler(tempBoard);
return tempHandler.isKingInCheck(tempPiece.isWhite());
} }
public boolean putsOpponentInCheck() { public boolean putsOpponentInCheck() {
@ -74,15 +104,25 @@ public class Move {
Piece tempPiece = tempBoard.getPieceAt(fromX, fromY); Piece tempPiece = tempBoard.getPieceAt(fromX, fromY);
if (tempPiece == null) return false; // Safety check if (tempPiece == null) return false; // Safety check
// Remove any piece at destination (normal capture) if (isCastling) {
tempBoard.removePiece(toX, toY); // For castling, execute the castling move
CastlingHandler tempHandler = new CastlingHandler(tempBoard);
// Move the piece CastlingHandler.CastlingMove tempCastling = tempHandler.getCastlingMove(fromX, fromY, toX, toY);
tempBoard.removePiece(fromX, fromY); if (tempCastling != null) {
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY); tempHandler.executeCastling(tempCastling);
}
} else {
// Remove any piece at destination (normal capture)
tempBoard.removePiece(toX, toY);
// Move the piece
tempBoard.removePiece(fromX, fromY);
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY);
}
// Check if opponent's king is in check after move // Check if opponent's king is in check after move
return isKingInCheck(tempBoard, !tempPiece.isWhite()); CastlingHandler tempHandler = new CastlingHandler(tempBoard);
return tempHandler.isKingInCheck(!tempPiece.isWhite());
} }
public boolean putsOpponentInCheckmate() { public boolean putsOpponentInCheckmate() {
@ -98,12 +138,21 @@ public class Move {
Piece tempPiece = tempBoard.getPieceAt(fromX, fromY); Piece tempPiece = tempBoard.getPieceAt(fromX, fromY);
if (tempPiece == null) return false; // Safety check if (tempPiece == null) return false; // Safety check
// Remove any piece at destination (normal capture) if (isCastling) {
tempBoard.removePiece(toX, toY); // For castling, execute the castling move
CastlingHandler tempHandler = new CastlingHandler(tempBoard);
// Move the piece CastlingHandler.CastlingMove tempCastling = tempHandler.getCastlingMove(fromX, fromY, toX, toY);
tempBoard.removePiece(fromX, fromY); if (tempCastling != null) {
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY); tempHandler.executeCastling(tempCastling);
}
} else {
// Remove any piece at destination (normal capture)
tempBoard.removePiece(toX, toY);
// Move the piece
tempBoard.removePiece(fromX, fromY);
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY);
}
boolean opponentColor = !tempPiece.isWhite(); boolean opponentColor = !tempPiece.isWhite();
@ -133,23 +182,23 @@ public class Move {
switch (piece.getType()) { switch (piece.getType()) {
case Pawn: case Pawn:
addPawnMoves(moves, piece, board); addPawnMoves(moves, piece, board);
return moves; break;
case Rook: case Rook:
addRookMoves(moves, piece, board); addRookMoves(moves, piece, board);
return moves; break;
case Knight: case Knight:
addKnightMoves(moves, piece, board); addKnightMoves(moves, piece, board);
return moves; break;
case Bishop: case Bishop:
addBishopMoves(moves, piece, board); addBishopMoves(moves, piece, board);
return moves; break;
case Queen: case Queen:
addRookMoves(moves, piece, board); addRookMoves(moves, piece, board);
addBishopMoves(moves, piece, board); addBishopMoves(moves, piece, board);
return moves; break;
case King: case King:
addKingMoves(moves, piece, board); addKingMoves(moves, piece, board);
return moves; break;
} }
return moves; return moves;
@ -172,33 +221,8 @@ public class Move {
} }
public boolean isKingInCheck(Board board, boolean isWhiteKing) { public boolean isKingInCheck(Board board, boolean isWhiteKing) {
// Find the king's position CastlingHandler handler = new CastlingHandler(board);
Piece king = null; return handler.isKingInCheck(isWhiteKing);
for (Piece p : board.getPieces()) {
if (p.getType() == PieceType.King && p.isWhite() == isWhiteKing) {
king = p;
return checkIfKingUnderAttack(board, isWhiteKing, king);
}
}
return false;
}
private boolean checkIfKingUnderAttack(Board board, boolean isWhiteKing, Piece king) {
// Check if any opponent piece can attack the king
for (Piece p : board.getPieces()) {
if (p.isWhite() == isWhiteKing) continue; // Skip pieces of same color
// Get raw moves without check validation
List<Position> attackMoves = getValidDestinations(p, board);
// If any piece can move to king's position, king is in check
if (attackMoves.contains(new Position(king.getX(), king.getY()))) {
return true;
}
}
return false;
} }
private void addPawnMoves(List<Position> validMoves, Piece piece, Board board) { private void addPawnMoves(List<Position> validMoves, Piece piece, Board board) {
@ -347,7 +371,7 @@ public class Move {
int y = piece.getY(); int y = piece.getY();
boolean isWhite = piece.isWhite(); boolean isWhite = piece.isWhite();
// All adjacent squares // All adjacent squares (normal king moves)
for (int dx = -1; dx <= 1; dx++) { for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) { for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue; // Skip the current position if (dx == 0 && dy == 0) continue; // Skip the current position
@ -365,6 +389,23 @@ public class Move {
} }
} }
} }
// Add castling moves
CastlingHandler handler = new CastlingHandler(board);
List<CastlingHandler.Position> castlingPositions = handler.getCastlingPositions(piece);
for (CastlingHandler.Position castlingPos : castlingPositions) {
validMoves.add(new Position(castlingPos.x, castlingPos.y));
}
}
// Helper method to check if a list contains a position
private boolean containsPosition(List<Position> positions, Position target) {
for (Position pos : positions) {
if (pos.x == target.x && pos.y == target.y) {
return true;
}
}
return false;
} }
public class Position { public class Position {
@ -376,7 +417,6 @@ public class Move {
this.y = y; this.y = y;
} }
@Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false; if (obj == null || getClass() != obj.getClass()) return false;