realized the castling didnt work twice (like white couldnt castle if

black had already castled) --solved
This commit is contained in:
Louise BERTELOOT 2025-05-21 21:11:29 +02:00
parent cf8a92b092
commit e221577626
1 changed files with 23 additions and 23 deletions

View File

@ -16,13 +16,9 @@ public class Board {
private int xCoordinatePawn; private int xCoordinatePawn;
private int yCoordinatePawn; private int yCoordinatePawn;
private boolean enPassant; private boolean enPassant;
<<<<<<< HEAD
private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore
=======
private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
private ArrayList<Board> previousStates; private ArrayList<Board> previousStates;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
@ -214,8 +210,6 @@ public class Board {
//store for castling //store for castling
int originalX = selectedPiece.getX(); int originalX = selectedPiece.getX();
// detects castling (piece is castling only if piecetype is king and distance between king and its move is of 2)
boolean isCastling = selectedPiece.getType() == PieceType.King && Math.abs(x - originalX) == 2;
if (clickedPiece != null) { if (clickedPiece != null) {
System.out.println("Capturing piece at: " + x + ", " + y); System.out.println("Capturing piece at: " + x + ", " + y);
@ -240,21 +234,27 @@ public class Board {
selectedPiece.setX(x); selectedPiece.setX(x);
selectedPiece.setY(y); selectedPiece.setY(y);
if (isCastling) {//checks if the king is eligible for castling // detects castling (piece is castling only if piecetype is king and distance between king and its move is of 2)
int row = selectedPiece.isWhite() ? 7 : 0;//deduces the row depending on weither the selected king is on white or not (if white, its in row 7, if its black its row 0) boolean isCastling = selectedPiece.getType() == PieceType.King && Math.abs(x - originalX) == 2;
if (x > originalX) {// means we are moving the king towards the right (king side castling)
Piece rook = getPieceAt(7, row);//selects the correct rook to be castled depending on weither the king went right or left (here selects right rook) if (isCastling) {
if (rook != null) {//if the rook is there int row = selectedPiece.isWhite() ? 7 : 0; //if the piece is white, the row is 7, otherwise its 0
rook.setX(5);//moves the rook to the square to the left of the king
rook.setMoved(true);//the rook has been moved once so wont be used for castling anymore boolean kingSide = x > selectedX;//detects if its king/queenside (right/left) bc if the destination x is greater than the selectedX, then you're going to the right
} Piece rook = getPieceAt(kingSide ? 7 : 0, row);//if the piece is kingside
}
else {//king moves to the left this time (queen side castling)
Piece rook = getPieceAt(0, row);//selects the rook on the left if (rook != null && !selectedPiece.hasMoved() && !rook.hasMoved()) {//if the rook on that side is at its initial pos+hasnt moved ever + king hasnt moved either
if (rook != null) { if (kingSide) {//if we are on kingside
rook.setX(3);//moves rook to the right of the king rook.setX(5);//column move of the rook for a kingside
rook.setMoved(true); rook.setY(row);//row defined for that rook depending on if its white or black
} else {//queenside
rook.setX(3);//column for queenside
rook.setY(row);//row depending black/white
} }
rook.setMoved(true);//this specific rook has been moved at least once
selectedPiece.setMoved(true);//selectedPiece (king, white or black) has been moved
} }
} }
@ -265,7 +265,7 @@ public class Board {
selectedY = -1; selectedY = -1;
highlightedSquares.clear(); highlightedSquares.clear();
// After move completed, check for check and checkmate // After move completed, check for check and checkmatea
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
boolean isWhite = (i == 0); boolean isWhite = (i == 0);