added comments to the loading/saving methods
This commit is contained in:
parent
6e70865ae6
commit
a93ea1c209
|
|
@ -222,11 +222,11 @@ public class Board {
|
||||||
/* saving-loading feature :*/
|
/* saving-loading feature :*/
|
||||||
|
|
||||||
public String[] toFileRep() {
|
public String[] toFileRep() {
|
||||||
String[] out = new String[height + 1];
|
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 (int y = 0; y < height; y++) { // // For each row y = 0 … height-1
|
||||||
StringBuilder row = new StringBuilder();
|
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 (int x = 0; x < width; x++) { // // For each column x = 0 … width-1
|
||||||
Piece p = board[x][y];
|
Piece p = board[x][y]; //Grab whatever piece (or null) lives at board[x][y]
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
row.append("--");
|
row.append("--");
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -240,29 +240,38 @@ public class Board {
|
||||||
case Pawn: row.append('P'); break;
|
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
|
// whose turn next
|
||||||
out[height] = isTurnWhite ? "W" : "B";
|
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) {
|
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.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
|
// parse each row
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) { //For each board row y = 0 … height-1:
|
||||||
String[] cells = fileRep[y].split(",");
|
String[] cells = fileRep[y].split(","); //Split that line on commas into exactly `width` cell codes
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) { //For each column x = 0 … width-1:
|
||||||
String c = cells[x];
|
String c = cells[x]; //Grab the two‐character code (or "--")
|
||||||
if (!c.equals("--")) {
|
if (!c.equals("--")) { //If it’s not “--”, we must build a Piece there
|
||||||
boolean white = (c.charAt(0) == 'W');
|
boolean white = (c.charAt(0) == 'W'); //Determine color: W→white=true, B→white=false
|
||||||
char t = c.charAt(1);
|
char t = c.charAt(1); //Look at the second character to pick the piece type
|
||||||
PieceType type;
|
PieceType type;
|
||||||
switch (t) {
|
switch (t) {
|
||||||
case 'K': type = PieceType.King; break;
|
case 'K': type = PieceType.King; break;
|
||||||
|
|
@ -284,7 +293,7 @@ public class Board {
|
||||||
this.isTurnWhite = fileRep[height].equals("W");
|
this.isTurnWhite = fileRep[height].equals("W");
|
||||||
this.previousStates = new ArrayList<>();
|
this.previousStates = new ArrayList<>();
|
||||||
this.highlightedSquares= new ArrayList<>();
|
this.highlightedSquares= new ArrayList<>();
|
||||||
// any other fields you need to init…
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue