diff --git a/src/backend/Board.java b/src/backend/Board.java index 3bb29b0..02dd053 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -267,14 +267,47 @@ public class Board { return x >= 0 && y >= 0 && x < colNum && y < lineNum; } - // other methods not changed - public String[] toFileRep() { - //TODO - return null; + // PART3 + public String[] toFileRep() { + String[] result = new String[lineNum + 1]; // One line per row + 1 for turn + for (int y = 0; y < lineNum; y++) { + StringBuilder sb = new StringBuilder(); + for (int x = 0; x < colNum; x++) { + if (x > 0) sb.append(","); + Piece piece = board[y][x]; + if (piece != null) { + sb.append(piece.isWhite() ? "W" : "B"); + sb.append(piece.getType().getSummary()); // Ensure this returns K/Q/R/N/B/P + } + } + result[y] = sb.toString(); + } + result[lineNum] = isWhiteTurn ? "W" : "B"; + return result; } + + public Board(String[] array) { - //TODO + this.colNum = 8; + this.lineNum = 8; + this.board = new Piece[lineNum][colNum]; + + for (int y = 0; y < lineNum; y++) { + String[] tokens = array[y].split(",", -1); + for (int x = 0; x < colNum; x++) { + String token = tokens[x].trim(); + if (token.length() == 2) { + boolean isWhite = token.charAt(0) == 'W'; + char typeChar = token.charAt(1); + PieceType type = PieceType.fromSummary(typeChar); // You must implement this method + board[y][x] = new Piece(x, y, type, isWhite); + } + } + } + this.isWhiteTurn = array[lineNum].equalsIgnoreCase("W"); } + + public void undoLastMove() { //TODO }