i need to modify highlight

This commit is contained in:
keshi 2025-05-16 08:13:43 +02:00
parent 96fe108557
commit 9dccb10d5f
3 changed files with 187 additions and 80 deletions

View File

@ -121,6 +121,8 @@ public class Board {
movementSuccess=movement.moveKing(x, y); movementSuccess=movement.moveKing(x, y);
}else if (chosenPiece.getType()==PieceType.Queen) { }else if (chosenPiece.getType()==PieceType.Queen) {
movementSuccess=movement.moveQueen(x, y); movementSuccess=movement.moveQueen(x, y);
}else if (chosenPiece.getType()==PieceType.Knight) {
movementSuccess=movement.moveKnight(x, y);
} }
if(movementSuccess) { if(movementSuccess) {
chosenPiece=null; chosenPiece=null;

View File

@ -7,23 +7,23 @@ 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();
//looping through each square space
for(int x=0; x<board.getHeight();x++) { //looping through each square space for (int x = 0; x < board.getWidth(); x++) {
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.movePawnSimulate(x, y); valid=movement.movePawnSimulate(x, y);
} else if(type==PieceType.Rook) { } else if(type==PieceType.Rook) {
valid=movement.moveRook(x, y); valid=movement.moveRookSimulate(x, y);
}else if(type==PieceType.King) { }else if(type==PieceType.King) {
valid=movement.moveKing(x, y); valid=movement.moveKingSimulate(x, y);
}else if(type==PieceType.Queen) { }else if(type==PieceType.Queen) {
valid=movement.moveQueen(x, y); valid=movement.moveQueenSimulate(x, y);
}else if(type==PieceType.Bishop) { }else if(type==PieceType.Bishop) {
valid=movement.moveBishop(x, y); valid=movement.moveBishopSimulate(x, y);
}else if(type==PieceType.Knight) { }else if(type==PieceType.Knight) {
valid=movement.moveKnight(x, y); valid=movement.moveKnightSimulate(x, y);
} }
if(valid) { if(valid) {
validMoves.add(new int[] {x,y}); validMoves.add(new int[] {x,y});

View File

@ -4,42 +4,80 @@ 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 /* 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;
}
}
}
return false;
} */
///Pawn movement logic
public boolean movePawn(int x, int y) { //method definition for pawn public boolean movePawn(int x, int y) { //method definition for pawn
int currentX = piece.getX(); // we call the method getX to get x coordinates int currentX = piece.getX(); // we call the method getX to get x coordinates
int currentY = piece.getY(); // we call the method getY to get y coordinates int currentY = piece.getY(); // we call the method getY to get y coordinates
boolean isWhite = piece.isWhite(); // checks if the pawn is white or black boolean isWhite = piece.isWhite(); // checks if the pawn is white or black
int direction = isWhite ? -1 : 1; // we check, if its white we have -1 as it goes down if false we have black then we have 1 int direction = isWhite ? -1 : 1; // we check, if its white we have -1 as it goes down if false we have black then we have 1
boolean firstMove = isWhite ? currentY == 6 : currentY == 1; // checks if its the first move. are the pawns still on their lines? boolean firstMove = isWhite ? currentY == 6 : currentY == 1; // checks if its the first move. are the pawns still on their lines?
if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) { // we go one step forward if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) { // we go one step forward
board.movePiece(currentX, currentY, x, y); board.movePiece(currentX, currentY, x, y);
return true; return true;
} }else if (x == currentX && y == currentY + (2 * direction) && firstMove && board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) { // we go two steps forward bcs its the start of the game
else if (x == currentX && y == currentY + (2 * direction) && firstMove && board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) { // we go two steps forward bcs its the start of the game
//and we ensure that there really is nothing in between the original position and the 2nd step //and we ensure that there really is nothing in between the original position and the 2nd step
board.movePiece(currentX, currentY, x, y); board.movePiece(currentX, currentY, x, y);
return true; return true;
} }
// Note: In a full version, you should also check the square in between is empty (e.g., board.getPiece(x, currentY + direction) == null) otherwise the pawn could "jump over" a piece
// Note: In a full version, you should also check the square in between is empty (e.g., board.getPiece(x, currentY + direction) == null) otherwise the pawn could "jump over" a piece!
else if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) { else if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
board.movePiece(currentX, currentY, x, y); // we go diagonally and capture where another black piece is placed board.movePiece(currentX, currentY, x, y); // we go diagonally and capture where another black piece is placed
return true; return true;
} }
return false; return false;
} }
@ -62,42 +100,6 @@ public class MovePiece {
} }
return false; //if not it was unsuccessful return false; //if not it was unsuccessful
} }
// Path Clearance Logic for Rook and Bishop
private boolean isPathClear(int currentX, int currentY, int targetX, int targetY) { //method to check if the path is clear
if (currentX == targetX) { //checks if it moves vertically
int step = (targetY > currentY) ? 1 : -1; // decides the direction of the step
for (int y = currentY + step; y != targetY; y += step) { // we have a loop in here
if (board.getPiece(currentX, y) != null) {
return false; // Blocked path
}
}
}
else if (currentY == targetY) { //checks if it moves horizontally
int step = (targetX > currentX) ? 1 : -1;
for (int x = currentX + step; x != targetX; x += step) {
if (board.getPiece(x, currentY) != null) {
return false; // Blocked path
}
}
} else if (Math.abs(currentX - targetX) == Math.abs(currentY - targetY)) {
int stepX = (targetX > currentX) ? 1 : -1;
int stepY = (targetY > currentY) ? 1 : -1;
int x = currentX + stepX;
int y = currentY + stepY;
while (x != targetX && y != targetY) {
if (board.getPiece(x, y) != null) {
return false;
}
x += stepX;
y += stepY;
}
}
return true; // No obstacles found
}
//Knight movement logic //Knight movement logic
public boolean moveKnight(int x, int y) { public boolean moveKnight(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -120,7 +122,6 @@ public class MovePiece {
} }
return false; return false;
} }
// Bishop Movement Logic // Bishop Movement Logic
public boolean moveBishop(int x, int y) { public boolean moveBishop(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -142,7 +143,7 @@ public class MovePiece {
boolean Diagonal = Math.abs(currentX - x)== Math.abs(currentY-y); //diagonal spaces identified boolean Diagonal = Math.abs(currentX - x)== Math.abs(currentY-y); //diagonal spaces identified
boolean Straight= currentX == x || currentY == y; //horizontal or vertical spaces identified boolean Straight= currentX == x || currentY == y; //horizontal or vertical spaces identified
if((Diagonal || Straight) && isPathClear(currentX, currentY, x, y)) { //if either space is available and clear if((Diagonal || Straight) && isPathClear(currentX, currentY, x, y)) { //if either space is available and clear
Piece targetPiece= board.getPiece(currentX, currentY); //choose the empty space Piece targetPiece= board.getPiece(x, y); //choose the empty space
if(targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move piece to null space or capture opposite colour piece if(targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move piece to null space or capture opposite colour piece
board.movePiece(currentX, currentY, x, y); board.movePiece(currentX, currentY, x, y);
return true; return true;
@ -167,7 +168,45 @@ public class MovePiece {
} }
return false; return false;
} }
//Pawn movement logic simulation: only checking where the piece can go to avoid changing the game state
// Path Clearance Logic for Rook and Bishop
private boolean isPathClear(int currentX, int currentY, int targetX, int targetY) { //method to check if the path is clear
if (currentX == targetX) { //checks if it moves vertically
int step = (targetY > currentY) ? 1 : -1; // decides the direction of the step
for (int y = currentY + step; y != targetY; y += step) { // we have a loop in here
if (board.getPiece(currentX, y) != null) {
return false; // Blocked path
}
}
}
else if (currentY == targetY) { //checks if it moves horizontally
int step = (targetX > currentX) ? 1 : -1;
for (int x = currentX + step; x != targetX; x += step) {
if (board.getPiece(x, currentY) != null) {
return false; // Blocked path
}
}
} else if (Math.abs(currentX - targetX) == Math.abs(currentY - targetY)) {
int stepX = (targetX > currentX) ? 1 : -1;
int stepY = (targetY > currentY) ? 1 : -1;
int x = currentX + stepX;
int y = currentY + stepY;
while (x != targetX && y != targetY) {
if (board.getPiece(x, y) != null) {
return false;
}
x += stepX;
y += stepY;
}
}
return true; // No obstacles found
}
//adding simulations part for the highlighting the pre-moves that can be performed(: only checking where the piece can go to avoid changing the game state)
//Pawn movement logic simulation
public boolean movePawnSimulate(int x, int y) { public boolean movePawnSimulate(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
int currentY = piece.getY(); int currentY = piece.getY();
@ -178,35 +217,101 @@ public class MovePiece {
// Same logic as movePawn but no actual move // Same logic as movePawn but no actual move
if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) { if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) {
return true; return true;
} else if (x == currentX && y == currentY + (2 * direction) && firstMove && }else if (x == currentX && y == currentY + (2 * direction) && firstMove &&
board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) { board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) {
return true; return true;
} else if (Math.abs(x - currentX) == 1 && y == currentY + direction && }else if (Math.abs(x - currentX) == 1 && y == currentY + direction &&
board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) { board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
return true; return true;
}return false;
}
//bishop movement simulate
public boolean moveBishopSimulate(int x, int y) {
int currentX = piece.getX();
int currentY = piece.getY();
if (Math.abs(currentX - x) == Math.abs(currentY - y) && isPathClear(currentX, currentY, x, y)) {
Piece targetPiece = board.getPiece(x, y);
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) {
return true;
}
}
return false;
}
//Rook movement simulate
public boolean moveRookSimulate(int x, int y) {
int currentX = piece.getX();
int currentY = piece.getY();
if (currentX == x || currentY == y) // Check if the move is either in the same row or the same column
if (isPathClear(currentX, currentY, x, y)) { //Check if there are obstacles in the path
Piece targetPiece = board.getPiece(x, y);
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //Check if the target is empty or an enemy piece
return true; //move was successful
}
}
return false; //if not it was unsuccessful
}
//Knight movement simulate
public boolean moveKnightSimulate(int x, int y) {
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()) {
return true;
}
}
}
return false;
}
//Queen movement simulate
public boolean moveQueenSimulate(int x, int y) {
int currentX=piece.getX();
int currentY=piece.getY();
boolean Diagonal = Math.abs(currentX - x)== Math.abs(currentY-y); //diagonal spaces identified
boolean Straight= currentX == x || currentY == y; //horizontal or vertical spaces identified
if((Diagonal || Straight) && isPathClear(currentX, currentY, x, y)) { //if either space is available and clear
Piece targetPiece= board.getPiece(x, y); //choose the empty space
if(targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move piece to null space or capture opposite colour piece
return true;
}
}
return false;
}
//King movement simulate
public boolean moveKingSimulate(int x, int y) {
int currentX = piece.getX();
int currentY = piece.getY();
int stepX = Math.abs(x - currentX); //spaces moved horizontally
int stepY = Math.abs(y - currentY);//spaces moved vertically
if ((stepX <= 1 && stepY <= 1) && !(stepX == 0 && stepY == 0)) { //moving 1 space in any direction
Piece targetPiece = board.getPiece(x, y);
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move is space is empty o
return true;
}
} }
return false; 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");
}
}
}
*/