diff --git a/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java b/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java index db972e7db47b4c6e5adbe21c1023e2d1be9dfe70..0ba835e2c06a4219a5adc968ffadcedb99cf3dee 100644 --- a/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java +++ b/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java @@ -11,7 +11,6 @@ import edu.ntnu.idatt2003.model.Vector2d; import edu.ntnu.idatt2003.view.MainPageView; import java.util.ArrayList; import java.util.List; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -40,7 +39,6 @@ public class MainPageController { private static final String TRANSFORMATIONS_PATH = "src/main/resources/transformations/"; private static final String SERIALIZED_GAME_PATH = "src/main/resources/savedTransformation.ser"; private static final Logger LOGGER = Logger.getLogger(MainPageController.class.getName()); - private int stepsCounter; static { try { @@ -96,8 +94,7 @@ public class MainPageController { * @param steps The number of steps to run the simulation. */ public void runSteps(int steps) { - game.runSteps(steps); - stepsCounter += steps; + game.runStepsAndUpdateTotal(steps); LOGGER.log(Level.INFO, "Chaos game simulation ran {0} steps successfully.", steps); } @@ -170,6 +167,12 @@ public class MainPageController { } } + /** + * Load the game state from the serialized file to restore progress. + * If the file does not exist, a new game state is created. + * + * @return The loaded game state, or a new game state if the file does not exist. + */ public ChaosGame loadGameState() { LOGGER.log(Level.INFO, "Loading game state."); File file = new File(SERIALIZED_GAME_PATH); @@ -258,7 +261,7 @@ public class MainPageController { list.add(new JuliaTransform(complex, -1)); ChaosGameDescription chaosGameDescription = new ChaosGameDescription(min, max, list); game.setDescription(chaosGameDescription); - game.runSteps(stepsCounter); + game.runStepsWithoutUpdatingTotal(game.getTotalSteps()); } } diff --git a/src/main/java/edu/ntnu/idatt2003/model/ChaosGame.java b/src/main/java/edu/ntnu/idatt2003/model/ChaosGame.java index 6825755adaa6204e9a99ff0dd2e9eba784cc66da..14bd40bf85ed3f46dd2a2859d56c0548ee0919db 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/ChaosGame.java +++ b/src/main/java/edu/ntnu/idatt2003/model/ChaosGame.java @@ -20,6 +20,7 @@ public class ChaosGame implements Serializable { private final int width; private final int height; private final List<ChaosGameObserver> observers; + private int totalSteps; Random randomGenerator; /** @@ -36,6 +37,7 @@ public class ChaosGame implements Serializable { this.height = height; setDescription(description); randomGenerator = new Random(); + this.totalSteps = 0; } /** @@ -48,6 +50,15 @@ public class ChaosGame implements Serializable { return canvas; } + public int getTotalSteps() { + return totalSteps; + } + public void runStepsAndUpdateTotal(int steps) { + runSteps(steps, true); + } + public void runStepsWithoutUpdatingTotal(int steps) { + runSteps(steps, false); + } /** * Runs the chaos game simulation for the specified number of steps. * Generates points on the canvas based on random chosen transformation. @@ -55,13 +66,17 @@ public class ChaosGame implements Serializable { * * @param steps The number of steps to run the simulation. */ - public void runSteps(int steps) { + private void runSteps(int steps, boolean addSteps) { if (steps < 0) { this.canvas.clear(); + totalSteps = 0; } else { for (int i = 0; i < steps; i++) { applyRandomTransformation(); } + if (addSteps) { + totalSteps += steps; + } } notifyObservers(); } diff --git a/src/main/java/edu/ntnu/idatt2003/view/UI.java b/src/main/java/edu/ntnu/idatt2003/view/UI.java index 49b30d4cd328bdb6aba48f5a96ee1177c424fd45..b63a99039d5f6a9bef711158f0e8c4e9d9c958cb 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/UI.java +++ b/src/main/java/edu/ntnu/idatt2003/view/UI.java @@ -49,7 +49,7 @@ public class UI { runIterations(); break; case SHOW_CANVAS: - ShowCanvas(); + showCanvas(); break; default: System.out.println("Invalid choice"); @@ -159,14 +159,14 @@ public class UI { } textRenderer.enterSteps(); int steps = numberInput(); - chaosGame.runSteps(steps); + chaosGame.runStepsAndUpdateTotal(steps); } /** * Shows the canvas, and ensures that chaosGame is not null. */ - public void ShowCanvas() { + public void showCanvas() { if(chaosGame == null) { System.out.println("chaosGame is null"); return; diff --git a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java index 217b1b93f4face52a30c734b990ed75161cb6055..1e15d590294601fd256016016a212217c5ce2b71 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java @@ -23,7 +23,7 @@ public class ChaosGameTest { List.of(transform)); cg = new ChaosGame(description, 100, 100); - cg.runSteps(100); + cg.runStepsAndUpdateTotal(100); } @Nested