Skip to content
Snippets Groups Projects
Commit 6d32226e authored by Håvard Daleng's avatar Håvard Daleng
Browse files

Added some getters to controllers and created some of the tests for ChaosGame...

Added some getters to controllers and created some of the tests for ChaosGame not involving observers.
parent 348a2993
No related branches found
No related tags found
No related merge requests found
......@@ -118,6 +118,24 @@ public class ChaosCanvas {
*/
public Vector2D getMaxCoords() { return this.maxCoords; }
/**
* Get the number of points in the canvas with a value of 1.
*
* @return the sum of the points.
*/
public int getNumOfPoints() {
int sum = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (canvas[i][j] == 1) {
sum++;
}
}
}
return sum;
}
/**
* Get the affine transform representing the conversion from coordinates to indices.
*
......
......@@ -71,8 +71,24 @@ public class ChaosGame {
this.observers.addAll(Arrays.asList(observers));
}
/**
* Notify the observers that a change has occurred in the chaos game.
* Get the canvas of this chaos game.
*
* @return the canvas.
*/
public ChaosCanvas getCanvas() { return this.canvas; }
/**
* Get the current point in the chaos game.
*
* @return the point.
*/
public Vector2D getCurrentPoint() { return this.currentPoint; }
/**
* Notify the observers that a change has occurred in the ChaosGame.
*/
public void notifyObservers() {
for (ChaosGameObserver observer : this.observers) {
......@@ -81,11 +97,13 @@ public class ChaosGame {
}
/**
* Get the canvas of this chaos game.
* Add an observer to the ChaosGame.
*
* @return the canvas.
* @param observer the observer to add.
*/
public ChaosCanvas getCanvas() { return this.canvas; }
public void addObserver(ChaosGameObserver observer) {
this.observers.add(observer);
}
/**
* Run the chaos game for n iterations.
......
......@@ -24,9 +24,9 @@ public class ChaosGameDescriptionFactory {
* The naming of the txt-files must follow the pattern "desc_X.txt" where X is an integer
* incremented by 1 for each file in the directory.
*
* @throws IOException if the method fails to read successfully from one of the data files.
*
*/
public ChaosGameDescriptionFactory() throws IOException {
public ChaosGameDescriptionFactory() {
/**
* The directory where the descriptions are located.
*/
......@@ -34,10 +34,19 @@ public class ChaosGameDescriptionFactory {
this.descriptions = new ArrayList<>();
ChaosGameFileHandler handler = new ChaosGameFileHandler();
File directory = new File(directory1);
int fileCount = Objects.requireNonNull(directory.list()).length;
int fileCount = 0;
for (int i = 0; i < fileCount; i++) {
this.descriptions.add(handler.readFromFile(directory1 + "desc_" + (i + 1) + ".txt"));
try {
fileCount = Objects.requireNonNull(directory.list()).length;
} catch (NullPointerException e) {
System.out.println(e.getMessage()); // TODO: replace with different handling
}
try {
for (int i = 0; i < fileCount; i++) {
this.descriptions.add(handler.readFromFile(directory1 + "desc_" + (i + 1) + ".txt"));
}
} catch (IOException e) {
System.out.println(e.getMessage()); // TODO: replace with different handling
}
}
......
package edu.ntnu.stud.chaosgame.game;
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.data.Vector2D;
import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Test for ChaosGame {@link ChaosGame}
......@@ -12,7 +19,42 @@ public class ChaosGameTest {
@BeforeEach
void setUp() {
//this.game = new ChaosGame()
ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory();
ChaosGameDescription description = factory.getDescriptions().get(0);
ChaosCanvas canvas = new ChaosCanvas(100, 100, description.getMinCoords(), description.getMaxCoords());
this.game = new ChaosGame(description, canvas);
}
/**
* Test whether creating the ChaosGame works as expected.
*/
@Test
void shouldCreateChaosGame() {
ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory();
ChaosGameDescription description = factory.getDescriptions().get(0);
ChaosCanvas canvas = new ChaosCanvas(100, 100, description.getMinCoords(), description.getMaxCoords());
this.game = new ChaosGame(description, canvas);
Assertions.assertEquals(100, this.game.getCanvas().getWidth());
Assertions.assertEquals(100, this.game.getCanvas().getHeight());
Assertions.assertEquals(description.getMinCoords(), this.game.getCanvas().getMinCoords());
Assertions.assertEquals(description.getMaxCoords(), this.game.getCanvas().getMaxCoords());
}
/**
* Test whether running steps successfully changes the current point.
*/
@Test
void shouldRunStepsAndChangeCurrentPoint() {
Vector2D pointOne = this.game.getCurrentPoint();
this.game.runSteps(10);
Vector2D pointTwo = this.game.getCurrentPoint();
// Ensure starting point is not the same as the point after running steps
Assertions.assertNotEquals(pointOne, pointTwo);
}
// TODO: Add tests involving observers
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment