diff --git a/src/backend/Board.java b/src/backend/Board.java index e068b20..1e0496a 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -116,6 +116,8 @@ public class Board { return pieces; } + + public void userTouch(int x, int y) { if (x < 0 || x >= width || y < 0 || y >= height) return; @@ -164,14 +166,58 @@ public class Board { /* saving-loading feature :*/ public String[] toFileRep() { - //TODO - return null; + String[] lines = new String[height + 1]; // one line per row + 1 for turn + + for (int y = 0; y < height; y++) { + StringBuilder sb = new StringBuilder(); + for (int x = 0; x < width; x++) { + Piece piece = board[x][y]; + if (piece == null) { + sb.append("--"); + } else { + sb.append(piece.isWhite() ? "W" : "B"); + sb.append(piece.getType().getSummary()); + } + if (x < width - 1) sb.append(","); + } + lines[y] = sb.toString(); + } + + // Last line stores the turn number + lines[height] = Integer.toString(turn); + + return lines; } - public Board(String[] array) { + //public Board(String[] array) { //TODO + //} + + public Board(String[] fileRepresentation) { + this.height = fileRepresentation.length - 1; + this.width = fileRepresentation[0].split(",").length; + this.board = new Piece[width][height]; + this.highlightedSquares = new boolean[width][height]; + + for (int y = 0; y < height; y++) { + String[] tokens = fileRepresentation[y].split(","); + for (int x = 0; x < width; x++) { + String token = tokens[x]; + if (!token.equals("--")) { + boolean isWhite = token.charAt(0) == 'W'; + char pieceChar = token.charAt(1); + PieceType type = PieceType.fromSummary(pieceChar); + board[x][y] = new Piece(isWhite, type, x, y); + } else { + board[x][y] = null; + } + } + } + + this.turn = Integer.parseInt(fileRepresentation[height]); } + private void calculateValidMoves(Piece piece) { clearHighlights();