diff --git a/src/backend/Board.java b/src/backend/Board.java index 7895b5d..03bd0f2 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -173,10 +173,50 @@ public class Board { } public String[] toFileRep() { - return null; + ArrayList 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) {