remove marietta's edit to try highlight again

This commit is contained in:
keshi 2025-05-11 16:08:57 +02:00
commit 811d9a28d5
5 changed files with 225 additions and 21 deletions

View File

@ -9,7 +9,7 @@ public class Board {
private Piece chosenPiece=null; private Piece chosenPiece=null;
private int turnNumber=0; private int turnNumber=0;
private boolean isTurnWhite=true; private boolean isTurnWhite=true;
private ArrayList<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board position // private ArrayList<int[]> highlightedSquares= new ArrayList<>();
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width=colNum; this.width=colNum;
@ -167,14 +167,9 @@ public class Board {
/* The following methods require more work ! */ /* The following methods require more work ! */
public boolean isHighlighted(int x, int y) { //loop through list of highlighted squares public boolean isHighlighted(int x, int y) {
for(int i=0;i<highlightedSquares.size();i++) { //gets the current position of the piece at (x,y) //return highlightedSquares.contains(Point(x,y));
int[] position = highlightedSquares.get(i); return true; }
if(position[0]==x && position[1]==y) { //checking if position matches coordinates
return true;
}
}
return false; }
public void undoLastMove() { public void undoLastMove() {
@ -193,4 +188,3 @@ public class Board {
} }
} }

View File

@ -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();
}
}
*/

View File

@ -4,10 +4,14 @@ public class MovePiece {
private Piece piece; private Piece piece;
private Board board; private Board board;
// private HighlightManager highlightManager;
public MovePiece(Piece piece, Board board) { public MovePiece(Piece piece, Board board) {
this.piece = piece; this.piece = piece;
this.board = board; this.board = board;
// this.highlightManager = new HighlightManager(board);
} }
//Pawn movement logic //Pawn movement logic
@ -166,6 +170,21 @@ public class MovePiece {
} }
/* 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");
}
}
}
*/

View File

@ -25,3 +25,42 @@ 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
}
}
}
}
*/

View File

@ -1,9 +0,0 @@
public class random_file_to_delete {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}