diff --git a/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java b/darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java index 3c9d3d7..b44a66a 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; } @@ -183,12 +184,14 @@ private void updateTracking() { childrenTrack.setText(String.valueOf(animal.getChildrenNo())); descendantsTrack.setText(String.valueOf(animal.getNumberOfDescendats())); if (animal.getEnergy() == 0) - deathTrack.setText("DIED AT DAY:"+animal.getDateOfDeath()); + + deathTrack.setText("DIED:"+animal.getDateOfDeath()); + else 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 1e3716f..f041c80 100644 --- a/darwinworld/src/main/java/agh/proj/model/Animal.java +++ b/darwinworld/src/main/java/agh/proj/model/Animal.java @@ -32,10 +32,15 @@ public Animal(Vector2d position, Genotype genotype, int energy, int dateOfBirth, orientation = MapDirection.NORTH; currentOrientationIndex = -1; } - public String toSaveString(){ - return dateOfBirth+":"+genotype.toString() - +":"+position.toString()+":"+orientation.toString()+":"+currentOrientationIndex+":" - +energy+":"+age+":"+noOfChildren+":"+numberOfDescendats+":"+numberOfEatedGrass; + + 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 e8b9fbc..e545b83 100644 --- a/darwinworld/src/main/java/agh/proj/model/Globe.java +++ b/darwinworld/src/main/java/agh/proj/model/Globe.java @@ -46,7 +46,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); } @@ -333,6 +333,7 @@ public void move(Animal animal) { animal.move(this); animals.get(animal.getPosition()).add(animal); } + @Override public void place(Animal animal, Vector2d position) { if (boundsValidator(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 7a08ac7..092fc08 100644 --- a/darwinworld/src/main/java/agh/proj/simulation/Simulation.java +++ b/darwinworld/src/main/java/agh/proj/simulation/Simulation.java @@ -3,7 +3,9 @@ import agh.proj.model.Globe; import agh.proj.model.GlobeAssistant; import agh.proj.model.interfaces.MapChangeListener; -import agh.proj.model.util.CsvSaver; + +import agh.proj.model.util.CSVSimulationSaver; + import agh.proj.model.util.MapVisualizer; import java.io.IOException; @@ -13,13 +15,16 @@ public class Simulation implements Runnable { private Globe worldMap; private GlobeAssistant assistant; 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); - this.assistant=new GlobeAssistant(worldMap); + + csvSaver=new CSVSimulationSaver("Simulation"); + } public void setPause(boolean pause) { @@ -35,11 +40,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 { @@ -64,10 +64,18 @@ public void run() { System.out.println(mapVisualizer.draw()); callSubscribers(); try { - CsvSaver.saveToCsv("Simulation", worldMap.getDay(), worldMap.getRecords()); + + 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"); } 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 @@ - +