From 5aab752b2240592968843fae3a1676ab9102b440 Mon Sep 17 00:00:00 2001 From: clara Date: Tue, 13 May 2025 14:07:54 +0200 Subject: [PATCH] problem solved for playing after loading a file saved --- src/Main.java | 9 +-------- src/backend/Board.java | 37 ++++++++++++++++++++----------------- src/backend/Game.java | 3 +++ 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/Main.java b/src/Main.java index d35d291..c78d4b3 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,11 +6,6 @@ import windowInterface.MyInterface; -import java.io.PrintWriter; -import java.io.IOException; -import java.io.FileReader; -import java.io.BufferedReader; -import java.util.ArrayList; public class Main { @@ -32,13 +27,11 @@ public class Main { MyInterface mjf = new MyInterface(); mjf.setVisible(true); - - //print the table for the memory } - } + } diff --git a/src/backend/Board.java b/src/backend/Board.java index 303e67f..9ca95c9 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -200,43 +200,46 @@ public class Board { } /* saving-loading feature :*/ + //this public method returns String[] + public String[] toFileRep() { //converts the game into a chain of characters : state of the game + ArrayList lines = new ArrayList<>();//creates a list arraylist to store each line of the save data as a string - public String[] toFileRep() { - ArrayList lines = new ArrayList<>(); + //number of tour + lines.add(String.valueOf(turnNumber));//It's converted to a string using String.valueOf - // Line 0 : number tour - lines.add(String.valueOf(turnNumber)); + //color of the player + lines.add(String.valueOf(isWhiteTurn));//same - // Line 1 : color of the player - lines.add(String.valueOf(isWhiteTurn)); - - // Line 2+: each piece - for (Piece piece : pieces) { + // piece type position and color + for (Piece piece : pieces) { //loop through all pieces of the game String line = piece.getType() + "," + piece.getX() + "," + piece.getY() + "," + piece.isWhite(); lines.add(line); } - return lines.toArray(new String[0]); + return lines.toArray(new String[0]); //Converts the ArrayList to a fixed-size String[] and returns it } - - public Board(String[] array) { - this.colNum = 8; + //constructor for the Board class. + public Board(String[] array) { //takes the previous string and reconstruct the game state from it + this.colNum = 8;//dimensions initialized this.lineNum = 8; - this.turnNumber = Integer.parseInt(array[0]); - this.isWhiteTurn = Boolean.parseBoolean(array[1]); + this.turnNumber = Integer.parseInt(array[0]);//array[0] is the turn number (converted from string to int), - this.pieces = new ArrayList<>(); + this.isWhiteTurn = Boolean.parseBoolean(array[1]);//array[1] is the current player’s turn (converted from string to boolean). + + this.pieces = new ArrayList<>();//initialize empty list to hold all pieces for (int i = 2; i < array.length; i++) { + String[] parts = array[i].split(","); + PieceType type = PieceType.valueOf(parts[0]); int x = Integer.parseInt(parts[1]); int y = Integer.parseInt(parts[2]); boolean isWhite = Boolean.parseBoolean(parts[3]); - pieces.add(new Piece(x, y, isWhite, type)); + pieces.add(new Piece(x, y, isWhite, type)); //Creates a new Piece with the extracted data and adds it to the pieces list. } } diff --git a/src/backend/Game.java b/src/backend/Game.java index 4c64f70..51d0356 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -23,6 +23,8 @@ public class Game extends Thread { aiPlayer = new AutoPlayer(); } + + public int getWidth() { return board.getWidth(); } @@ -85,6 +87,7 @@ public class Game extends Thread { public void setBoard(String[] array) { board = new Board(array); + } public Iterable getPieces() {