Rule load and save, basic functions, high life rule

This commit is contained in:
isabe 2024-05-20 19:08:21 +02:00
parent d577d49a42
commit ce0cb0a56c
5 changed files with 56 additions and 68 deletions

View File

@ -1,2 +1,2 @@
1;3;5;8
3;5;7;
1;3;5;8
3;5;7;

1 1 3 5 8
2 3 5 7

View File

@ -1,2 +1,2 @@
2;3
2;3
3
1 2;3
2 3

View File

@ -4,13 +4,13 @@ import java.util.Random;
import windowInterface.MyInterface;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
public class Simulator extends Thread {
private MyInterface mjf;
private final int COL_NUM = 100;
private final int LINE_NUM = 100;
private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1
private final int LIFE_AREA_RADIUS = 1;
@ -22,6 +22,8 @@ public class Simulator extends Thread {
private ArrayList<Agent> agents;
private World world;
private Rule rule;
private BasicFunctions functions;
private boolean stopFlag;
private boolean pauseFlag;
@ -48,6 +50,9 @@ public class Simulator extends Thread {
//TODO : add missing attribute initialization
world = new World(100, 100);
rule = new Rule();
functions = new BasicFunctions();
loadInitialRule();
//Default rule : Survive always, birth never
for(int i =0; i<9; i++) {
@ -55,7 +60,19 @@ public class Simulator extends Thread {
}
}
public void loadInitialRule()
{
int[] surviveLine = new int[2];
surviveLine[0] = 2;
surviveLine[1] = 3;
int[] birthLine = new int[1];
birthLine[0] = 3;
ArrayList<String> stringArray = new ArrayList<String>();
stringArray.add(functions.intArrayToCSVString(surviveLine));
stringArray.add(functions.intArrayToCSVString(birthLine));
rule.loadRule(stringArray);
}
//Should probably stay as is
public void run() {
int stepCount=0;
@ -110,19 +127,13 @@ public class Simulator extends Thread {
Cell cell = new Cell(x,y,world.getCell(x, y));
int count= cell.countNeighbouringAliveCells(worldItr,loopingBorderFlag, width, height, iteration);
if (cell.getValue()==0) {
if (count==3) {
tempWorld[x][y]=1;
if (functions.isIntInArray(rule.getBirthArray(), count)) {
tempWorld[x][y] = 1;
}
}
if (cell.getValue()==1){
if (count<2) {
tempWorld[x][y]=0;
}
else if (count>3) {
tempWorld[x][y]=0;
}
else {
tempWorld[x][y]=1;
if (functions.isIntInArray(rule.getSurviveArray(), count)) {
tempWorld[x][y] = 1;
}
}
}
@ -149,11 +160,15 @@ public class Simulator extends Thread {
}
public World getWorld()
public World getSimWorld()
{
return world;
}
public void setSimWorld(World newWorld)
{
world = newWorld;
}
/*
* leave this as is
*/
@ -278,36 +293,14 @@ public class Simulator extends Thread {
* @return File content as an ArrayList of Lines (String)
* @see loadRule for inverse process
*/
public ArrayList<String> getRule() {
//TODO : complete method with proper return
return null;
public void setRule(ArrayList<String> lines) {
rule.loadRule(lines);
}
public void loadRule(ArrayList<String> lines) {
if(lines.size()<=0) {
System.out.println("empty rule file");
return;
}
//TODO : remove previous rule (=emptying lists)
String surviveLine = lines.get(0);
String birthLine = lines.get(1);
String[] surviveElements = surviveLine.split(";");
for(int x=0; x<surviveElements.length;x++) {
String elem = surviveElements[x];
int value = Integer.parseInt(elem);
//TODO : add value to possible survive values
}
String[] birthElements = birthLine.split(";");
for(int x=0; x<birthElements.length;x++) {
String elem = birthElements[x];
int value = Integer.parseInt(elem);
//TODO : add value to possible birth values
}
public Rule getRule() {
return rule;
}
public ArrayList<String> getAgentsSave() {

View File

@ -29,7 +29,7 @@ public class JPanelDraw extends JPanel {
if(mySimu == null) {
interfaceGlobal.instantiateSimu();
}
world = mySimu.getWorld();
world = mySimu.getSimWorld();
int x = (me.getX()*world.getWidth())/getWidth();
int y = (me.getY()*world.getHeight())/getHeight();
mySimu.clickCell(x,y);
@ -48,7 +48,7 @@ public class JPanelDraw extends JPanel {
super.paintComponent(g);
this.setBackground(Color.black);
if (mySimu != null) {
world = mySimu.getWorld();
world = mySimu.getSimWorld();
// Draw Interface from state of simulator
float cellWidth = (float)this.getWidth()/(float)world.getWidth();
float cellHeight = (float)this.getHeight()/(float)world.getHeight();

View File

@ -267,7 +267,7 @@ public class MyInterface extends JFrame {
public void clicLoadFileButton() {
Simulator loadedSim = new Simulator(this);
String fileName=SelectFile("World", "load");
String fileName=SelectFile("World");
ArrayList<String> stringArray = new ArrayList<String>();
if (fileName.length()>0) {
try {
@ -285,11 +285,11 @@ public class MyInterface extends JFrame {
mySimu.stopSimu();
this.eraseLabels();
}
File file = new File("world");
int[][] fileImage = file.loadSaveFile(stringArray);
World world = loadedSim.getWorld();
int[] size = file.getSize();
int[][] fileImage = this.worldFile.loadSaveFile(stringArray);
World world = loadedSim.getSimWorld();
int[] size = this.worldFile.getSize();
world.setWorld(fileImage, size[0], size[1]);
loadedSim.setSimWorld(world);
mySimu = loadedSim;
panelDraw.setSimu(mySimu);
this.repaint();
@ -297,7 +297,7 @@ public class MyInterface extends JFrame {
}
public void clicLoadRuleFileButton() {
String fileName=SelectFile("Rule", "load");
String fileName=SelectFile("Rule");
ArrayList<String> stringArray = new ArrayList<String>();
if (fileName.length()>0) {
try {
@ -311,13 +311,13 @@ public class MyInterface extends JFrame {
} catch (Exception e) {
e.printStackTrace();
}
mySimu.loadRule(stringArray);
mySimu.setRule(stringArray);
this.repaint();
}
}
public void clicLoadAgentsFileButton() {
String fileName=SelectFile("Agent", "load");
String fileName=SelectFile("Agent");
ArrayList<String> stringArray = new ArrayList<String>();
if (fileName.length()>0) {
try {
@ -338,7 +338,7 @@ public class MyInterface extends JFrame {
public void clicSaveToFileButton() {
String fileName=SelectFile("World", "save");
String fileName=SelectFile("World");
if (fileName.length()>0) {
ArrayList<String> content = mySimu.getSaveState();
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
@ -347,16 +347,16 @@ public class MyInterface extends JFrame {
}
public void clicSaveRuleToFileButton() {
String fileName=SelectFile("Rule", "save");
String fileName=SelectFile("Rule");
if (fileName.length()>0) {
ArrayList<String> content = mySimu.getRule();
ArrayList<String> content = mySimu.getRule().getRuleDataLines();
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
ruleFile.writeFile(fileName, strArr);
}
}
public void clicSaveAgentsToFileButton() {
String fileName=SelectFile("Agents", "save");
String fileName=SelectFile("Agents");
if (fileName.length()>0) {
ArrayList<String> content = mySimu.getAgentsSave();
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
@ -365,15 +365,10 @@ public class MyInterface extends JFrame {
}
public String SelectFile(String fileType, String operationType) {
public String SelectFile(String fileType) {
String s;
JFileChooser chooser = new JFileChooser();
if (operationType == "load") {
chooser.setCurrentDirectory(new java.io.File(".\\" + fileType));
}
else if (operationType == "save") {
chooser.setCurrentDirectory(new java.io.File(".\\" + fileType));
}
chooser.setCurrentDirectory(new java.io.File(".\\" + fileType));
chooser.setDialogTitle("Choose a file");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);