diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 8f6ba94..ca12683 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,4 +1,6 @@ package backend; +import java.io.BufferedReader; +import java.io.FileReader; import java.util.ArrayList; import java.util.Random; @@ -422,6 +424,31 @@ public class Simulator extends Thread { } this.stepCount = 0; } + + + + public boolean isWorldEmpty() { + for (int x = 0; x < getWidth(); x++) { + for (int y = 0; y < getHeight(); y++) { + if (getCell(x, y) != 0) { //0 represents a "dead" cell + return false; + } + } + } + return true; + } + + + + public void startSimulation() { + if (isWorldEmpty()) { + mjf.clicLoadFileButtonCSV("World/baseworld.csv"); + } + + } + + } + diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index d0509ac..a2d4393 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -226,6 +226,9 @@ public class MyInterface extends JFrame { public void clicButtonGo() { this.instantiateSimu(); if(!mySimu.isAlive()) { + if(mySimu.isWorldEmpty()) { + mySimu.startSimulation(); + } mySimu.startSimu(); mySimu.start(); } else { @@ -306,6 +309,40 @@ public class MyInterface extends JFrame { this.repaint(); } } + + + + //trying to make the baseworld + public void clicLoadFileButtonCSV(String fileName) { + Simulator loadedSim = new Simulator(this); + //String fileName="baseworld.csv"; + ArrayList stringArray = new ArrayList(); + if (fileName.length()>0) { + try { + BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); + String line = fileContent.readLine(); + while (line != null) { + stringArray.add(line); + line = fileContent.readLine(); + } + fileContent.close(); + } catch (Exception e) { + e.printStackTrace(); + } + if(mySimu != null) { + mySimu.stopSimu(); + this.eraseLabels(); + } + loadedSim.loadSaveState(stringArray); + mySimu = loadedSim; + panelDraw.setSimu(mySimu); + this.repaint(); + } + } + + + + public void clicLoadRuleFileButton() { String fileName=SelectFile(); @@ -418,5 +455,8 @@ public class MyInterface extends JFrame { this.setClickBanner("click : X"); speedSlider.setValue(3); } + + + }