added the highlight moves for enpassant and castling. going to try to
work on autoplayer or smth
This commit is contained in:
parent
9e69b29e91
commit
13873f52b4
|
|
@ -164,12 +164,22 @@ public class Board {
|
||||||
//checking the type of piece it is to move it
|
//checking the type of piece it is to move it
|
||||||
if(chosenPiece.getType()==PieceType.Pawn) {
|
if(chosenPiece.getType()==PieceType.Pawn) {
|
||||||
movementSuccess=movement.movePawn(x,y);
|
movementSuccess=movement.movePawn(x,y);
|
||||||
|
|
||||||
|
if(!movementSuccess) {
|
||||||
|
MovementCapabilities specialMoves= new MovementCapabilities();
|
||||||
|
movementSuccess= specialMoves.enPassant(chosenPiece, x, y, this);
|
||||||
|
}
|
||||||
} else if (chosenPiece.getType()==PieceType.Rook) {
|
} else if (chosenPiece.getType()==PieceType.Rook) {
|
||||||
movementSuccess=movement.moveRook(x, y);
|
movementSuccess=movement.moveRook(x, y);
|
||||||
}else if (chosenPiece.getType()==PieceType.Bishop) {
|
}else if (chosenPiece.getType()==PieceType.Bishop) {
|
||||||
movementSuccess=movement.moveBishop(x, y);
|
movementSuccess=movement.moveBishop(x, y);
|
||||||
}else if (chosenPiece.getType()==PieceType.King) {
|
}else if (chosenPiece.getType()==PieceType.King) {
|
||||||
movementSuccess=movement.moveKing(x, y);
|
movementSuccess=movement.moveKing(x, y);
|
||||||
|
|
||||||
|
if(!movementSuccess) {
|
||||||
|
MovementCapabilities specialMoves=new MovementCapabilities();
|
||||||
|
movementSuccess= specialMoves.castling(chosenPiece, x, y, this);
|
||||||
|
}
|
||||||
}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) {
|
}else if (chosenPiece.getType()==PieceType.Knight) {
|
||||||
|
|
@ -249,8 +259,8 @@ public class Board {
|
||||||
return out; //Give back the full array of text‐lines ready to be written to a file.
|
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:
|
// 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"
|
// fileRep[0] might be something like "WR,WN,WB,WQ,WK,WB,WN,WR"
|
||||||
// splitting on “,” gives 8 pieces, so width = 8.
|
// splitting on “,” gives 8 pieces, so width = 8.
|
||||||
|
|
|
||||||
|
|
@ -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,6 +7,7 @@ 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();
|
||||||
|
MovementCapabilities specialMoves= new MovementCapabilities();
|
||||||
|
|
||||||
//looping through each square space
|
//looping through each square space
|
||||||
for (int x = 0; x < board.getWidth(); x++) {
|
for (int x = 0; x < board.getWidth(); x++) {
|
||||||
|
|
@ -15,11 +16,13 @@ public class MoveHighlighter {
|
||||||
System.out.println("MoveHighlighter: checking (" + x + "," + y + ") for type " + type);
|
System.out.println("MoveHighlighter: checking (" + x + "," + y + ") for type " + type);
|
||||||
|
|
||||||
if(type==PieceType.Pawn) {
|
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) {
|
} else if(type==PieceType.Rook) {
|
||||||
valid=movement.moveRookSimulate(x, y);
|
valid=movement.moveRookSimulate(x, y);
|
||||||
}else if(type==PieceType.King) {
|
}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) {
|
}else if(type==PieceType.Queen) {
|
||||||
valid=movement.moveQueenSimulate(x, y);
|
valid=movement.moveQueenSimulate(x, y);
|
||||||
}else if(type==PieceType.Bishop) {
|
}else if(type==PieceType.Bishop) {
|
||||||
|
|
@ -28,6 +31,8 @@ public class MoveHighlighter {
|
||||||
valid=movement.moveKnightSimulate(x, y);
|
valid=movement.moveKnightSimulate(x, y);
|
||||||
}
|
}
|
||||||
if(valid) {
|
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});
|
validMoves.add(new int[] {x,y});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public class MovementCapabilities {
|
public class MovementCapabilities {
|
||||||
|
|
||||||
|
//en passant
|
||||||
public boolean enPassant(Piece piece, int x, int y, Board board) {
|
public boolean enPassant(Piece piece, int x, int y, Board board) {
|
||||||
if (piece.getType() != PieceType.Pawn) {
|
if (piece.getType() != PieceType.Pawn) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -17,7 +19,7 @@ public class MovementCapabilities {
|
||||||
if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y)== null) {
|
if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y)== null) {
|
||||||
Move last = board.getLastMove();
|
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) {
|
if (last != null) {
|
||||||
Piece lastMoved = last.getPieceMoved();
|
Piece lastMoved = last.getPieceMoved();
|
||||||
|
|
@ -49,6 +51,7 @@ public class MovementCapabilities {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//castling
|
||||||
public boolean castling(Piece king, int x, int y, Board board) {
|
public boolean castling(Piece king, int x, int y, Board board) {
|
||||||
if (king.getType()!= PieceType.King|| king.getDidMove()) {
|
if (king.getType()!= PieceType.King|| king.getDidMove()) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -58,7 +61,6 @@ public class MovementCapabilities {
|
||||||
int currentY = king.getY();
|
int currentY = king.getY();
|
||||||
boolean isWhite = king.isWhite(); // getting coordinates and color of the king
|
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
|
if (y != currentY) { // checking to see if the king has moved during the game yet
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -73,10 +75,8 @@ public class MovementCapabilities {
|
||||||
|
|
||||||
if (isRook && rookMoved) {
|
if (isRook && rookMoved) {
|
||||||
if (emptySpaceRight) {
|
if (emptySpaceRight) {
|
||||||
|
|
||||||
board.movePiece(currentX, currentY, 6, currentY); // moving the king to ...
|
board.movePiece(currentX, currentY, 6, currentY); // moving the king to ...
|
||||||
board.movePiece(7, currentY, 5, currentY); // moving the rook to ...
|
board.movePiece(7, currentY, 5, currentY); // moving the rook to ...
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -94,13 +94,9 @@ public class MovementCapabilities {
|
||||||
board.movePiece(currentX, currentY, 2, currentY); // moving king to...
|
board.movePiece(currentX, currentY, 2, currentY); // moving king to...
|
||||||
board.movePiece(0,currentY,3,currentY); // moving rook to...
|
board.movePiece(0,currentY,3,currentY); // moving rook to...
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue