sauvegarde

This commit is contained in:
Gaspard VANCOMPERNOLLE 2025-05-07 14:17:15 +02:00
parent 865287dc3d
commit 9fd67df3c8
1 changed files with 39 additions and 4 deletions

View File

@ -164,15 +164,50 @@ public class Board {
/* saving-loading feature :*/
public String[] toFileRep() {
//TODO
return null;
String[] lines = new String[height + 1];
for (int y = 0; y < height; y++) {
StringBuilder row = new StringBuilder();
for (int x = 0; x < width; x++) {
Piece p = getPieceAt(x, y);
if (p != null) {
row.append(p.isWhite() ? "W" : "B").append(p.getType().getSummary());
}
row.append(x < width - 1 ? "," : "");
}
lines[y] = row.toString();
}
// Dernière ligne : couleur du joueur actif
lines[height] = isTurnWhite() ? "W" : "B";
return lines;
}
public Board(String[] array) {
//TODO
this.width = 8;
this.height = 8;
this.pieces = new ArrayList<>();
this.highlightedSquares = new ArrayList<>();
this.selectedX = -1;
this.selectedY = -1;
for (int y = 0; y < height; y++) {
String[] cells = array[y].split(",", -1);
for (int x = 0; x < cells.length; x++) {
String cell = cells[x];
if (cell.length() == 2) {
boolean isWhite = cell.charAt(0) == 'W';
PieceType type = PieceType.fromSummary(cell.charAt(1));
setPiece(isWhite, type, x, y);
}
}
}
// Ligne 9 : couleur du joueur actif
this.turnNumber = array[height].equals("W") ? 0 : 1;
}
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {