Skip to content
Open

Fix #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions darwinworld/src/main/java/agh/proj/gui/SimulationRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand Down
13 changes: 9 additions & 4 deletions darwinworld/src/main/java/agh/proj/model/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion darwinworld/src/main/java/agh/proj/model/Globe.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Globe(int width, int height, Parameters parameters) {
initialAnimalMap();
initialAnimalsGenerator();
}

public ArrayList<Animal> getRecords(){return (ArrayList<Animal>) records;}
public List<Animal> getAnimalsAtPosition(Vector2d position) {
return animals.get(position);
}
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Animal> 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();
}

}
26 changes: 17 additions & 9 deletions darwinworld/src/main/java/agh/proj/simulation/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,13 +15,16 @@ public class Simulation implements Runnable {
private Globe worldMap;
private GlobeAssistant assistant;
private MapVisualizer mapVisualizer;
private CSVSimulationSaver csvSaver;
private final ArrayList<MapChangeListener> 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) {
Expand All @@ -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 {
Expand All @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion darwinworld/src/main/resources/simulation.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<TextField fx:id="descendantsTrack" editable="false"/>
</VBox>
<VBox>
<Label style="-fx-font-weight: bold">Life status</Label>
<Label style="-fx-font-weight: bold">When it Died</Label>
<TextField fx:id="deathTrack" editable="false"/>
</VBox>
</VBox>
Expand Down