Compare commits

...

6 Commits

2 changed files with 105 additions and 70 deletions

34
Test.board Normal file
View File

@ -0,0 +1,34 @@
8,white
8,8
Rook,0,0,B
Knight,1,0,B
Bishop,2,0,B
Queen,3,0,B
King,4,0,B
Bishop,5,0,B
Knight,6,0,B
Rook,7,0,B
Pawn,0,1,B
Pawn,1,1,B
Pawn,2,1,B
Pawn,4,1,B
Pawn,5,1,B
Pawn,6,2,B
Pawn,7,2,B
Pawn,3,3,B
Pawn,3,4,W
Pawn,7,5,W
Pawn,0,6,W
Pawn,1,6,W
Pawn,2,6,W
Pawn,4,6,W
Pawn,5,6,W
Pawn,6,6,W
Rook,7,6,W
Rook,0,7,W
Knight,1,7,W
Bishop,2,7,W
Queen,3,7,W
King,4,7,W
Bishop,5,7,W
Knight,6,7,W

View File

@ -9,8 +9,8 @@ public class Board {
private int [] selectedPosition = null; // No piece selected initially private int [] selectedPosition = null; // No piece selected initially
private int turnNumber=0; // Tracks the number of turns elapsed 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 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 ArrayList<int[]> highlightedPositions = new ArrayList<>();// List containing all board positions
private ArrayList<MoveRecord> moveHistory = new ArrayList<>(); // Stack to store move history private boolean isValidMove;
private boolean inBounds(int x, int y) { private boolean inBounds(int x, int y) {
// Verify the bounds of the board // Verify the bounds of the board
@ -175,19 +175,29 @@ public class Board {
if(selectedX == x && selectedY == y) { if(selectedX == x && selectedY == y) {
selectedPosition = null; selectedPosition = null;
highlightedPositions.clear(); //Unhighlight 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 // 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 // Check if the move is valid by checking if it exists in highlightedPositions
boolean isValidMove = false; boolean isValidMove = false;
for (int[] pos : highlightedPositions) { for (int[] pos1 : highlightedPositions) {
if (pos[0] == x && pos[1] == y) { if (pos1[0] == x && pos1[1] == y) {
isValidMove = true; isValidMove = true;
break; break;
} }
} }
}
// Only move the piece if the destination is valid // Only move the piece if the destination is valid
if (isValidMove) { if (isValidMove) {
movePiece(selectedX, selectedY, x, y); movePiece(selectedX, selectedY, x, y);
@ -199,8 +209,25 @@ public class Board {
// Deselect the position after moving // Deselect the position after moving
this.selectedPosition = null; this.selectedPosition = null;
highlightedPositions.clear();//Clear after move 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
}
} }
@ -213,23 +240,12 @@ public class Board {
switch (p.getType()) { switch (p.getType()) {
case Pawn: case Pawn:
int dir = isWhite ? -1 : 1; int dir = isWhite ? -1 : 1;
// Forward one square if (inBounds(x, y + dir) && board[y + dir][x] == null)
if (inBounds(x, y + dir) && board[y + dir][x] == null) {
moves.add(new int[]{x, y + dir}); 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}); 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}); moves.add(new int[]{x + 1, y + dir});
}
break; break;
case Rook: case Rook:
@ -242,7 +258,8 @@ public class Board {
case Queen: case Queen:
moves.addAll(lineMoves(x, y, isWhite, new int[][]{ 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; break;
case Knight: case Knight:
@ -256,7 +273,6 @@ public class Board {
break; break;
case King: case King:
// Regular king moves
for (int dx = -1; dx <= 1; dx++) { for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) { for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue; if (dx == 0 && dy == 0) continue;
@ -273,31 +289,26 @@ public class Board {
public void movePiece(int fromX, int fromY, int toX, int toY) { public void movePiece(int fromX, int fromY, int toX, int toY) {
// Additional method: moves a piece from one position to another on the board // Additional method: moves a piece from one position to another on the board
Piece movingPiece = board[fromY][fromX];
Piece capturedPiece = board[toY][toX];
// Record the move before making it // Get the piece at the source position
moveHistory.add(new MoveRecord( Piece piece = board[fromY][fromX];
movingPiece, capturedPiece,
fromX, fromY, toX, toY,
turnNumber, turnWhite));
// Move the piece on the board if (piece != null) {
board[toY][toX] = movingPiece; // Place piece at the new position // Update the piece's internal position
board[fromY][fromX] = null; // Clear the old position piece.setX(toX);
piece.setY(toY);
if (movingPiece != null) { // Move the piece on the board
// Update the piece's internal position board[toY][toX] = piece; // Place piece at the new position
movingPiece.setX(toX); board[fromY][fromX] = null; // Clear the old position
movingPiece.setY(toY); }
}
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y; return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y;
} }
/* saving-loading feature :*/ /* saving-loading feature :*/
public String[] toFileRep() { public String[] toFileRep() {
ArrayList<String> fileLines = new ArrayList<>(); ArrayList<String> fileLines = new ArrayList<>();
@ -368,39 +379,29 @@ public class Board {
} }
public void undoLastMove() { public void undoLastMove() {
// Check if there are moves to undo //TODO
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) { public Board(Board board) {
//TODO //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) {
}
} }