From 9fd67df3c8e88c625c866ef38bfefe82e756d39d Mon Sep 17 00:00:00 2001 From: "g.vancompernolle" Date: Wed, 7 May 2025 14:17:15 +0200 Subject: [PATCH] sauvegarde --- src/backend/Board.java | 43 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 75eda61..01ce7dc 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -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) {