i got the highlight to work by the miracle of god

This commit is contained in:
keshi 2025-05-11 16:46:32 +02:00
parent c54e62078f
commit 96fe108557
4 changed files with 37 additions and 3 deletions

View File

@ -29,7 +29,8 @@ public class Main {
//getting the pieces on the board //getting the pieces on the board
ArrayList<Piece> allPieces=testBoard.getPieces(); ArrayList<Piece> allPieces=testBoard.getPieces();
for (Piece piece:allPieces) { for (int i=0;i<allPieces.size();i++) {
Piece piece=allPieces.get(i);
System.out.println((piece.isWhite() ? "White ":"Black ")+ piece.getType().getSummary()+" located at "+ piece.getX()+ "," + piece.getY()); System.out.println((piece.isWhite() ? "White ":"Black ")+ piece.getType().getSummary()+" located at "+ piece.getX()+ "," + piece.getY());
} }

View File

@ -100,11 +100,13 @@ public class Board {
//when no piece is selected //when no piece is selected
if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) {
chosenPiece=clickedPiece; chosenPiece=clickedPiece;
highlightedPossibleMoves(clickedPiece);
} }
} }
else { //if a piece is already selected else { //if a piece is already selected
if(isSelected(x,y)) { if(isSelected(x,y)) {
chosenPiece=null; //unselect the chosen piece to revert to normal state chosenPiece=null; //unselect the chosen piece to revert to normal state
highlightedSquares.clear();
} }
else { //move selected piece to new position using MovePiece logics else { //move selected piece to new position using MovePiece logics
MovePiece movement= new MovePiece(chosenPiece, this); MovePiece movement= new MovePiece(chosenPiece, this);
@ -122,6 +124,7 @@ public class Board {
} }
if(movementSuccess) { if(movementSuccess) {
chosenPiece=null; chosenPiece=null;
highlightedSquares.clear();
turnNumber++; turnNumber++;
isTurnWhite=! isTurnWhite; isTurnWhite=! isTurnWhite;
} else { //invalid move } else { //invalid move
@ -167,6 +170,11 @@ public class Board {
/* The following methods require more work ! */ /* The following methods require more work ! */
private void highlightedPossibleMoves(Piece piece) {
highlightedSquares.clear();
highlightedSquares.addAll(MoveHighlighter.getPossibleMoves(piece, this));
}
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
for(int i=0; i<highlightedSquares.size();i++) { //loops through list of highlighted squares for(int i=0; i<highlightedSquares.size();i++) { //loops through list of highlighted squares
int[] position= highlightedSquares.get(i); //gets position of highlighted square int[] position= highlightedSquares.get(i); //gets position of highlighted square

View File

@ -7,11 +7,13 @@ public class MoveHighlighter {
ArrayList<int[]> validMoves = new ArrayList<int[]>(); ArrayList<int[]> validMoves = new ArrayList<int[]>();
MovePiece movement = new MovePiece(piece, board); MovePiece movement = new MovePiece(piece, board);
PieceType type= piece.getType(); PieceType type= piece.getType();
for(int x=0; x<board.getHeight();x++) {
for(int x=0; x<board.getHeight();x++) { //looping through each square space
for(int y=0; y<board.getHeight();y++) { for(int y=0; y<board.getHeight();y++) {
boolean valid=false; boolean valid=false;
if(type==PieceType.Pawn) { if(type==PieceType.Pawn) {
valid=movement.movePawn(x, y); valid=movement.movePawnSimulate(x, y);
} else if(type==PieceType.Rook) { } else if(type==PieceType.Rook) {
valid=movement.moveRook(x, y); valid=movement.moveRook(x, y);
}else if(type==PieceType.King) { }else if(type==PieceType.King) {

View File

@ -167,7 +167,30 @@ public class MovePiece {
} }
return false; return false;
} }
//Pawn movement logic simulation: only checking where the piece can go to avoid changing the game state
public boolean movePawnSimulate(int x, int y) {
int currentX = piece.getX();
int currentY = piece.getY();
boolean isWhite = piece.isWhite();
int direction = isWhite ? -1 : 1;
boolean firstMove = isWhite ? currentY == 6 : currentY == 1;
// Same logic as movePawn but no actual move
if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) {
return true;
} else if (x == currentX && y == currentY + (2 * direction) && firstMove &&
board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) {
return true;
} else if (Math.abs(x - currentX) == 1 && y == currentY + direction &&
board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
return true;
} }
return false;
}
}
/* public void addPossibleMoves() { /* public void addPossibleMoves() {