Updated board class

This commit is contained in:
Lymeng LY 2025-05-09 10:58:33 +02:00
commit 301c3bb709
2 changed files with 81 additions and 69 deletions

View File

@ -9,8 +9,8 @@ public class Board {
private int [] selectedPosition = null; // No piece selected initially
private int turnNumber=0; // Tracks the number of turns elapsed
private boolean turnWhite=true; // True if it's White's turn, False if it's Black's turn
private ArrayList<int[]> highlightedPositions = new ArrayList<>();// List containing all board positions
private boolean isValidMove;
private ArrayList<int[]> highlightedPositions = new ArrayList<>(); // List containing all board positions
private ArrayList<MoveRecord> moveHistory = new ArrayList<>(); // List to store move history
private boolean inBounds(int x, int y) {
// Verify the bounds of the board
@ -175,29 +175,19 @@ public class Board {
if(selectedX == x && selectedY == y) {
selectedPosition = null;
highlightedPositions.clear(); //Unhighlight
return;
}
//To check if square valid
boolean valid = false;
for (int[] pos : highlightedPositions) {
if (pos[0]==x&&pos[1]==y) {
valid = true;
break;
}
// If a piece is selected and the user clicks a new position
else {
else {
// Check if the move is valid by checking if it exists in highlightedPositions
boolean isValidMove = false;
for (int[] pos1 : highlightedPositions) {
if (pos1[0] == x && pos1[1] == y) {
boolean isValidMove = false;
for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) {
isValidMove = true;
break;
}
}
}
// Only move the piece if the destination is valid
if (isValidMove) {
movePiece(selectedX, selectedY, x, y);
@ -209,25 +199,8 @@ public class Board {
// Deselect the position after moving
this.selectedPosition = null;
highlightedPositions.clear();//Clear after move
}
// If a piece is selected and the user clicks a new position
if (valid) {
movePiece(selectedX, selectedY, x, y);
//update turn
this.turnNumber++;
this.turnWhite=!this.turnWhite;
}
else {
System.out.println("Blocked! This is not a valid move.");
}
// Deselect the position after moving
this.selectedPosition = null;
highlightedPositions.clear();//Clear after move
}
}
}
@ -240,12 +213,23 @@ public class Board {
switch (p.getType()) {
case Pawn:
int dir = isWhite ? -1 : 1;
if (inBounds(x, y + dir) && board[y + dir][x] == null)
// Forward one square
if (inBounds(x, y + dir) && board[y + dir][x] == null) {
moves.add(new int[]{x, y + dir});
if (inBounds(x - 1, y + dir) && hasEnemy(x - 1, y + dir, isWhite))
// Initial two-square move
int startRow = isWhite ? 6 : 1; // Starting row for pawns
if (y == startRow && inBounds(x, y + 2*dir) && board[y + 2*dir][x] == null) {
moves.add(new int[]{x, y + 2*dir});
}
}
// Captures (diagonal)
if (inBounds(x - 1, y + dir) && hasEnemy(x - 1, y + dir, isWhite)) {
moves.add(new int[]{x - 1, y + dir});
if (inBounds(x + 1, y + dir) && hasEnemy(x + 1, y + dir, isWhite))
}
if (inBounds(x + 1, y + dir) && hasEnemy(x + 1, y + dir, isWhite)) {
moves.add(new int[]{x + 1, y + dir});
}
break;
case Rook:
@ -258,8 +242,7 @@ public class Board {
case Queen:
moves.addAll(lineMoves(x, y, isWhite, new int[][]{
{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}
}));
{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1} }));
break;
case Knight:
@ -273,6 +256,7 @@ public class Board {
break;
case King:
// Regular king moves
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue;
@ -289,26 +273,31 @@ public class Board {
public void movePiece(int fromX, int fromY, int toX, int toY) {
// Additional method: moves a piece from one position to another on the board
Piece movingPiece = board[fromY][fromX];
Piece capturedPiece = board[toY][toX];
// Get the piece at the source position
Piece piece = board[fromY][fromX];
// Record the move before making it
moveHistory.add(new MoveRecord(
movingPiece, capturedPiece,
fromX, fromY, toX, toY,
turnNumber, turnWhite));
if (piece != null) {
// Update the piece's internal position
piece.setX(toX);
piece.setY(toY);
// Move the piece on the board
board[toY][toX] = piece; // Place piece at the new position
board[toY][toX] = movingPiece; // Place piece at the new position
board[fromY][fromX] = null; // Clear the old position
}
if (movingPiece != null) {
// Update the piece's internal position
movingPiece.setX(toX);
movingPiece.setY(toY);
}
}
public boolean isSelected(int x, int y) {
return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y;
}
/* saving-loading feature :*/
public String[] toFileRep() {
ArrayList<String> fileLines = new ArrayList<>();
@ -379,29 +368,51 @@ public class Board {
}
public void undoLastMove() {
//TODO
// Check if there are moves to undo
if (moveHistory.isEmpty()) {
return; // Nothing to undo
}
// Get the last move
MoveRecord lastMove = moveHistory.remove(moveHistory.size() - 1);
// Restore the moved piece to its original position
board[lastMove.fromY][lastMove.fromX] = lastMove.movedPiece;
if (lastMove.movedPiece != null) {
lastMove.movedPiece.setX(lastMove.fromX);
lastMove.movedPiece.setY(lastMove.fromY);
}
// Restore the captured piece if any
board[lastMove.toY][lastMove.toX] = lastMove.capturedPiece;
if (lastMove.capturedPiece != null) {
lastMove.capturedPiece.setX(lastMove.toX);
lastMove.capturedPiece.setY(lastMove.toY);
}
// Restore game state
turnNumber = lastMove.turnNumberAtMove;
turnWhite = lastMove.turnWhiteAtMove;
// Clear any selections or highlights
selectedPosition = null;
highlightedPositions.clear();
}
public Board(Board board) {
//copy the board size
this.col = board.col; //number of column
this.line = board.line; //number of row
this.turnNumber = board.turnNumber;
this.turnWhite = board.turnWhite;
//copy the selected positions
this.selectedPosition = board.selectedPosition == null ? null:
new int[] {board.selectedPosition[0], board.selectedPosition[1]};
//Deep copy highlighted positions
this.highlightedPositions = new ArrayList<>();
for (int[] pos : highlightedPositions) {
}
this.col = board.col; //number of column
this.line = board.line; //number of row
this.turnNumber = board.turnNumber;
this.turnWhite = board.turnWhite;
//copy the selected positions
this.selectedPosition = board.selectedPosition == null ? null:
new int[] {board.selectedPosition[0], board.selectedPosition[1]};
//Deep copy highlighted positions
this.highlightedPositions = new ArrayList<>();
for (int[] pos : highlightedPositions) {
//TO BE DONE
}
}

View File

@ -1,6 +1,7 @@
package backend;
public class MoveRecord {
//record the piece last move
Piece movedPiece;
Piece capturedPiece;
int fromX, fromY, toX, toY;