Compare commits
4 Commits
ce7ef10eb8
...
7509196100
| Author | SHA1 | Date |
|---|---|---|
|
|
7509196100 | |
|
|
df56940b07 | |
|
|
59569de160 | |
|
|
13873f52b4 |
|
|
@ -164,12 +164,22 @@ public class Board {
|
|||
//checking the type of piece it is to move it
|
||||
if(chosenPiece.getType()==PieceType.Pawn) {
|
||||
movementSuccess=movement.movePawn(x,y);
|
||||
|
||||
if(!movementSuccess) { //in case the initial pawn move fails or cannot be done, enPassant is checked
|
||||
MovementCapabilities specialMoves= new MovementCapabilities();
|
||||
movementSuccess= specialMoves.enPassant(chosenPiece, x, y, this);
|
||||
}
|
||||
} 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);
|
||||
|
||||
if(!movementSuccess) { //in case the initial pawn move fails or cannot be done, castling is checked
|
||||
MovementCapabilities specialMoves=new MovementCapabilities();
|
||||
movementSuccess= specialMoves.castling(chosenPiece, x, y, this);
|
||||
}
|
||||
}else if (chosenPiece.getType()==PieceType.Queen) {
|
||||
movementSuccess=movement.moveQueen(x, y);
|
||||
}else if (chosenPiece.getType()==PieceType.Knight) {
|
||||
|
|
@ -249,12 +259,11 @@ public class Board {
|
|||
return out; //Give back the full array of text‐lines ready to be written to a file.
|
||||
}
|
||||
|
||||
public Board(String[] fileRep) {
|
||||
|
||||
public Board(String[] fileRep) {
|
||||
// Figure out how wide the board is by splitting line 0 on commas:
|
||||
// fileRep[0] might be something like "WR,WN,WB,WQ,WK,WB,WN,WR"
|
||||
// splitting on “,” gives 8 pieces, so width = 8.
|
||||
|
||||
this.width = fileRep[0].split(",").length;
|
||||
|
||||
//The number of rows is the number of lines minus the final turn‐line:
|
||||
|
|
@ -293,7 +302,6 @@ public class Board {
|
|||
this.isTurnWhite = fileRep[height].equals("W");
|
||||
this.previousStates = new ArrayList<>();
|
||||
this.highlightedSquares= new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,161 +0,0 @@
|
|||
/*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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -7,19 +7,22 @@ public class MoveHighlighter {
|
|||
ArrayList<int[]> validMoves = new ArrayList<int[]>();
|
||||
MovePiece movement = new MovePiece(piece, board);
|
||||
PieceType type= piece.getType();
|
||||
MovementCapabilities specialMoves= new MovementCapabilities();
|
||||
|
||||
//looping through each square space
|
||||
for (int x = 0; x < board.getWidth(); x++) {
|
||||
for (int y = 0; y < board.getHeight(); y++) {
|
||||
boolean valid=false;
|
||||
System.out.println("MoveHighlighter: checking (" + x + "," + y + ") for type " + type);
|
||||
//System.out.println("MoveHighlighter: checking (" + x + "," + y + ") for type " + type);
|
||||
|
||||
if(type==PieceType.Pawn) {
|
||||
valid=movement.movePawnSimulate(x, y);
|
||||
valid=movement.movePawnSimulate(x, y)|| specialMoves.enPassant(piece, x, y, board);;
|
||||
} else if(type==PieceType.Rook) {
|
||||
valid=movement.moveRookSimulate(x, y);
|
||||
}else if(type==PieceType.King) {
|
||||
valid=movement.moveKingSimulate(x, y);
|
||||
valid=movement.moveKingSimulate(x, y)|| specialMoves.castling(piece, x, y, new Board(board));
|
||||
//Because castling moves two pieces (king and rook), and you don’t want the highlight simulation to affect your real board.
|
||||
//Creating a temporary copy (new Board(board)) allows us to test if castling would work without breaking anything.
|
||||
}else if(type==PieceType.Queen) {
|
||||
valid=movement.moveQueenSimulate(x, y);
|
||||
}else if(type==PieceType.Bishop) {
|
||||
|
|
@ -28,6 +31,8 @@ public class MoveHighlighter {
|
|||
valid=movement.moveKnightSimulate(x, y);
|
||||
}
|
||||
if(valid) {
|
||||
System.out.println("✅ Valid move for " + type + " at (" + piece.getX() + "," + piece.getY() + ") → (" + x + "," + y + ")");
|
||||
System.out.println("Highlighting move to (" + x + "," + y + ") for " + type);
|
||||
validMoves.add(new int[] {x,y});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package backend;
|
||||
|
||||
public class MovementCapabilities {
|
||||
|
||||
//en passant
|
||||
public boolean enPassant(Piece piece, int x, int y, Board board) {
|
||||
if (piece.getType() != PieceType.Pawn) {
|
||||
return false;
|
||||
|
|
@ -17,7 +19,7 @@ public class MovementCapabilities {
|
|||
if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y)== null) {
|
||||
Move last = board.getLastMove();
|
||||
|
||||
System.out.println("Last move is " + last); // Keeping record of the last move the
|
||||
System.out.println("Last move is " + last); // Keeping record of the last move
|
||||
|
||||
if (last != null) {
|
||||
Piece lastMoved = last.getPieceMoved();
|
||||
|
|
@ -49,6 +51,7 @@ public class MovementCapabilities {
|
|||
}
|
||||
|
||||
|
||||
//castling
|
||||
public boolean castling(Piece king, int x, int y, Board board) {
|
||||
if (king.getType()!= PieceType.King|| king.getDidMove()) {
|
||||
return false;
|
||||
|
|
@ -58,7 +61,6 @@ public class MovementCapabilities {
|
|||
int currentY = king.getY();
|
||||
boolean isWhite = king.isWhite(); // getting coordinates and color of the king
|
||||
|
||||
|
||||
if (y != currentY) { // checking to see if the king has moved during the game yet
|
||||
return false;
|
||||
}
|
||||
|
|
@ -73,10 +75,8 @@ public class MovementCapabilities {
|
|||
|
||||
if (isRook && rookMoved) {
|
||||
if (emptySpaceRight) {
|
||||
|
||||
board.movePiece(currentX, currentY, 6, currentY); // moving the king to ...
|
||||
board.movePiece(7, currentY, 5, currentY); // moving the rook to ...
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -94,13 +94,9 @@ public class MovementCapabilities {
|
|||
board.movePiece(currentX, currentY, 2, currentY); // moving king to...
|
||||
board.movePiece(0,currentY,3,currentY); // moving rook to...
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue