From bbf4f07c5d4e130e1e06683c657a9273c80b794e Mon Sep 17 00:00:00 2001 From: Jakub Mrozowski Date: Mon, 22 Jan 2024 10:03:20 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Usuni=C4=99cie=20komantarza?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- darwinworld/src/main/java/agh/proj/model/Globe.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/darwinworld/src/main/java/agh/proj/model/Globe.java b/darwinworld/src/main/java/agh/proj/model/Globe.java index 813923d..8ca4029 100644 --- a/darwinworld/src/main/java/agh/proj/model/Globe.java +++ b/darwinworld/src/main/java/agh/proj/model/Globe.java @@ -427,7 +427,7 @@ public void move(Animal animal) { } - //Ogólnie tej funcki nie potrzebujemy bo nic nie placujemy + @Override public void place(Animal animal, Vector2d position) { if (boundsValidator(position)) { From 40eebf3f5e3b369303668de79a1b06e9ecbb7c32 Mon Sep 17 00:00:00 2001 From: Jakub Mrozowski Date: Mon, 22 Jan 2024 10:07:09 +0100 Subject: [PATCH 2/3] Showing correct death date --- .../src/main/java/agh/proj/gui/SimulationRunner.java | 11 ++++------- darwinworld/src/main/java/agh/proj/model/Animal.java | 4 +++- darwinworld/src/main/resources/simulation.fxml | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java b/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java index 1e8dcbf..3d64592 100644 --- a/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java +++ b/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java @@ -13,16 +13,13 @@ import javafx.fxml.FXML; import javafx.geometry.HPos; import javafx.geometry.Insets; -import javafx.scene.control.*; -import javafx.scene.effect.Blend; -import javafx.scene.effect.BlendMode; -import javafx.scene.effect.ColorAdjust; -import javafx.scene.effect.ColorInput; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.*; import javafx.scene.paint.Color; -import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; import javafx.scene.text.TextFlow; import javafx.scene.transform.Rotate; @@ -186,7 +183,7 @@ private void updateTracking() { childrenTrack.setText(String.valueOf(animal.getChildrenNo())); descendantsTrack.setText(String.valueOf(animal.getNumberOfDescendats())); if (animal.getEnergy() == 0) - deathTrack.setText("DEAD"); + deathTrack.setText("DIED:"+animal.getDateOfDeath()); else deathTrack.setText("ALIVE"); } diff --git a/darwinworld/src/main/java/agh/proj/model/Animal.java b/darwinworld/src/main/java/agh/proj/model/Animal.java index a6f396c..33e8a77 100644 --- a/darwinworld/src/main/java/agh/proj/model/Animal.java +++ b/darwinworld/src/main/java/agh/proj/model/Animal.java @@ -28,7 +28,9 @@ public Animal(Vector2d position, Genotype genotype, int energy, int dateOfBirth, orientation = MapDirection.NORTH; currentOrientationIndex = -1; } - + public int getDateOfDeath(){ + return dateOfBirth+age; + } public Animal(Vector2d position, int energy, int genoTypeLength, int hisNumber) { this(position, Genotype.randomGenotype(genoTypeLength), energy, 0, hisNumber); } diff --git a/darwinworld/src/main/resources/simulation.fxml b/darwinworld/src/main/resources/simulation.fxml index eb429cd..8d92793 100644 --- a/darwinworld/src/main/resources/simulation.fxml +++ b/darwinworld/src/main/resources/simulation.fxml @@ -100,7 +100,7 @@ - + From f117b6520d8219ba58bbb0eecb85e951171c0c2a Mon Sep 17 00:00:00 2001 From: Jakub Mrozowski Date: Mon, 22 Jan 2024 11:08:46 +0100 Subject: [PATCH 3/3] Saving to csv Working --- .../java/agh/proj/gui/SimulationRunner.java | 5 +-- .../src/main/java/agh/proj/model/Animal.java | 5 +++ .../src/main/java/agh/proj/model/Globe.java | 2 +- .../proj/model/util/CSVSimulationSaver.java | 31 +++++++++++++++++++ .../java/agh/proj/simulation/Simulation.java | 22 +++++++++---- 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 darwinworld/src/main/java/agh/proj/model/util/CSVSimulationSaver.java diff --git a/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java b/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java index 3d64592..fe5a551 100644 --- a/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java +++ b/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java @@ -24,6 +24,7 @@ import javafx.scene.text.TextFlow; import javafx.scene.transform.Rotate; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -77,7 +78,7 @@ public class SimulationRunner implements MapChangeListener { @FXML private HBox hBox; - public void initialize() { + public void initialize() throws IOException { if (parameters == null) { return; } @@ -188,7 +189,7 @@ private void updateTracking() { deathTrack.setText("ALIVE"); } - public void setParameters(Parameters parameters) { + public void setParameters(Parameters parameters) throws IOException { this.parameters = parameters; initialize(); } diff --git a/darwinworld/src/main/java/agh/proj/model/Animal.java b/darwinworld/src/main/java/agh/proj/model/Animal.java index 33e8a77..1cbfd73 100644 --- a/darwinworld/src/main/java/agh/proj/model/Animal.java +++ b/darwinworld/src/main/java/agh/proj/model/Animal.java @@ -31,6 +31,11 @@ public Animal(Vector2d position, Genotype genotype, int energy, int dateOfBirth, public int getDateOfDeath(){ return dateOfBirth+age; } + public String getSevingString(){ + return hisNumber+":"+dateOfBirth+":"+genotype.toString()+":"+position.toString()+":" + +orientation.toString()+":"+currentOrientationIndex+":"+energy+":"+age+":"+noOfChildren + +":"+numberOfEatedGrass+":"+numberOfDescendats; + } public Animal(Vector2d position, int energy, int genoTypeLength, int hisNumber) { this(position, Genotype.randomGenotype(genoTypeLength), energy, 0, hisNumber); } diff --git a/darwinworld/src/main/java/agh/proj/model/Globe.java b/darwinworld/src/main/java/agh/proj/model/Globe.java index 8ca4029..3a78dc2 100644 --- a/darwinworld/src/main/java/agh/proj/model/Globe.java +++ b/darwinworld/src/main/java/agh/proj/model/Globe.java @@ -40,7 +40,7 @@ public Globe(int width, int height, Parameters parameters) { initialAnimalMap(); initialAnimalsGenerator(); } - + public ArrayList getRecords(){return (ArrayList) records;} public List getAnimalsAtPosition(Vector2d position) { return animals.get(position); } diff --git a/darwinworld/src/main/java/agh/proj/model/util/CSVSimulationSaver.java b/darwinworld/src/main/java/agh/proj/model/util/CSVSimulationSaver.java new file mode 100644 index 0000000..c63d208 --- /dev/null +++ b/darwinworld/src/main/java/agh/proj/model/util/CSVSimulationSaver.java @@ -0,0 +1,31 @@ +package agh.proj.model.util; + +import agh.proj.model.Animal; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; + +public class CSVSimulationSaver { + private final File file; + private final String fileName; + public CSVSimulationSaver(String nameOfSimulation) throws IOException { + fileName=nameOfSimulation+".csv"; + file=new File(nameOfSimulation+".csv"); + FileWriter fw=new FileWriter(file); + } + public void updateCSV(int day, ArrayList records)throws IOException { + FileWriter fw=new FileWriter(fileName,true); + BufferedWriter bw = new BufferedWriter(fw); + bw.write("Day "+String.valueOf(day)); + for(Animal animal:records){ + bw.write(";"+animal.getSevingString()); + } + bw.newLine(); + bw.close(); + fw.close(); + } + +} diff --git a/darwinworld/src/main/java/agh/proj/simulation/Simulation.java b/darwinworld/src/main/java/agh/proj/simulation/Simulation.java index 4a2e38a..6c8892c 100644 --- a/darwinworld/src/main/java/agh/proj/simulation/Simulation.java +++ b/darwinworld/src/main/java/agh/proj/simulation/Simulation.java @@ -2,19 +2,23 @@ import agh.proj.model.Globe; import agh.proj.model.interfaces.MapChangeListener; +import agh.proj.model.util.CSVSimulationSaver; import agh.proj.model.util.MapVisualizer; +import java.io.IOException; import java.util.ArrayList; public class Simulation implements Runnable { private Globe worldMap; private MapVisualizer mapVisualizer; + private CSVSimulationSaver csvSaver; private final ArrayList subscribers = new ArrayList<>(); private boolean pause = false; - public Simulation(Globe worldMap) { + public Simulation(Globe worldMap) throws IOException { this.worldMap = worldMap; mapVisualizer = new MapVisualizer(worldMap); + csvSaver=new CSVSimulationSaver("Simulation"); } public void setPause(boolean pause) { @@ -30,11 +34,6 @@ public void run() { System.out.println(mapVisualizer.draw()); callSubscribers(); while (worldMap.allDead() != 0) { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } while (pause) { try { @@ -58,6 +57,17 @@ public void run() { System.out.println(worldMap.mostPopularGenome() + ":Genom"); System.out.println(mapVisualizer.draw()); callSubscribers(); + try { + csvSaver.updateCSV(worldMap.getDay(), worldMap.getRecords()); + } catch (IOException e) { + throw new RuntimeException(e); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } System.out.println("Sim End"); }