Skip to content
Snippets Groups Projects
Commit ba6e0321 authored by Sverre Grønhaug Halvorsen's avatar Sverre Grønhaug Halvorsen
Browse files

Refactor: Move stepsCounter to ChaosGame so it gets saved

parent 2b523148
No related branches found
No related tags found
2 merge requests!41final delivery,!33Resolve "Fix bug where altering c in Julia with mouse lags"
Pipeline #287955 passed
......@@ -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());
}
}
......
......@@ -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();
}
......
......@@ -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;
......
......@@ -23,7 +23,7 @@ public class ChaosGameTest {
List.of(transform));
cg = new ChaosGame(description, 100, 100);
cg.runSteps(100);
cg.runStepsAndUpdateTotal(100);
}
@Nested
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment