Skip to content

Commit

Permalink
restructured
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 23, 2020
1 parent d427f64 commit 248e23e
Show file tree
Hide file tree
Showing 26 changed files with 216 additions and 41 deletions.
2 changes: 1 addition & 1 deletion parameters.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"width": 20,
"height": 20,
"jungleRatio": 0.1,
"jungleRatio": 0.2,
"moveEnergy": 1.0,
"initialEnergy": 100.0,
"plantEnergy": 25.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package simulation;
package application;

public class InvalidStartingParametersException extends Exception {
InvalidStartingParametersException(String errorMessage){
public InvalidStartingParametersException(String errorMessage){
super(errorMessage);
}
}
2 changes: 1 addition & 1 deletion src/sample/Main.java → src/application/Main.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sample;
package application;

import javafx.application.Application;
import javafx.stage.Stage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package sample;
package application;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import simulation.InvalidStartingParametersException;
import simulation.Simulation;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package sample;
package application;

import application.tiles.AnimalTileFactory;
import application.tiles.TerrainTileFactory;
import application.tiles.Tile;
import javafx.scene.image.Image;
import javafx.scene.canvas.Canvas;
import simulation.Animal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package sample;
package application;

import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
Expand All @@ -12,8 +11,8 @@
import javafx.stage.Stage;
import simulation.Animal;
import simulation.Simulation;
import simulation.SimulationErrorException;
import simulation.StatsWatcher;
import simulation.exceptions.SimulationErrorException;
import simulation.stats.StatsWatcher;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
Expand All @@ -31,6 +30,8 @@ public class SimulationThread extends Thread{

private final Button pauseButton = createPauseButton();
private final Button highlightButton = createHighlightButton();
private final Button endButton = createEndButton();
private final Stage stage = new Stage();

public void run() {
Platform.runLater(this::displaySimulation);
Expand Down Expand Up @@ -67,7 +68,7 @@ public void displaySimulation() {
e.printStackTrace();
}
canvas.update();
statsLabel.setText(statsWatcher.getSummary());
statsLabel.setText(statsWatcher.getDailyStatsAndUpdateSummarizer());
});
}
return iterations;
Expand All @@ -80,7 +81,6 @@ public void displaySimulation() {
private void loadWindow() {
final Group root = new Group();
final Scene scene = new Scene(root, simulation.getWidth()*10+400, Math.max(simulation.getHeight()*10,350), Color.BLACK);
final Stage stage = new Stage();

canvas = new SimulationCanvas(simulation);
canvas.setLayoutX(400);
Expand All @@ -91,7 +91,7 @@ private void loadWindow() {
selectedAnimalLabel.setLayoutX(5);
selectedAnimalLabel.setLayoutY(210);
selectedAnimalLabel.setTextFill(Color.web("#FFFFFF"));
root.getChildren().addAll(statsLabel,selectedAnimalLabel,canvas,pauseButton,highlightButton);
root.getChildren().addAll(statsLabel,selectedAnimalLabel,canvas,pauseButton,highlightButton,endButton);

stage.setScene(scene);
stage.setTitle("Evolution Simulator");
Expand Down Expand Up @@ -131,10 +131,25 @@ private Button createPauseButton(){
button.setLayoutY(150);
button.setOnMouseClicked(mouseEvent -> {
paused.set(!paused.get());
highlightButton.setVisible(!this.highlightButton.isVisible());
highlightButton.setVisible(!highlightButton.isVisible());
endButton.setVisible(!endButton.isVisible());
});
return button;
}

private Button createEndButton() {
Button button = new Button("End simulation");
button.setLayoutX(185);
button.setLayoutY(180);
button.setVisible(false);
button.setOnMouseClicked(mouseEvent -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION,statsWatcher.summarizer.getSummary());
alert.setHeaderText("Simulation finished!");
alert.showAndWait();
stage.close();}
);
return button;
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sample;
package application.tiles;

import javafx.scene.image.Image;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package sample;
package application.tiles;

import simulation.Animal;
import simulation.AnimalEnergyComparator;
import simulation.Simulation;

public class AnimalTileFactory{
private final Simulation simulation;
AnimalTileFactory(Simulation simulation){
public AnimalTileFactory(Simulation simulation){
this.simulation = simulation;
}
public AnimalTile getTile(int x, int y){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sample;
package application.tiles;

import javafx.scene.image.Image;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sample;
package application.tiles;

import simulation.Simulation;

public class TerrainTileFactory {
private final Simulation simulation;
TerrainTileFactory(Simulation simulation){
public TerrainTileFactory(Simulation simulation){
this.simulation = simulation;
}
public TerrainTile getTile(int x, int y){
Expand Down
2 changes: 1 addition & 1 deletion src/sample/Tile.java → src/application/tiles/Tile.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sample;
package application.tiles;

import javafx.scene.image.Image;

Expand Down
2 changes: 2 additions & 0 deletions src/simulation/Animal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package simulation;

import simulation.exceptions.AnimalStateException;

import java.util.*;

public class Animal implements Comparable<Animal>{
Expand Down
5 changes: 4 additions & 1 deletion src/simulation/AnimalBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public List<Location> locations(){
return new ArrayList<>(locationAnimalMap.keySet());
}


public List<Animal> getAllAnimals(){
return Stream.concat(getAllAlive().stream(), getAllDead().stream())
.collect(Collectors.toList());
}

public void remove(Animal animal) {
Location locationToRemove = null;
Expand Down
4 changes: 3 additions & 1 deletion src/simulation/Jungle.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package simulation;

import simulation.exceptions.InvalidRectangleException;

public class Jungle {
private final int x1;
private final int x2;
private final int y1;
private final int y2;
Jungle(int boardWidth, int boardHeight, double jungleRatio) throws InvalidRectangleException{
Jungle(int boardWidth, int boardHeight, double jungleRatio) throws InvalidRectangleException {
this(
(int) Math.round((1.0 - Math.sqrt(jungleRatio))*0.5*boardWidth),
(int) Math.round((1.0 - Math.sqrt(jungleRatio))*0.5*boardHeight),
Expand Down
2 changes: 2 additions & 0 deletions src/simulation/Location.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package simulation;

import simulation.exceptions.InvalidRectangleException;

import java.util.Random;

public class Location implements Comparable<Location>{
Expand Down
5 changes: 3 additions & 2 deletions src/simulation/PlantBoard.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package simulation;

import simulation.exceptions.UnplantingUnplantedLocationException;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class PlantBoard {
private AbstractMap<Location,Integer> plantedSpots = new TreeMap<Location,Integer>();
Expand All @@ -13,7 +14,7 @@ public void plant(Location location){
if (!isPlanted(location))
getPlantedSpots().put(location,1);
}
public void unplant(Location location) throws UnplantingUnplantedLocationException{
public void unplant(Location location) throws UnplantingUnplantedLocationException {
if (! isPlanted(location)){
throw new UnplantingUnplantedLocationException("Trying to unplant an unplanted location"
+ "x: " + location.getX() + "y: "+ location.getY() + "\n");
Expand Down
18 changes: 13 additions & 5 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package simulation;

import application.InvalidStartingParametersException;
import simulation.exceptions.AnimalStateException;
import simulation.exceptions.InvalidRectangleException;
import simulation.exceptions.SimulationErrorException;
import simulation.exceptions.UnplantingUnplantedLocationException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -23,7 +29,7 @@ public class Simulation {
private List<Location> eatenPlants = new ArrayList<>();
private List<Location> previousAnimalLocations = new ArrayList<>();

public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum, double initialEnergy) throws InvalidStartingParametersException{
public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum, double initialEnergy) throws InvalidStartingParametersException {
this.name = name;
this.width = width;
this.height = height;
Expand Down Expand Up @@ -76,7 +82,7 @@ private void moveAnimals() throws AnimalStateException {
public Location toBoardLimits(Location location){
return new Location((location.getX()+width)%width, (location.getY()+height)%height);
}
public void eatPlants() throws UnplantingUnplantedLocationException{
public void eatPlants() throws UnplantingUnplantedLocationException {
eatenPlants = new ArrayList<>();
for (Location location : plantBoard.getPlantedLocations()) {
List<Animal> animals = animalBoard.get(location);
Expand Down Expand Up @@ -147,7 +153,7 @@ public void start(){
addInitialAnimals();
}

public void simulateOneDay() throws SimulationErrorException{
public void simulateOneDay() throws SimulationErrorException {
currentDay++;
try {
moveAnimals();
Expand All @@ -161,8 +167,6 @@ public void simulateOneDay() throws SimulationErrorException{
}
}



public int getCurrentDay() {
return currentDay;
}
Expand All @@ -186,4 +190,8 @@ public List<Location> getEatenPlants() {
public List<Location> getPreviousAnimalLocations() {
return previousAnimalLocations;
}

public int getInitialAnimalsNum() {
return initialAnimalsNum;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simulation;
package simulation.exceptions;

public class AnimalStateException extends Exception {
public AnimalStateException(String errorMessage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simulation;
package simulation.exceptions;

public class InvalidRectangleException extends Exception {
public InvalidRectangleException(String errorMessage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package simulation;
package simulation.exceptions;

import simulation.Simulation;

public class SimulationErrorException extends Exception{
public SimulationErrorException(String errorMessage, Simulation simulation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simulation;
package simulation.exceptions;

public class UnplantingUnplantedLocationException extends Exception {
public UnplantingUnplantedLocationException(String errorMessage) {
Expand Down
17 changes: 17 additions & 0 deletions src/simulation/stats/DailyStats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package simulation.stats;

public class DailyStats {
public final int day;
public final int aliveAnimals;
public final int plants;
public final double averageEnergy;
public final double lifeExpectancy;

public DailyStats(int day, int aliveAnimals, int plants, double averageEnergy, double lifeExpectancy) {
this.day = day;
this.aliveAnimals = aliveAnimals;
this.plants = plants;
this.averageEnergy = averageEnergy;
this.lifeExpectancy = lifeExpectancy;
}
}
7 changes: 7 additions & 0 deletions src/simulation/stats/OutputUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package simulation.stats;

public class OutputUtils {
public static double toTwoDecimalPlaces(double v) {
return ((Long) Math.round(v * 100)).doubleValue() / 100;
}
}
Loading

0 comments on commit 248e23e

Please sign in to comment.