From 612ee4ab97d6616dfc95d4e04fe4f9430835fc30 Mon Sep 17 00:00:00 2001 From: eliot Date: Wed, 21 May 2025 20:06:49 +0200 Subject: [PATCH] Loading/Saving system (Board and Main classes touched) --- default.board | 10 +++--- src/Main.java | 54 +++++++++++++++++------------ src/backend/Board.java | 77 +++++++++++++++++++++++++++++++++++------- 3 files changed, 102 insertions(+), 39 deletions(-) diff --git a/default.board b/default.board index 368db78..a50fc7c 100644 --- a/default.board +++ b/default.board @@ -1,9 +1,9 @@ BR,BN,BB,BQ,BK,BB,BN,BR BP,BP,BP,BP,BP,BP,BP,BP - , , , , , , , - , , , , , , , - , , , , , , , - , , , , , , , +,,,,,,, +,,,,,,, +,,,,,,, +,,,,,,, WP,WP,WP,WP,WP,WP,WP,WP WR,WN,WB,WQ,WK,WB,WN,WR - +W diff --git a/src/Main.java b/src/Main.java index 15ee119..518ea9b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,30 +3,42 @@ import backend.Move; import backend.Piece; import backend.PieceType; import windowInterface.MyInterface; - +import java.io.*; public class Main { + + public static void main(String[] args) { + // Testing + Board testBoard = new Board(8, 8); + testBoard.populateBoard(); + System.out.println("Initial board:"); + System.out.println(testBoard.toString()); + saveBoard(testBoard, "save.board"); + Board loadedBoard = loadBoard("save.board"); + System.out.println("\nLoaded board:\n" + loadedBoard); - public static void main(String[] args) { - // testing : - Board testBoard = new Board(8, 8); - testBoard.populateBoard(); - testBoard.getHeight(); - testBoard.getWidth(); - System.out.println(testBoard.toString()); - testBoard.populateBoard(); - - /**Piece testPiece = new Piece(true, PieceType.Rook, 2,3); - testPiece.getX(); - testPiece.getY(); - testPiece.getType(); - testPiece.isWhite();*/ - - // launches graphical interface : - MyInterface mjf = new MyInterface(); - mjf.setVisible(true); - } + MyInterface mjf = new MyInterface(); + mjf.setVisible(true); + } -} + private static void saveBoard(Board board, String filename) { + try (FileWriter fw = new FileWriter(filename)) { + for (String line : board.toFileRep()) { + fw.write(line + "\n"); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static Board loadBoard(String filename) { + try (BufferedReader br = new BufferedReader(new FileReader(filename))) { + return new Board(br.lines().toArray(String[]::new)); + } catch (IOException e) { + e.printStackTrace(); + return new Board(8, 8); + } + } +} \ No newline at end of file diff --git a/src/backend/Board.java b/src/backend/Board.java index 8c68729..5f8b017 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -10,10 +10,10 @@ public class Board { private PieceType type; private ArrayList pieces; private ArrayList highlight = new ArrayList<>(); - private int selectedX = -1; // Tracks selected column (-1 = no selection) - private int selectedY = -1; // Tracks selected row (-1 = no selection) - private int turnNumber = 0; // Starts at 0 (White's turn) - private boolean isWhiteTurn = true; // Track current player's turn + private int selectedX = -1; + private int selectedY = -1; + private int turnNumber = 0; + private boolean isWhiteTurn = true; private char getPieceSymbol(Piece p) { char symbol; @@ -24,7 +24,7 @@ public class Board { case Bishop: symbol = 'B'; break; case Queen: symbol = 'Q'; break; case King: symbol = 'K'; break; - default: symbol = '?'; // Should never happen + default: symbol = '?'; } return p.isWhite() ? symbol : Character.toLowerCase(symbol); } @@ -48,8 +48,8 @@ public class Board { this.colNum = colNum; this.lineNum = lineNum; this.pieces = new ArrayList<>(); - this.turnNumber = 0; // Initialize turn number - this.isWhiteTurn = true; // White starts first + this.turnNumber = 0; + this.isWhiteTurn = true; } public int getWidth() { @@ -101,7 +101,7 @@ public class Board { } public void cleanBoard() { - pieces.clear(); // Remove all pieces from the board + pieces.clear(); } public String toString() { @@ -180,12 +180,63 @@ public class Board { } public String[] toFileRep() { - //TODO - return null; + String[] fileRep = new String[lineNum + 1]; + + for (int y = 0; y < lineNum; y++) { + StringBuilder rowBuilder = new StringBuilder(); + for (int x = 0; x < colNum; x++) { + Piece p = getPieceAt(x, y); + if (p != null) { + char colorChar = p.isWhite() ? 'W' : 'B'; + char typeChar = p.getType().getSummary().charAt(0); + rowBuilder.append(colorChar).append(typeChar); + } + if (x < colNum - 1) rowBuilder.append(","); + } + fileRep[y] = rowBuilder.toString(); + } + + fileRep[lineNum] = isWhiteTurn ? "W" : "B"; + return fileRep; } - public Board(String[] array) { - //TODO + public Board(String[] fileRepresentation) { + if (fileRepresentation == null || fileRepresentation.length < 1) { + throw new IllegalArgumentException("Invalid file format"); + } + + int totalRows = fileRepresentation.length - 1; + this.lineNum = totalRows; + String[] firstRow = fileRepresentation[0].split(",", -1); + this.colNum = firstRow.length; + this.pieces = new ArrayList<>(); + + for (int y = 0; y < lineNum; y++) { + String[] squares = fileRepresentation[y].split(",", -1); + if (squares.length != colNum) { + throw new IllegalArgumentException("Row " + y + " has invalid column count"); + } + + for (int x = 0; x < colNum; x++) { + String square = squares[x].trim(); + if (!square.isEmpty()) { + if (square.length() != 2) { + throw new IllegalArgumentException("Invalid piece format: " + square); + } + boolean isWhite = square.charAt(0) == 'W'; + char typeChar = square.charAt(1); + PieceType type = PieceType.fromSummary(typeChar); + pieces.add(new Piece(isWhite, type, x, y)); + } + } + } + + String turnLine = fileRepresentation[fileRepresentation.length - 1].trim(); + if (turnLine.isEmpty()) { + throw new IllegalArgumentException("Turn line is missing"); + } + this.isWhiteTurn = turnLine.charAt(0) == 'W'; + this.turnNumber = 0; } public boolean isHighlighted(int x, int y) { @@ -319,4 +370,4 @@ public class Board { ny += dy; } } -} +} \ No newline at end of file