Optimised loading function (moved everything in the same constructor)

This commit is contained in:
tothe 2025-05-06 15:24:33 +02:00
parent 2417b7b2ba
commit ca4c22f545
1 changed files with 26 additions and 16 deletions

View File

@ -222,29 +222,39 @@ public class Board {
return lines.toArray(new String[0]); return lines.toArray(new String[0]);
} }
public void loadFromFileRep(String[] lines) { //loading feature
cleanBoard();
for (String line : lines) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length != 4) continue;
PieceType type = PieceType.valueOf(tokens[0]);
boolean isWhite = Boolean.parseBoolean(tokens[1]);
int x = Integer.parseInt(tokens[2]);
int y = Integer.parseInt(tokens[3]);
setPiece(type, isWhite, x, y);
}
}
public Board(String[] array) { public Board(String[] array) {
this.cNum = 8; this.cNum = 8;
this.lNum = 8; this.lNum = 8;
this.cells = new Piece[cNum][lNum]; this.cells = new Piece[cNum][lNum];
this.selectedCell = null; this.selectedCell = null;
loadFromFileRep(array);
//clean board
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
cells[x][y] = null;
}
}
//load pieces
for (String line : array) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length != 4) continue;
try {
PieceType type = PieceType.valueOf(tokens[0]);
boolean isWhite = Boolean.parseBoolean(tokens[1]);
int x = Integer.parseInt(tokens[2]);
int y = Integer.parseInt(tokens[3]);
setPiece(type, isWhite, x, y);
} catch (Exception e) {
System.err.println("Error parsing line: " + line);
}
}
} }
/* The following methods require more work ! */ /* The following methods require more work ! */