From 13914dd3900c593c056de07e5e4fdae848f96f46 Mon Sep 17 00:00:00 2001 From: VALENTINE GIRAL Date: Wed, 23 Apr 2025 12:54:56 +0200 Subject: [PATCH] everything done until setPiece, the board showcases the piece --- src/backend/Board.java | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index c6f8ea3..4454735 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -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 getPieces() { ArrayList 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) {