diff --git a/src/backend/Board.java b/src/backend/Board.java index 68c6d3b..58c353b 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -308,12 +308,49 @@ public class Board { /* The following methods require more work ! */ - private void highlightedPossibleMoves(Piece piece) { + /*private void highlightedPossibleMoves(Piece piece) { highlightedSquares.clear(); MoveHighlighter highlighter= new MoveHighlighter(); highlightedSquares.addAll(highlighter.getPossibleMoves(piece, this)); } +*/ + public void highlightedPossibleMoves(Piece piece) { + highlightedSquares.clear(); + MovePiece move = new MovePiece(piece, this); + int x = piece.getX(); + int y = piece.getY(); + for (int newX = 0; newX < width; newX++) { + for (int newY = 0; newY < height; newY++) { + boolean isValid = false; + + switch (piece.getType()) { + case Pawn: + isValid = move.movePawnSimulate(newX, newY); + break; + case Rook: + isValid = move.moveRookSimulate(newX, newY); + break; + case Knight: + isValid = move.moveKnightSimulate(newX, newY); + break; + case Bishop: + isValid = move.moveBishopSimulate(newX, newY); + break; + case Queen: + isValid = move.moveQueenSimulate(newX, newY); + break; + case King: + isValid = move.moveKingSimulate(newX, newY); + break; + } + if (isValid) { + highlightedSquares.add(new int[]{newX, newY}); + } + } + } + } + public boolean isHighlighted(int x, int y) { for(int i=0; i getPossibleMoves(Piece piece, Board board){ - ArrayList validMoves = new ArrayList(); + public ArrayList getPossibleMoves(Piece piece, Board board){ //checking every sqaure on the board + ArrayList validMoves = new ArrayList();//adding possible squares to list MovePiece movement = new MovePiece(piece, board); PieceType type= piece.getType(); MovementCapabilities specialMoves= new MovementCapabilities(); diff --git a/src/backend/MovePiece.java b/src/backend/MovePiece.java index 745650a..91c5429 100644 --- a/src/backend/MovePiece.java +++ b/src/backend/MovePiece.java @@ -11,53 +11,27 @@ public class MovePiece { } /*alternative - * public boolean validateMove(int x, int y, boolean simulate) { - PieceType type = piece.getType(); - switch(type) { - case Pawn: - return validatePawnMove(x, y, simulate); - case Rook: - return validateRookMove(x, y, simulate); - case Knight: - return validateKnightMove(x, y, simulate); - case Bishop: - return validateBishopMove(x, y, simulate); - case Queen: - return validateQueenMove(x, y, simulate); - case King: - return validateKingMove(x, y, simulate); - default: - return false; - } - } - - public boolean moveKnight(int x, int y) { - return validateMove(x, y, false); - } - - public boolean moveKnightSimulate(int x, int y) { - return validateMove(x, y, true); - } - - private boolean validateKnightMove(int x, int y, boolean simulate) { - int currentX = piece.getX(); - int currentY = piece.getY(); - - int[][] knightMoves = { //we establish all possible moves a knight can have - {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, - {1, 2}, {1, -2}, {-1, 2}, {-1, -2} - }; - for (int[] move : knightMoves) { // we create a loop through each possible move - if (currentX + move[0] == x && currentY + move[1] == y) { // checks if move matches the target coordination (move0 is in x direction, move 1 in y) - Piece targetPiece = board.getPiece(x, y); //get the piece on the target spot - if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { - board.movePiece(currentX, currentY, x, y); //move our figure to the target spot - return true; - } + public boolean moveKnight(int x, int y, boolean simulate) { + int currentX = piece.getX(); + int currentY = piece.getY(); + + int[][] knightMoves = { + {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, + {1, 2}, {1, -2}, {-1, 2}, {-1, -2} + }; + + for (int[] move : knightMoves) { + if (currentX + move[0] == x && currentY + move[1] == y) { + Piece targetPiece = board.getPiece(x, y); + if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { + if (!simulate) board.movePiece(currentX, currentY, x, y); + return true; } } - return false; - } */ + } + return false; + } + } */ ///Pawn movement logic public boolean movePawn(int x, int y) { //method definition for pawn @@ -127,7 +101,6 @@ public class MovePiece { return true; } } - } return false; }