everything done until setPiece, the board showcases the piece

This commit is contained in:
Valentine GIRAL 2025-04-23 12:54:56 +02:00
parent 4c53382718
commit 13914dd390
1 changed files with 33 additions and 5 deletions

View File

@ -47,6 +47,8 @@ public class Board {
}
public void populateBoard() {
// Puts the board in its default configuration
// adding all necessary pieces at the expected starting locations
PieceType[] backRow = {
PieceType.Rook, PieceType.Knight, PieceType.Bishop,
PieceType.Queen, PieceType.King, PieceType.Bishop,
@ -71,6 +73,7 @@ public class Board {
}
public void cleanBoard() {
// removes all pieces from the board, readying it to add pieces for a new game
// Iterate over each row and column and set the board position to null
for (int y = 0; y < line; y++) {
for (int x = 0; x < col; x++) {
@ -81,15 +84,40 @@ public class Board {
}
public String toString() {
//TODO
return "";
// Returns a string representation of the board's current state
StringBuilder boardString = new StringBuilder(); // To construct the string representation
// Iterate through each row and column to build the string
for (int row = 0; row < line; row++) {
for (int col = 0; col < this.col; col++) {
Piece piece = board[row][col];
if (piece != null) {
boardString.append(piece.getType().getSummary()); // append the piece symbol
} else {
boardString.append("."); // the dot symbolizes empty space
}
if (col < this.col - 1) {
boardString.append(" "); // add space between columns
}
}
boardString.append("\n"); // new line after each row
}
return boardString.toString();
}
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
//TODO
return pieces;
// Iterate through the 2D array to gather all non-null pieces
for (int y = 0; y < line; y++) {
for (int x = 0; x < col; x++) {
Piece piece = board[y][x];
if (piece != null) {
pieces.add(piece); // Add the piece to the list if it's not null
}
}
}
return pieces; // return the list or array of pieces
}
public void userTouch(int x, int y) {