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 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 ArrayList<MoveRecord> moveHistory = new ArrayList<>(); // Stack to store move history
private ArrayList<int[]> highlightedPositions = new ArrayList<>();// List containing all board positions
private boolean isValidMove;
private boolean inBounds(int x, int y) {
// Verify the bounds of the board
@ -175,19 +175,29 @@ 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[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) {
boolean isValidMove = false;
for (int[] pos1 : highlightedPositions) {
if (pos1[0] == x && pos1[1] == y) {
isValidMove = true;
break;
}
}
}
// Only move the piece if the destination is valid
if (isValidMove) {
movePiece(selectedX, selectedY, x, y);
@ -199,8 +209,25 @@ 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
}
}
@ -213,23 +240,12 @@ public class Board {
switch (p.getType()) {
case Pawn:
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});
// 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)) {
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:
@ -242,7 +258,8 @@ 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:
@ -256,7 +273,6 @@ 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;
@ -273,31 +289,26 @@ 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];
// Record the move before making it
moveHistory.add(new MoveRecord(
movingPiece, capturedPiece,
fromX, fromY, toX, toY,
turnNumber, turnWhite));
// Get the piece at the source position
Piece piece = board[fromY][fromX];
// Move the piece on the board
board[toY][toX] = movingPiece; // Place piece at the new position
board[fromY][fromX] = null; // Clear the old position
if (piece != null) {
// Update the piece's internal position
piece.setX(toX);
piece.setY(toY);
if (movingPiece != null) {
// Update the piece's internal position
movingPiece.setX(toX);
movingPiece.setY(toY);
}
// Move the piece on the board
board[toY][toX] = piece; // Place piece at the new position
board[fromY][fromX] = null; // Clear the old position
}
}
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<>();
@ -368,39 +379,29 @@ public class Board {
}
public void undoLastMove() {
// 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();
//TODO
}
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) {
}
}