diff --git a/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java b/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java index ab3046d871fdf318a5329041020cf0a9ef9800e3..0ba835e2c06a4219a5adc968ffadcedb99cf3dee 100644 --- a/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java +++ b/src/main/java/edu/ntnu/idatt2003/controller/MainPageController.java @@ -3,18 +3,14 @@ package edu.ntnu.idatt2003.controller; import edu.ntnu.idatt2003.model.ChaosGame; import edu.ntnu.idatt2003.model.ChaosGameDescription; import edu.ntnu.idatt2003.model.ChaosGameDescriptionFactory; -import edu.ntnu.idatt2003.model.ChaosGameDescriptionFactory.descriptionTypeEnum; -import edu.ntnu.idatt2003.model.ChaosGameFileHandler; import edu.ntnu.idatt2003.model.ChaosGameFileHandler; import edu.ntnu.idatt2003.model.Complex; import edu.ntnu.idatt2003.model.JuliaTransform; import edu.ntnu.idatt2003.model.Transform2D; import edu.ntnu.idatt2003.model.Vector2d; import edu.ntnu.idatt2003.view.MainPageView; -import edu.ntnu.idatt2003.view.MainPageView.TransformationType; import java.util.ArrayList; import java.util.List; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -43,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 static int stepsCounter; static { try { @@ -99,15 +94,10 @@ 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); } - public int getSteps() { - return stepsCounter; - } - /** * Validates the file that is uploaded., and calls storeFile if the * file exists and is formatted correctly. @@ -177,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); @@ -234,7 +230,6 @@ public class MainPageController { new ChaosGameDescription(minCoords, maxCoords, transform); chaosGameFileHandler .writeToFile(newChaosGameDescription, TRANSFORMATIONS_PATH + transformationName + ".txt"); - System.out.println(transformationName); customTransformations.add(transformationName); view.render(); } @@ -266,6 +261,7 @@ public class MainPageController { list.add(new JuliaTransform(complex, -1)); ChaosGameDescription chaosGameDescription = new ChaosGameDescription(min, max, list); game.setDescription(chaosGameDescription); + 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/MainPageView.java b/src/main/java/edu/ntnu/idatt2003/view/MainPageView.java index 764467b8a6b366c33dc2982ae2d02712f964cabc..1c666cb8de2deb679fad65ba0c3a3195b7fe7ef6 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/MainPageView.java +++ b/src/main/java/edu/ntnu/idatt2003/view/MainPageView.java @@ -550,7 +550,6 @@ public class MainPageView extends Scene implements ChaosGameObserver { */ private void updateValues(double x, double y) { controller.changeJuliaTransformationDynamic(x, y); - controller.runSteps(controller.getSteps()); xField.setText(String.format("X: %.1f", x)); yField.setText(String.format("Y: %.1f", y)); } 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