working with new class no static

This commit is contained in:
Jérôme BEDIER 2025-05-21 21:59:42 +02:00
parent 661d5980c7
commit fd51f7932a
2 changed files with 28 additions and 74 deletions

View File

@ -524,12 +524,13 @@ public class Board {
undoStack.push(toFileRep()); undoStack.push(toFileRep());
Piece piece = board[move.getFromX()][move.getFromY()]; Piece piece = board[move.getFromX()][move.getFromY()];
SpecialMoves specialMoves = new SpecialMoves();
// Handle en passant capture // Handle en passant capture
if (SpecialMoves.isEnPassant(this, move)) { if (specialMoves.isEnPassant(this, move)) {
Piece capturedPawn = board[move.getToX()][move.getFromY()]; Piece capturedPawn = board[move.getToX()][move.getFromY()];
move.setCapturedPiece(capturedPawn); move.setCapturedPiece(capturedPawn);
SpecialMoves.handleEnPassant(this, move); specialMoves.handleEnPassant(this, move);
} }
// Move the piece // Move the piece
@ -539,18 +540,19 @@ public class Board {
piece.y = move.getToY(); piece.y = move.getToY();
// Update en passant tracking // Update en passant tracking
SpecialMoves.updateEnPassantTracking(this, move, piece); specialMoves.updateEnPassantTracking(this, move, piece);
// Handle pawn promotion // Handle pawn promotion
if (piece.getType() == PieceType.Pawn && if (piece.getType() == PieceType.Pawn &&
(piece.getY() == 0 || piece.getY() == height - 1)) { (piece.getY() == 0 || piece.getY() == height - 1)) {
board[piece.getX()][piece.getY()] = SpecialMoves.promotePawn(piece.isWhite(), piece.getX(), piece.getY()); board[piece.getX()][piece.getY()] = specialMoves.promotePawn(piece.isWhite(), piece.getX(), piece.getY());
} }
turn++; turn++;
} }
//Finds the position of the king of a given color. //Finds the position of the king of a given color.
public Piece findKing(boolean isWhite) { public Piece findKing(boolean isWhite) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {

View File

@ -2,89 +2,41 @@ package backend;
public class SpecialMoves { public class SpecialMoves {
/** // Pawn promotion: always promotes to Queen for simplicity
* Handles pawn promotion, returning a new Queen piece public Piece promotePawn(boolean isWhite, int x, int y) {
* @param isWhite color of the pawn being promoted
* @param x x-coordinate of the pawn
* @param y y-coordinate of the pawn
* @return the new Queen piece that replaces the pawn
*/
public static Piece promotePawn(boolean isWhite, int x, int y) {
return new Piece(isWhite, PieceType.Queen, x, y); return new Piece(isWhite, PieceType.Queen, x, y);
} }
/** // Checks if a move is an en passant capture
* Checks if a move is an en passant capture public boolean isEnPassant(Board board, Move move) {
* @param board the current board state
* @param move the move being checked
* @return true if the move is an en passant capture
*/
public static boolean isEnPassant(Board board, Move move) {
Piece piece = board.getPieceAt(move.getFromX(), move.getFromY()); Piece piece = board.getPieceAt(move.getFromX(), move.getFromY());
if (piece == null || piece.getType() != PieceType.Pawn) return false;
// Must be a pawn if (move.getToX() == move.getFromX()) return false;
if (piece == null || piece.getType() != PieceType.Pawn) {
return false;
}
// Must be a diagonal move
if (move.getToX() == move.getFromX()) {
return false;
}
// Must be moving to an empty square
return board.isEmpty(move.getToX(), move.getToY()); return board.isEmpty(move.getToX(), move.getToY());
} }
/** // Handles the en passant capture
* Handles the en passant capture by removing the captured pawn public void handleEnPassant(Board board, Move move) {
* @param board the current board state
* @param move the en passant move
*/
public static void handleEnPassant(Board board, Move move) {
// The captured pawn is on the same row as the starting position
// but on the same column as the destination
board.removePieceAt(move.getToX(), move.getFromY()); board.removePieceAt(move.getToX(), move.getFromY());
} }
/** // Updates en passant tracking after a pawn's double move
* Tracks en passant opportunities after a pawn's double move public void updateEnPassantTracking(Board board, Move move, Piece piece) {
* @param board the current board state
* @param move the move that was just played
*/
public static void updateEnPassantTracking(Board board, Move move, Piece piece) {
// Reset en passant tracking
board.setEnPassantCol(-1); board.setEnPassantCol(-1);
board.setEnPassantRow(-1); board.setEnPassantRow(-1);
// Check if this is a pawn making a double move
if (piece.getType() == PieceType.Pawn && if (piece.getType() == PieceType.Pawn &&
Math.abs(move.getFromY() - move.getToY()) == 2) { Math.abs(move.getFromY() - move.getToY()) == 2) {
board.setEnPassantCol(move.getToX()); board.setEnPassantCol(move.getToX());
board.setEnPassantRow((move.getFromY() + move.getToY()) / 2); // Middle square board.setEnPassantRow((move.getFromY() + move.getToY()) / 2);
} }
} }
/** // (Optional) Placeholder for castling logic
* Checks if a castling move is legal public boolean canCastle(Board board, Piece king, boolean isKingside) {
* @param board the current board state
* @param king the king piece
* @param isKingside true for kingside castling, false for queenside
* @return true if castling is legal
*/
public static boolean canCastle(Board board, Piece king, boolean isKingside) {
// Implementation for castling checks
// This is a placeholder - you'll need to implement the full logic
return false; return false;
} }
/** public void handleCastling(Board board, Move move, boolean isKingside) {
* Executes a castling move // Implement castling logic if needed
* @param board the current board state
* @param move the castling move
*/
public static void handleCastling(Board board, Move move, boolean isKingside) {
// Implementation for castling execution
// This is a placeholder - you'll need to implement the full logic
} }
} }