save / load partially fixed, still an error with cells and selectedcell

This commit is contained in:
tothe 2025-05-22 23:15:38 +02:00
parent 3669b81822
commit fd9f2cce62
1 changed files with 41 additions and 1 deletions

View File

@ -173,10 +173,50 @@ public class Board {
}
public String[] toFileRep() {
return null;
ArrayList<String> lines = new ArrayList<>();
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
Piece piece = cells[x][y];
if (piece != null) {
String line = piece.getType() + " " + piece.isWhite() + " " + x + " " + y;
lines.add(line);
}
}
}
return lines.toArray(new String[0]);
}
//loading feature
public Board(String[] array) {
this.cNum = 8;
this.lNum = 8;
this.cells = new Piece[cNum][lNum];
this.selectedCell = null;
//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(isWhite, type, x, y);
} catch (Exception e) {
System.err.println("Error parsing line: " + line);
}
}
}
public boolean isHighlighted(int destX, int destY) {