diff --git a/src/backend/Board.java b/src/backend/Board.java index b191471..caed3c2 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -222,11 +222,11 @@ public class Board { /* saving-loading feature :*/ public String[] toFileRep() { - String[] out = new String[height + 1]; - for (int y = 0; y < height; y++) { - StringBuilder row = new StringBuilder(); - for (int x = 0; x < width; x++) { - Piece p = board[x][y]; + String[] out = new String[height + 1]; // Create an output array with one String per board‐row plus one for the turn line + for (int y = 0; y < height; y++) { // // For each row y = 0 … height-1 + StringBuilder row = new StringBuilder(); // We’ll build this row’s CSV (Comma-Separated Values) line in a StringBuilder + for (int x = 0; x < width; x++) { // // For each column x = 0 … width-1 + Piece p = board[x][y]; //Grab whatever piece (or null) lives at board[x][y] if (p == null) { row.append("--"); } else { @@ -240,29 +240,38 @@ public class Board { case Pawn: row.append('P'); break; } } - if (x < width - 1) row.append(','); + if (x < width - 1) row.append(','); // After each cell except the last in the row, add a comma } - out[y] = row.toString(); + out[y] = row.toString(); // Convert our StringBuilder into a String and store it as line y } // whose turn next out[height] = isTurnWhite ? "W" : "B"; - return out; + return out; //Give back the full array of text‐lines ready to be written to a file. } public Board(String[] fileRep) { - // derive dimensions + + // Figure out how wide the board is by splitting line 0 on commas: + // fileRep[0] might be something like "WR,WN,WB,WQ,WK,WB,WN,WR" + // splitting on “,” gives 8 pieces, so width = 8. + this.width = fileRep[0].split(",").length; - this.height = fileRep.length - 1; - this.board = new Piece[width][height]; + + //The number of rows is the number of lines minus the final turn‐line: + // fileRep.length is 9 for an 8×8 board (8 rows + 1 turn line), so height = 8. + + this.height = fileRep.length - 1; + + this.board = new Piece[width][height]; // Allocate the 2D array to hold Piece references // parse each row - for (int y = 0; y < height; y++) { - String[] cells = fileRep[y].split(","); - for (int x = 0; x < width; x++) { - String c = cells[x]; - if (!c.equals("--")) { - boolean white = (c.charAt(0) == 'W'); - char t = c.charAt(1); + for (int y = 0; y < height; y++) { //For each board row y = 0 … height-1: + String[] cells = fileRep[y].split(","); //Split that line on commas into exactly `width` cell codes + for (int x = 0; x < width; x++) { //For each column x = 0 … width-1: + String c = cells[x]; //Grab the two‐character code (or "--") + if (!c.equals("--")) { //If it’s not “--”, we must build a Piece there + boolean white = (c.charAt(0) == 'W'); //Determine color: W→white=true, B→white=false + char t = c.charAt(1); //Look at the second character to pick the piece type PieceType type; switch (t) { case 'K': type = PieceType.King; break; @@ -284,7 +293,7 @@ public class Board { this.isTurnWhite = fileRep[height].equals("W"); this.previousStates = new ArrayList<>(); this.highlightedSquares= new ArrayList<>(); - // any other fields you need to init… + }