diff --git a/src/backend/Board.java b/src/backend/Board.java index 32f3681..1ff87a7 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -16,13 +16,9 @@ public class Board { private int xCoordinatePawn; private int yCoordinatePawn; 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 - ->>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git + private ArrayList previousStates; public Board(int colNum, int lineNum) { @@ -214,9 +210,7 @@ public class Board { //store for castling 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) { System.out.println("Capturing piece at: " + x + ", " + y); pieces.remove(clickedPiece); @@ -240,21 +234,27 @@ public class Board { selectedPiece.setX(x); selectedPiece.setY(y); - if (isCastling) {//checks if the king is eligible for castling - 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) - 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 (rook != null) {//if the rook is there - 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 - } - } - 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) { - rook.setX(3);//moves rook to the right of the king - rook.setMoved(true); + // 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 (isCastling) { + int row = selectedPiece.isWhite() ? 7 : 0; //if the piece is white, the row is 7, otherwise its 0 + + 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 + + + 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 (kingSide) {//if we are on kingside + rook.setX(5);//column move of the rook for a kingside + 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; 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++) { boolean isWhite = (i == 0);