From 13873f52b40d7a5deff745357bcb90cb662de9c5 Mon Sep 17 00:00:00 2001 From: keshi Date: Sat, 17 May 2025 22:21:48 +0200 Subject: [PATCH] added the highlight moves for enpassant and castling. going to try to work on autoplayer or smth --- src/backend/Board.java | 16 ++- src/backend/HighlightManager.java | 161 -------------------------- src/backend/MoveHighlighter.java | 9 +- src/backend/MovementCapabilities.java | 16 +-- 4 files changed, 26 insertions(+), 176 deletions(-) delete mode 100644 src/backend/HighlightManager.java diff --git a/src/backend/Board.java b/src/backend/Board.java index caed3c2..1cca104 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -164,12 +164,22 @@ public class Board { //checking the type of piece it is to move it if(chosenPiece.getType()==PieceType.Pawn) { movementSuccess=movement.movePawn(x,y); + + if(!movementSuccess) { + MovementCapabilities specialMoves= new MovementCapabilities(); + movementSuccess= specialMoves.enPassant(chosenPiece, x, y, this); + } } else if (chosenPiece.getType()==PieceType.Rook) { movementSuccess=movement.moveRook(x, y); }else if (chosenPiece.getType()==PieceType.Bishop) { movementSuccess=movement.moveBishop(x, y); }else if (chosenPiece.getType()==PieceType.King) { movementSuccess=movement.moveKing(x, y); + + if(!movementSuccess) { + MovementCapabilities specialMoves=new MovementCapabilities(); + movementSuccess= specialMoves.castling(chosenPiece, x, y, this); + } }else if (chosenPiece.getType()==PieceType.Queen) { movementSuccess=movement.moveQueen(x, y); }else if (chosenPiece.getType()==PieceType.Knight) { @@ -184,7 +194,7 @@ public class Board { } else { //invalid move and try again } } //does nothing - } + } } //checks if any piece is selected and if its position matches that of the x,y coordinates @@ -249,9 +259,9 @@ public class Board { return out; //Give back the full array of text‐lines ready to be written to a file. } + public Board(String[] fileRep) { - - // Figure out how wide the board is by splitting line 0 on commas: + // Figure out how wide the board is by splitting line 0 on commas: // fileRep[0] might be something like "WR,WN,WB,WQ,WK,WB,WN,WR" // splitting on “,” gives 8 pieces, so width = 8. diff --git a/src/backend/HighlightManager.java b/src/backend/HighlightManager.java deleted file mode 100644 index effbb12..0000000 --- a/src/backend/HighlightManager.java +++ /dev/null @@ -1,161 +0,0 @@ -/*package backend; - -public class HighlightManager { - - private Board board; - - public HighlightManager(Board board) { - this.board = board; - } - - public void highlightPawnMoves(Piece piece) { - board.clearHighlights(); // Clear previous highlights - int currentX = piece.getX(); - int currentY = piece.getY(); - int direction = piece.isWhite() ? -1 : 1; - - // Forward move - if (board.getPiece(currentX, currentY + direction) == null) { - board.addHighlight(currentX, currentY + direction); - - // First move two steps forward - if ((piece.isWhite() && currentY == 6) || (!piece.isWhite() && currentY == 1)) { - if (board.getPiece(currentX, currentY + direction) == null && - board.getPiece(currentX, currentY + 2 * direction) == null) { - board.addHighlight(currentX, currentY + 2 * direction); - } - } - } - - // Diagonal captures - Piece rightCapture = board.getPiece(currentX + 1, currentY + direction); - if (rightCapture != null && rightCapture.isWhite() != piece.isWhite()) { - board.addHighlight(currentX + 1, currentY + direction); - } - - Piece leftCapture = board.getPiece(currentX - 1, currentY + direction); - if (leftCapture != null && leftCapture.isWhite() != piece.isWhite()) { - board.addHighlight(currentX - 1, currentY + direction); - - } - - board.refreshHighlights(); - - } - - - - - - - // Rook Moves - public void highlightRookMoves(Piece piece) { - board.clearHighlights(); - int currentX = piece.getX(); - int currentY = piece.getY(); - - // Vertical and Horizontal Moves - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX + i, currentY)) break; - } - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX - i, currentY)) break; - } - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX, currentY + i)) break; - } - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX, currentY - i)) break; - } - board.refreshHighlights(); - } - - // Bishop Moves - public void highlightBishopMoves(Piece piece) { - board.clearHighlights(); - int currentX = piece.getX(); - int currentY = piece.getY(); - - // Diagonal Moves - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX + i, currentY + i)) break; - } - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX - i, currentY - i)) break; - } - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX + i, currentY - i)) break; - } - for (int i = 1; i < 8; i++) { - if (!addHighlightIfValid(currentX - i, currentY + i)) break; - } - board.refreshHighlights(); - } - - // Knight Moves - public void highlightKnightMoves(Piece piece) { - board.clearHighlights(); - int currentX = piece.getX(); - int currentY = piece.getY(); - int[][] moves = { - {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, - {1, 2}, {1, -2}, {-1, 2}, {-1, -2} - }; - - for (int[] move : moves) { - addHighlightIfValid(currentX + move[0], currentY + move[1]); - } - board.refreshHighlights(); - } - - // Helper method to add highlight if the move is valid - private boolean addHighlightIfValid(int x, int y) { - if (x >= 0 && x < 8 && y >= 0 && y < 8) { - Piece target = board.getPiece(x, y); - if (target == null) { - board.addHighlight(x, y); - return true; - } else if (target.isWhite() != board.getPiece(x, y).isWhite()) { - board.addHighlight(x, y); - return false; - } else { - return false; - } - } - return false; - } - // Queen Moves (Combination of Rook and Bishop) - public void highlightQueenMoves(Piece piece) { - board.clearHighlights(); - highlightRookMoves(piece); // Horizontal and Vertical paths - highlightBishopMoves(piece); // Diagonal paths - - board.refreshHighlights(); - } - - public void highlightKingMoves(Piece piece) { - board.clearHighlights(); - int currentX = piece.getX(); - int currentY = piece.getY(); - int[][] moves = { - {1, 0}, {-1, 0}, {0, 1}, {0, -1}, - {1, 1}, {-1, -1}, {1, -1}, {-1, 1} - }; - - for (int[] move : moves) { - addHighlightIfValid(currentX + move[0], currentY + move[1]); - } - board.refreshHighlights(); - } - - - - - } - - */ - - - - - diff --git a/src/backend/MoveHighlighter.java b/src/backend/MoveHighlighter.java index 453bee1..bf82d8e 100644 --- a/src/backend/MoveHighlighter.java +++ b/src/backend/MoveHighlighter.java @@ -7,6 +7,7 @@ public class MoveHighlighter { ArrayList validMoves = new ArrayList(); MovePiece movement = new MovePiece(piece, board); PieceType type= piece.getType(); + MovementCapabilities specialMoves= new MovementCapabilities(); //looping through each square space for (int x = 0; x < board.getWidth(); x++) { @@ -15,11 +16,13 @@ public class MoveHighlighter { System.out.println("MoveHighlighter: checking (" + x + "," + y + ") for type " + type); if(type==PieceType.Pawn) { - valid=movement.movePawnSimulate(x, y); + valid=movement.movePawnSimulate(x, y)|| specialMoves.enPassant(piece, x, y, board);; } else if(type==PieceType.Rook) { valid=movement.moveRookSimulate(x, y); }else if(type==PieceType.King) { - valid=movement.moveKingSimulate(x, y); + valid=movement.moveKingSimulate(x, y)|| specialMoves.castling(piece, x, y, new Board(board)); + //Because castling moves two pieces (king and rook), and you don’t want the highlight simulation to affect your real board. + //Creating a temporary copy (new Board(board)) allows us to test if castling would work without breaking anything. }else if(type==PieceType.Queen) { valid=movement.moveQueenSimulate(x, y); }else if(type==PieceType.Bishop) { @@ -28,6 +31,8 @@ public class MoveHighlighter { valid=movement.moveKnightSimulate(x, y); } if(valid) { + System.out.println("✅ Valid move for " + type + " at (" + piece.getX() + "," + piece.getY() + ") → (" + x + "," + y + ")"); + System.out.println("Highlighting move to (" + x + "," + y + ") for " + type); validMoves.add(new int[] {x,y}); } } diff --git a/src/backend/MovementCapabilities.java b/src/backend/MovementCapabilities.java index ba7f572..e0d9897 100644 --- a/src/backend/MovementCapabilities.java +++ b/src/backend/MovementCapabilities.java @@ -1,6 +1,8 @@ package backend; public class MovementCapabilities { + + //en passant public boolean enPassant(Piece piece, int x, int y, Board board) { if (piece.getType() != PieceType.Pawn) { return false; @@ -17,7 +19,7 @@ public class MovementCapabilities { if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y)== null) { Move last = board.getLastMove(); - System.out.println("Last move is " + last); // Keeping record of the last move the + System.out.println("Last move is " + last); // Keeping record of the last move if (last != null) { Piece lastMoved = last.getPieceMoved(); @@ -49,6 +51,7 @@ public class MovementCapabilities { } + //castling public boolean castling(Piece king, int x, int y, Board board) { if (king.getType()!= PieceType.King|| king.getDidMove()) { return false; @@ -58,7 +61,6 @@ public class MovementCapabilities { int currentY = king.getY(); boolean isWhite = king.isWhite(); // getting coordinates and color of the king - if (y != currentY) { // checking to see if the king has moved during the game yet return false; } @@ -73,10 +75,8 @@ public class MovementCapabilities { if (isRook && rookMoved) { if (emptySpaceRight) { - board.movePiece(currentX, currentY, 6, currentY); // moving the king to ... board.movePiece(7, currentY, 5, currentY); // moving the rook to ... - return true; } } @@ -93,14 +93,10 @@ public class MovementCapabilities { if (emptySpaceLeft) { board.movePiece(currentX, currentY, 2, currentY); // moving king to... board.movePiece(0,currentY,3,currentY); // moving rook to... - return true; - - } - + return true; + } } } - - return false; } }