From 859e7439227ca2307a298eb1d782db253aa9be7d Mon Sep 17 00:00:00 2001 From: mariettakazimierczak Date: Sat, 10 May 2025 20:00:48 +0200 Subject: [PATCH 1/2] highlights and usertouch in progress --- src/backend/Board.java | 123 +++++++++++++++++++++-------- src/backend/initial_Usertouch.java | 39 +++++++++ 2 files changed, 127 insertions(+), 35 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index ed72e07..5b248cc 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -96,39 +96,88 @@ public class Board { public void userTouch(int x, int y) { Piece clickedPiece=board[x][y]; + + if(chosenPiece==null) { - //when no piece is selected - if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { - chosenPiece=clickedPiece; - } - } - else { //if a piece is already selected - if(isSelected(x,y)) { - chosenPiece=null; //unselect the chosen piece to revert to normal state - } - else { //move selected piece to new position using MovePiece logics - MovePiece movement= new MovePiece(chosenPiece, this); - boolean movementSuccess=false; - if(chosenPiece.getType()==PieceType.Pawn) { - movementSuccess=movement.movePawn(x,y); - } 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); - }else if (chosenPiece.getType()==PieceType.Queen) { - movementSuccess=movement.moveQueen(x, y); - } - if(movementSuccess) { - chosenPiece=null; - turnNumber++; - isTurnWhite=! isTurnWhite; - } else { //invalid move - } - } + // Select a piece if one is clicked + if (clickedPiece != null) { + chosenPiece = clickedPiece; + // Clear previous highlights + highlightedSquares.clear(); + // Highlight the new possible moves + addPossibleMoves(chosenPiece); + } + + } else { + // If a move is clicked, try to move + if (isHighlighted(x, y)) { + MovePiece movePiece = new MovePiece(chosenPiece, this); + boolean successfulMove = false; + + // Call the right move method based on the piece type + switch (chosenPiece.getType()) { + case Pawn: + successfulMove = movePiece.movePawn(x, y); + break; // (break means exit the loop) + case Rook: + successfulMove = movePiece.moveRook(x, y); + break; + case Bishop: + successfulMove = movePiece.moveBishop(x, y); + break; + case Knight: + successfulMove = movePiece.moveKnight(x, y); + break; + default: + System.out.println("Unknown Piece Type"); //if the piece.getType() returns something unexpected we get this message + + } + if (successfulMove) { + turnNumber++; + isTurnWhite = !isTurnWhite; + } + } + + // Unselect the piece and clear highlights + chosenPiece = null; + highlightedSquares.clear(); + } + + repaint(); + + } + + private void addPossibleMoves(Piece piece) { + MovePiece movePiece = new MovePiece(piece, this); + int currentX = piece.getX(); + int currentY = piece.getY(); + + // Loop through the board to check possible moves + for (int x = 0; x < 8; x++) { + for (int y = 0; y < 8; y++) { + boolean canMove; + switch (piece.getType()) { + case Pawn: + movePiece.movePawn(x, y); + case Rook: + movePiece.moveRook(x, y); + case Bishop: + movePiece.moveBishop(x, y); + case Knight: + movePiece.moveKnight(x, y); + default: + canMove = false; + System.out.println("Unknown Piece Type"); + }; + + if (canMove) { + highlightedSquares.add(new int[]{x, y}); + } + } + } } + public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y; @@ -166,12 +215,16 @@ public class Board { } /* The following methods require more work ! */ - + public boolean isHighlighted(int x, int y) { - //return highlightedSquares.contains(Point(x,y)); - return true - ;} - + for (int[] square : highlightedSquares) { + if (square[0] == x && square[1] == y) { + return true; + } + } + return false; + } + public void undoLastMove() { //TODO diff --git a/src/backend/initial_Usertouch.java b/src/backend/initial_Usertouch.java index 93dced1..6a28ec6 100644 --- a/src/backend/initial_Usertouch.java +++ b/src/backend/initial_Usertouch.java @@ -24,4 +24,43 @@ public class initial_Usertouch { } } +*/ +//Keshini's user touch +/* +public void userTouch(int x, int y) { + Piece clickedPiece=board[x][y]; + if(chosenPiece==null) { + //when no piece is selected + if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { + chosenPiece=clickedPiece; + } + } + else { //if a piece is already selected + if(isSelected(x,y)) { + chosenPiece=null; //unselect the chosen piece to revert to normal state + } + else { //move selected piece to new position using MovePiece logics + MovePiece movement= new MovePiece(chosenPiece, this); + boolean movementSuccess=false; + if(chosenPiece.getType()==PieceType.Pawn) { + movementSuccess=movement.movePawn(x,y); + } 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); + }else if (chosenPiece.getType()==PieceType.Queen) { + movementSuccess=movement.moveQueen(x, y); + } + if(movementSuccess) { + chosenPiece=null; + turnNumber++; + isTurnWhite=! isTurnWhite; + } else { //invalid move + } + } + } + } + */ \ No newline at end of file From e4561f42aba8d938fcf52e4fdce98e8a6ce4b4ed Mon Sep 17 00:00:00 2001 From: mariettakazimierczak Date: Sat, 10 May 2025 23:50:01 +0200 Subject: [PATCH 2/2] I DID HIGHLIGHTS BUT ITS NOT WORKING CORRECTLY ALSO WE CAN NOT MOVE THE FIGURE --- src/backend/Board.java | 140 +++++++++++--------------- src/backend/HighlightManager.java | 161 ++++++++++++++++++++++++++++++ src/backend/MovePiece.java | 19 ++++ 3 files changed, 239 insertions(+), 81 deletions(-) create mode 100644 src/backend/HighlightManager.java diff --git a/src/backend/Board.java b/src/backend/Board.java index 5b248cc..d43cf34 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -1,9 +1,13 @@ package backend; -import java.util.ArrayList; -public class Board { - private int width; +import java.util.ArrayList; +import javax.swing.JPanel; + + +public class Board extends JPanel { + private static final long serialVersionUID = 1L; + private int width; private int height; private Piece[][] board;//creating an array for the coordinates of the board private Piece chosenPiece=null; @@ -34,7 +38,7 @@ public class Board { } public void setPiece(boolean isWhite, PieceType type, int x, int y) { - board[y][x]= new Piece (x, y, type, isWhite); + board[x][y]= new Piece (x, y, type, isWhite); } public void populateBoard() { @@ -51,6 +55,7 @@ public class Board { board[x][0]= new Piece(x, 0, backRowPieces[x], false); board[x][7]= new Piece(x, 7, backRowPieces[x], true); } + } @@ -79,6 +84,7 @@ public class Board { } return builder.toString(); + } @@ -95,101 +101,72 @@ public class Board { } public void userTouch(int x, int y) { - Piece clickedPiece=board[x][y]; - - - if(chosenPiece==null) { - // Select a piece if one is clicked - if (clickedPiece != null) { - chosenPiece = clickedPiece; - // Clear previous highlights - highlightedSquares.clear(); - // Highlight the new possible moves - addPossibleMoves(chosenPiece); - } - - } else { - // If a move is clicked, try to move - if (isHighlighted(x, y)) { - MovePiece movePiece = new MovePiece(chosenPiece, this); - boolean successfulMove = false; - - // Call the right move method based on the piece type - switch (chosenPiece.getType()) { - case Pawn: - successfulMove = movePiece.movePawn(x, y); - break; // (break means exit the loop) - case Rook: - successfulMove = movePiece.moveRook(x, y); - break; - case Bishop: - successfulMove = movePiece.moveBishop(x, y); - break; - case Knight: - successfulMove = movePiece.moveKnight(x, y); - break; - default: - System.out.println("Unknown Piece Type"); //if the piece.getType() returns something unexpected we get this message - - } - if (successfulMove) { - turnNumber++; - isTurnWhite = !isTurnWhite; - } - } + // Get the clicked piece from the board + Piece clickedPiece = getPiece(x, y); - // Unselect the piece and clear highlights - chosenPiece = null; - highlightedSquares.clear(); + if (clickedPiece != null) { + // Initialize the MovePiece with the clicked piece and the current board + MovePiece movePiece = new MovePiece(clickedPiece, this); + + // Call to add the possible moves + movePiece.addPossibleMoves(); } + } - repaint(); - + public void addHighlight(int x, int y) { + highlightedSquares.add(new int[]{x, y}); + repaint(); + } + + + public Piece getPiece(int x, int y) { + if (x >= 0 && x < 8 && y >= 0 && y < 8) { + return board[x][y]; // Assuming `board` is your 2D array of Pieces + } + return null; + } + + public ArrayList getHighlightedSquares() { + return highlightedSquares; + } + + + + public void refreshHighlights() { + super.repaint(); // Just call the original JPanel repaint } - private void addPossibleMoves(Piece piece) { - MovePiece movePiece = new MovePiece(piece, this); - int currentX = piece.getX(); - int currentY = piece.getY(); - - // Loop through the board to check possible moves - for (int x = 0; x < 8; x++) { - for (int y = 0; y < 8; y++) { - boolean canMove; - switch (piece.getType()) { - case Pawn: - movePiece.movePawn(x, y); - case Rook: - movePiece.moveRook(x, y); - case Bishop: - movePiece.moveBishop(x, y); - case Knight: - movePiece.moveKnight(x, y); - default: - canMove = false; - System.out.println("Unknown Piece Type"); - }; - - if (canMove) { - highlightedSquares.add(new int[]{x, y}); - } - } - } + public void clearHighlights() { + highlightedSquares.clear(); + repaint(); // This refreshes the board after clearing highlights + } + + + public void repaint() { + super.repaint(); // This calls the original JPanel repaint, not the same method again + } + + + + + public void revalidate() { + // TODO Auto-generated method stub + } - public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y; } - + /* public Piece getPiece(int x, int y) { //returns figure and gives coordinates if (x >= 0 && x < width && y >= 0 && y < height) { //checks if coordinates are inside the board return board[x][y]; //returns the piece which is currently on this coordinates } return null; } + */ public void movePiece(int oldX, int oldY, int newX, int newY) { if (board[oldX][oldY] != null) { //checks if there is a piece at that position @@ -200,6 +177,7 @@ public class Board { // Update its internal coordinates piece.setX(newX); piece.setY(newY); + this.repaint(); } } /* saving-loading feature :*/ diff --git a/src/backend/HighlightManager.java b/src/backend/HighlightManager.java new file mode 100644 index 0000000..cd024d5 --- /dev/null +++ b/src/backend/HighlightManager.java @@ -0,0 +1,161 @@ +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/MovePiece.java b/src/backend/MovePiece.java index a1bda7d..bcfba6c 100644 --- a/src/backend/MovePiece.java +++ b/src/backend/MovePiece.java @@ -4,10 +4,14 @@ public class MovePiece { private Piece piece; private Board board; + private HighlightManager highlightManager; + + public MovePiece(Piece piece, Board board) { this.piece = piece; this.board = board; + this.highlightManager = new HighlightManager(board); } //Pawn movement logic @@ -163,7 +167,22 @@ public class MovePiece { } return false; } + + + + public void addPossibleMoves() { + switch (piece.getType()) { + case Pawn:highlightManager.highlightPawnMoves(piece); + case Rook:highlightManager.highlightRookMoves(piece); + case Knight:highlightManager.highlightKnightMoves(piece); + case Bishop:highlightManager.highlightBishopMoves(piece); + case Queen:highlightManager.highlightQueenMoves(piece); + case King:highlightManager.highlightKingMoves(piece); + default:System.out.println("Unknown piece type"); + } + } } +