From 984a189c9827ed369910e27620f29eb9647c66a1 Mon Sep 17 00:00:00 2001 From: Magnus Eik <“mageik@stud.ntnu.noâ€> Date: Wed, 3 Apr 2024 18:09:19 +0200 Subject: [PATCH] Move game to controller, fix comment handling in readFromFile --- .../java/edu/ntnu/stud/chaosgame/Main.java | 9 +++---- .../controller/ChaosGameObserver.java | 4 +-- .../game/ChaosCanvas.java | 2 +- .../{model => controller}/game/ChaosGame.java | 2 +- .../game/ChaosGameDescription.java | 2 +- .../game/ChaosGameFileHandler.java | 10 ++++--- .../stud/chaosgame/model/data/Matrix2x2.java | 2 +- .../ChaosGameDescriptionFactory.java | 4 +-- .../transformations/AffineTransform2D.java | 2 +- .../model/transformations/JuliaTransform.java | 2 +- .../stud/chaosgame/view/ChaosGameGUIView.java | 11 ++++---- .../resources/descriptions/factory/desc_1.txt | 12 ++++----- .../resources/descriptions/factory/desc_2.txt | 14 +++++----- .../resources/descriptions/factory/desc_3.txt | 8 +++--- .../game/ChaosGameDescriptionTest.java | 2 +- .../game/ChaosGameFileHandlerTest.java | 26 ++++++++++--------- 16 files changed, 59 insertions(+), 53 deletions(-) rename src/main/java/edu/ntnu/stud/chaosgame/{model => controller}/game/ChaosCanvas.java (98%) rename src/main/java/edu/ntnu/stud/chaosgame/{model => controller}/game/ChaosGame.java (97%) rename src/main/java/edu/ntnu/stud/chaosgame/{model => controller}/game/ChaosGameDescription.java (97%) rename src/main/java/edu/ntnu/stud/chaosgame/{model => controller}/game/ChaosGameFileHandler.java (97%) diff --git a/src/main/java/edu/ntnu/stud/chaosgame/Main.java b/src/main/java/edu/ntnu/stud/chaosgame/Main.java index 7d60361..146a728 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/Main.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/Main.java @@ -1,10 +1,9 @@ package edu.ntnu.stud.chaosgame; -import edu.ntnu.stud.chaosgame.controller.ChaosGameButtonController; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; -import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas; -import edu.ntnu.stud.chaosgame.model.game.ChaosGame; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameFileHandler; +import edu.ntnu.stud.chaosgame.controller.game.ChaosCanvas; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGame; import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory; import edu.ntnu.stud.chaosgame.view.ChaosGameGUIView; import javafx.application.Application; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java index d558acb..f9a259e 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java @@ -1,7 +1,7 @@ package edu.ntnu.stud.chaosgame.controller; -import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.controller.game.ChaosCanvas; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; /** * Observer class for monitoring changes to the active diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java similarity index 98% rename from src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java rename to src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java index d8d4afa..6cb462f 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java @@ -1,4 +1,4 @@ -package edu.ntnu.stud.chaosgame.model.game; +package edu.ntnu.stud.chaosgame.controller.game; import edu.ntnu.stud.chaosgame.model.data.Matrix2x2; import edu.ntnu.stud.chaosgame.model.data.Vector2D; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java similarity index 97% rename from src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java rename to src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java index c9e34d3..925b698 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java @@ -1,4 +1,4 @@ -package edu.ntnu.stud.chaosgame.model.game; +package edu.ntnu.stud.chaosgame.controller.game; import edu.ntnu.stud.chaosgame.model.data.Vector2D; import java.util.Random; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameDescription.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java similarity index 97% rename from src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameDescription.java rename to src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java index eb54ec4..069109a 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameDescription.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java @@ -1,4 +1,4 @@ -package edu.ntnu.stud.chaosgame.model.game; +package edu.ntnu.stud.chaosgame.controller.game; import edu.ntnu.stud.chaosgame.model.data.Vector2D; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameFileHandler.java similarity index 97% rename from src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java rename to src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameFileHandler.java index 9bd2532..8af2091 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameFileHandler.java @@ -1,4 +1,4 @@ -package edu.ntnu.stud.chaosgame.model.game; +package edu.ntnu.stud.chaosgame.controller.game; import edu.ntnu.stud.chaosgame.model.data.Complex; import edu.ntnu.stud.chaosgame.model.data.Matrix2x2; @@ -34,11 +34,15 @@ public class ChaosGameFileHandler { scanner.useLocale(Locale.ENGLISH); // Use a delimiter that splits on commas and new lines, and ignores comment lines. - scanner.useDelimiter("\\s*,\\s*|\\s*\\r?\\n+\\s*|(?m)^#.*$"); +scanner.useDelimiter(",\\s*|\\r?\\n|(\\s*#.*\\s*)"); + String firstLine = scanner.next().trim(); + + - String firstLine = scanner.nextLine().trim(); if (firstLine.equals("Affine2D")) { + + // Read the minimum vector if (scanner.hasNextDouble()) { double x0min = scanner.nextDouble(); diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/data/Matrix2x2.java b/src/main/java/edu/ntnu/stud/chaosgame/model/data/Matrix2x2.java index 8b5e1de..945ee4d 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/data/Matrix2x2.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/data/Matrix2x2.java @@ -1,6 +1,6 @@ package edu.ntnu.stud.chaosgame.model.data; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; /** * Class representing a 2x2 matrix. diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java b/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java index 78a7738..a83986b 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java @@ -1,7 +1,7 @@ package edu.ntnu.stud.chaosgame.model.generators; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameFileHandler; import java.io.File; import java.io.IOException; import java.util.ArrayList; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java index 65a871d..5d519ba 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java @@ -2,7 +2,7 @@ package edu.ntnu.stud.chaosgame.model.transformations; import edu.ntnu.stud.chaosgame.model.data.Matrix2x2; import edu.ntnu.stud.chaosgame.model.data.Vector2D; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameFileHandler; /** * Represents affine transformations in a 2D-plane by extending the abstract diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java index 909c4f9..d5ccef2 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java @@ -2,7 +2,7 @@ package edu.ntnu.stud.chaosgame.model.transformations; import edu.ntnu.stud.chaosgame.model.data.Complex; import edu.ntnu.stud.chaosgame.model.data.Vector2D; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameFileHandler; public class JuliaTransform extends Transform2D { diff --git a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java index 0cbf217..91b7b6a 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java @@ -1,9 +1,9 @@ package edu.ntnu.stud.chaosgame.view; import edu.ntnu.stud.chaosgame.controller.ChaosGameButtonController; -import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas; -import edu.ntnu.stud.chaosgame.model.game.ChaosGame; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.controller.game.ChaosCanvas; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGame; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory; import javafx.scene.Scene; @@ -64,7 +64,7 @@ public class ChaosGameGUIView { //TEMPORARY CODE to test Chaos Games in GUI ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory(); - ChaosGameDescription description = factory.getDescriptions().get(1); + ChaosGameDescription description = factory.getDescriptions().get(2); ChaosCanvas canvas = new ChaosCanvas(1000, 1000, description.getMinCoords(), description.getMaxCoords()); game = new ChaosGame(description, canvas); @@ -74,7 +74,7 @@ public class ChaosGameGUIView { public void drawChaosGame(){ ChaosCanvas canvas = game.getCanvas(); - game.runSteps(100000); + game.runSteps(1000); // Test implementation for drawing fractals int[][] betaArray = canvas.getCanvasArray(); @@ -88,6 +88,7 @@ public class ChaosGameGUIView { } + } public int getWidth(){ diff --git a/src/main/resources/descriptions/factory/desc_1.txt b/src/main/resources/descriptions/factory/desc_1.txt index e98ca28..210beba 100644 --- a/src/main/resources/descriptions/factory/desc_1.txt +++ b/src/main/resources/descriptions/factory/desc_1.txt @@ -1,6 +1,6 @@ -Affine2D -0.0, 0.0 -1.0, 1.0 -0.5, 0.0, 0.0, 0.5, 0.0, 0.0 -0.5, 0.0, 0.0, 0.5, 0.25, 0.5 -0.5, 0.0, 0.0, 0.5, 0.5, 0.0 +Affine2D # Comment +0.0, 0.0 # Comment +1.0, 1.0 # Comment +0.5, 0.0, 0.0, 0.5, 0.0, 0.0 # Comment +0.5, 0.0, 0.0, 0.5, 0.25, 0.5 # Comment +0.5, 0.0, 0.0, 0.5, 0.5, 0.0 # Comment diff --git a/src/main/resources/descriptions/factory/desc_2.txt b/src/main/resources/descriptions/factory/desc_2.txt index 6985707..a925c12 100644 --- a/src/main/resources/descriptions/factory/desc_2.txt +++ b/src/main/resources/descriptions/factory/desc_2.txt @@ -1,7 +1,7 @@ -Affine2D --2.65, 0 -2.65, 10 -0, 0, 0, .16, 0, 0 -.85, .04, -.04, .85, 0, 1.6 -.2, -.26, .23, .22, 0, 1.6 --.15, .28, .26, .24, 0, .44 \ No newline at end of file +Affine2D # Comment +-2.65, 0 # Comment +2.65, 10 # Comment +0, 0, 0, .16, 0, 0 # Comment +.85, .04, -.04, .85, 0, 1.6 # Comment +.2, -.26, .23, .22, 0, 1.6 # Comment +-.15, .28, .26, .24, 0, .44 # Comment \ No newline at end of file diff --git a/src/main/resources/descriptions/factory/desc_3.txt b/src/main/resources/descriptions/factory/desc_3.txt index ed3fd5a..29d7802 100644 --- a/src/main/resources/descriptions/factory/desc_3.txt +++ b/src/main/resources/descriptions/factory/desc_3.txt @@ -1,4 +1,4 @@ -Julia --1.6, -1 -1.6, 1 --.74543, .11301 \ No newline at end of file +Julia # Comment +-1.6, -1 # Comment +1.6, 1 # Comment +-.74543, .11301 # Comment \ No newline at end of file diff --git a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java index 027010f..fdd360b 100644 --- a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java +++ b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java @@ -3,7 +3,7 @@ package edu.ntnu.stud.chaosgame.game; import edu.ntnu.stud.chaosgame.model.data.Complex; import edu.ntnu.stud.chaosgame.model.data.Matrix2x2; import edu.ntnu.stud.chaosgame.model.data.Vector2D; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; import edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D; import edu.ntnu.stud.chaosgame.model.transformations.JuliaTransform; import edu.ntnu.stud.chaosgame.model.transformations.Transform2D; diff --git a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java index b220186..e283320 100644 --- a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java +++ b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java @@ -4,8 +4,8 @@ package edu.ntnu.stud.chaosgame.game; import edu.ntnu.stud.chaosgame.model.data.Complex; import edu.ntnu.stud.chaosgame.model.data.Matrix2x2; import edu.ntnu.stud.chaosgame.model.data.Vector2D; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; -import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameFileHandler; import edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D; import edu.ntnu.stud.chaosgame.model.transformations.JuliaTransform; import edu.ntnu.stud.chaosgame.model.transformations.Transform2D; @@ -28,12 +28,12 @@ class ChaosGameFileHandlerTest { @Test void testReadFromFileAffine2D() throws IOException { String fileContent = """ - Affine2D - 0.0, 0.0 - 1.0, 1.0 - 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 - 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 - 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 + Affine2D # Comment + 0.0, 0.0 # Comment + 1.0, 1.0 # Comment + 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 # Comment + 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 # Comment + 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 # Comment """; Path tempFile = Files.createTempFile("test", ".txt"); @@ -44,6 +44,8 @@ class ChaosGameFileHandlerTest { Vector2D minCoords = description.getMinCoords(); Vector2D maxCoords = description.getMaxCoords(); + + assertEquals(0.0, minCoords.getX0()); assertEquals(0.0, minCoords.getX1()); assertEquals(1.0, maxCoords.getX0()); @@ -67,10 +69,10 @@ class ChaosGameFileHandlerTest { @Test void testReadFromFileJulia() throws IOException { String fileContent = """ - Julia - -1.6, -1 - 1.6, 1 - -.74543, .11301 + Julia # Comment \n + -1.6, -1 # Comment \n + 1.6, 1 # Comment \n + -.74543, .11301 # Comment """; Path tempFile = Files.createTempFile("test", ".txt"); -- GitLab