added new highlightedPossibleMoves alternative

This commit is contained in:
keshi 2025-05-23 09:33:58 +02:00
parent 0449c8220a
commit 36e09d74a2
3 changed files with 59 additions and 49 deletions

View File

@ -308,11 +308,48 @@ public class Board {
/* The following methods require more work ! */ /* The following methods require more work ! */
private void highlightedPossibleMoves(Piece piece) { /*private void highlightedPossibleMoves(Piece piece) {
highlightedSquares.clear(); highlightedSquares.clear();
MoveHighlighter highlighter= new MoveHighlighter(); MoveHighlighter highlighter= new MoveHighlighter();
highlightedSquares.addAll(highlighter.getPossibleMoves(piece, this)); 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) { 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

View File

@ -3,8 +3,8 @@ package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class MoveHighlighter { public class MoveHighlighter {
public ArrayList<int[]> getPossibleMoves(Piece piece, Board board){ public ArrayList<int[]> getPossibleMoves(Piece piece, Board board){ //checking every sqaure on the board
ArrayList<int[]> validMoves = new ArrayList<int[]>(); ArrayList<int[]> validMoves = new ArrayList<int[]>();//adding possible squares to list
MovePiece movement = new MovePiece(piece, board); MovePiece movement = new MovePiece(piece, board);
PieceType type= piece.getType(); PieceType type= piece.getType();
MovementCapabilities specialMoves= new MovementCapabilities(); MovementCapabilities specialMoves= new MovementCapabilities();

View File

@ -11,53 +11,27 @@ public class MovePiece {
} }
/*alternative /*alternative
* public boolean validateMove(int x, int y, boolean simulate) { public boolean moveKnight(int x, int y, boolean simulate) {
PieceType type = piece.getType(); int currentX = piece.getX();
switch(type) { int currentY = piece.getY();
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) { int[][] knightMoves = {
return validateMove(x, y, false); {2, 1}, {2, -1}, {-2, 1}, {-2, -1},
} {1, 2}, {1, -2}, {-1, 2}, {-1, -2}
};
public boolean moveKnightSimulate(int x, int y) { for (int[] move : knightMoves) {
return validateMove(x, y, true); if (currentX + move[0] == x && currentY + move[1] == y) {
} Piece targetPiece = board.getPiece(x, y);
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) {
private boolean validateKnightMove(int x, int y, boolean simulate) { if (!simulate) board.movePiece(currentX, currentY, x, y);
int currentX = piece.getX(); return true;
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; }
} */ return false;
}
} */
///Pawn movement logic ///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
@ -127,7 +101,6 @@ public class MovePiece {
return true; return true;
} }
} }
} }
return false; return false;
} }