added comments to the loading/saving methods

This commit is contained in:
mariettakazimierczak 2025-05-17 20:26:50 +02:00
parent 6e70865ae6
commit a93ea1c209
1 changed files with 28 additions and 19 deletions

View File

@ -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 boardrow plus one for the turn line
for (int y = 0; y < height; y++) { // // For each row y = 0 height-1
StringBuilder row = new StringBuilder(); // Well build this rows 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 textlines 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;
//The number of rows is the number of lines minus the final turnline:
// 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];
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 twocharacter code (or "--")
if (!c.equals("--")) { //If its not --, we must build a Piece there
boolean white = (c.charAt(0) == 'W'); //Determine color: Wwhite=true, Bwhite=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
}