created classes rules and world and moved things there, and resolved pb
with rand and density rand
This commit is contained in:
parent
024cbf7191
commit
a16b1691d3
|
|
@ -34,7 +34,7 @@ public abstract class Agent {
|
|||
// then returns a boolean
|
||||
// if false, agent dies at end of turn
|
||||
// see step function in Simulator
|
||||
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
|
||||
public abstract boolean liveTurn(ArrayList<Agent> neighbors, World world);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
package backend;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Rules {
|
||||
|
||||
|
||||
private int [] survivalRulesArray;
|
||||
private int[] birthRulesArray;
|
||||
|
||||
|
||||
public Rules() {
|
||||
|
||||
survivalRulesArray = new int[0];
|
||||
birthRulesArray = new int[0];
|
||||
}
|
||||
//initialize my arrays to empty
|
||||
|
||||
public int[] getBirthRulesArray() {
|
||||
return this.birthRulesArray;
|
||||
|
||||
}
|
||||
|
||||
public int[] getSurvivalRulesArray() {
|
||||
return this.survivalRulesArray;
|
||||
|
||||
}
|
||||
|
||||
public void loadRule(ArrayList<String> row) {
|
||||
if (row.size() <= 0) {
|
||||
System.out.println("wrong file buddy, this one's empty");
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
String surviveRulesRow = row.get(0);
|
||||
String[] surviveCells = surviveRulesRow.split(";");
|
||||
if(surviveCells.length<=0) {
|
||||
System.out.println("wrong file buddy, this one's does not have survival rules, won't work");
|
||||
return;
|
||||
}
|
||||
|
||||
String birthRulesRow = row.get(1);
|
||||
String[] birthCells = birthRulesRow.split(";");
|
||||
|
||||
//places the values of our rules CSV file in the right category, as survival or birth rules
|
||||
|
||||
|
||||
|
||||
survivalRulesArray = new int[surviveCells.length];
|
||||
birthRulesArray = new int[birthCells.length];
|
||||
//initialize my arrays with the correct length
|
||||
|
||||
for (int x = 0; x < birthCells.length; x++) {
|
||||
String elem = surviveCells[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
}
|
||||
//determines the number of alive neighboring cells needed to birth, and places them in the neededNb list
|
||||
|
||||
|
||||
for (int x = 0; x < surviveCells.length; x++) {
|
||||
String elem = surviveCells[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
}
|
||||
//determines the number of alive neighboring cells needed to survive, and places them in the neededNb list
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ public class Sheep extends Agent {
|
|||
* it can interact with the cells or with other animals
|
||||
* as you wish
|
||||
*/
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors, World world) {
|
||||
if(world.getCell(x, y)==1) {
|
||||
world.setCell(x, y, 0);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -5,14 +5,18 @@ import java.util.ArrayList;
|
|||
import java.util.Random;
|
||||
|
||||
|
||||
|
||||
import windowInterface.MyInterface;
|
||||
import windowInterface.JPanelDraw;
|
||||
|
||||
|
||||
public class Simulator extends Thread {
|
||||
|
||||
private MyInterface mjf;
|
||||
public Rules rules;
|
||||
|
||||
private final int COL_NUM = 100;
|
||||
private final int LINE_NUM = 100;
|
||||
//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,19 +26,21 @@ public class Simulator extends Thread {
|
|||
private ArrayList<Integer> fieldBirthValues;
|
||||
|
||||
private ArrayList<Agent> agents;
|
||||
|
||||
|
||||
private boolean stopFlag;
|
||||
private boolean pauseFlag;
|
||||
private boolean loopingBorder;
|
||||
private boolean clickActionFlag;
|
||||
private int loopDelay = 150;
|
||||
private int[][] world;
|
||||
private int[][] worldGrid;
|
||||
private World world; //get the World instance
|
||||
|
||||
//TODO : add missing attribute(s)
|
||||
private int stepCount;
|
||||
|
||||
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
public Simulator(MyInterface mjfParam, int worldWidth, int worldHeight) {
|
||||
mjf = mjfParam;
|
||||
//stopFlag=false; //not necessary since i set the state when pressing the button start
|
||||
pauseFlag=false;
|
||||
|
|
@ -44,8 +50,10 @@ public class Simulator extends Thread {
|
|||
agents = new ArrayList<Agent>();
|
||||
fieldBirthValues = new ArrayList<Integer>();
|
||||
fieldSurviveValues = new ArrayList<Integer>();
|
||||
world =new int[getWidth()][getHeight()];
|
||||
//TODO : add missing attribute initialization
|
||||
world =new World(worldWidth, worldHeight); //to initialize the world instance
|
||||
worldGrid = new int[world.getWidth()][world.getHeight()];
|
||||
this.rules = new Rules();
|
||||
//TODO : add missing attribute initialization
|
||||
|
||||
|
||||
|
||||
|
|
@ -55,15 +63,28 @@ public class Simulator extends Thread {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int getWidth() {
|
||||
return COL_NUM;
|
||||
return world.getWidth();
|
||||
}
|
||||
|
||||
|
||||
public int getHeight() {
|
||||
return LINE_NUM;
|
||||
return world.getHeight();
|
||||
}
|
||||
|
||||
|
||||
public World getActualWorld(){
|
||||
return world;
|
||||
}
|
||||
|
||||
public int getCell(int x, int y) {
|
||||
return world.getCell(x, y);
|
||||
}
|
||||
|
||||
public void setWorld(World world) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
|
||||
//Should probably stay as is
|
||||
public void run() {
|
||||
stepCount=0;
|
||||
|
|
@ -101,11 +122,11 @@ public class Simulator extends Thread {
|
|||
int nx = x + dir[0];
|
||||
int ny = y + dir[1];
|
||||
if (nx >= 0 && nx < getWidth() && ny >= 0 && ny < getHeight()) {
|
||||
count += world[nx][ny];
|
||||
count += worldGrid[nx][ny];
|
||||
} else if (loopingBorder) {
|
||||
nx = (nx + getWidth()) % getWidth();
|
||||
ny = (ny + getHeight()) % getHeight();
|
||||
count += world[nx][ny];
|
||||
count += worldGrid[nx][ny];
|
||||
}
|
||||
}
|
||||
return count;
|
||||
|
|
@ -122,18 +143,18 @@ public class Simulator extends Thread {
|
|||
|
||||
int[][] newWorld = new int[getWidth()][getHeight()];
|
||||
|
||||
ArrayList<Agent> newAgents = new ArrayList<>();
|
||||
/*ArrayList<Agent> newAgents = new ArrayList<>();
|
||||
for(Agent agent : agents) {
|
||||
ArrayList<Agent> neighbors =
|
||||
this.getNeighboringAnimals(
|
||||
agent.getX(),
|
||||
agent.getY(),
|
||||
ANIMAL_AREA_RADIUS);}
|
||||
//if(!agent.liveTurn(
|
||||
// neighbors,
|
||||
// this)) {
|
||||
// agents.remove(agent);
|
||||
//{
|
||||
if(!agent.liveTurn(
|
||||
neighbors,
|
||||
this)) {
|
||||
agents.remove(agent);
|
||||
}*/
|
||||
|
||||
|
||||
// Apply Game of Life rules
|
||||
|
|
@ -150,18 +171,14 @@ public class Simulator extends Thread {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//world = newWorld;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
//then evolution of the field
|
||||
//then evolution of the field
|
||||
|
||||
// TODO : apply game rule to all cells of the field
|
||||
|
||||
/* you should distribute this action in methods/classes
|
||||
|
|
@ -177,6 +194,9 @@ public class Simulator extends Thread {
|
|||
* and the count is in the birth list,
|
||||
* then the cell becomes alive
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -204,20 +224,11 @@ public class Simulator extends Thread {
|
|||
* method called when clicking on a cell in the interface
|
||||
*/
|
||||
public void clickCell(int x, int y) {
|
||||
setCell(x, y, getCell(x, y) == 1 ? 0 : 1);
|
||||
world.setCell(x, y, getCell(x, y) == 1 ? 0 : 1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* get cell value in simulated world
|
||||
* @param x coordinate of cell
|
||||
* @param y coordinate of cell
|
||||
* @return value of cell
|
||||
*/
|
||||
public int getCell(int x, int y) {
|
||||
return world[x][y];
|
||||
//get the value (dead or alive) of my cell at x y
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return list of Animals in simulated world
|
||||
|
|
@ -243,16 +254,6 @@ public class Simulator extends Thread {
|
|||
return inArea;
|
||||
}
|
||||
|
||||
/**
|
||||
* set value of cell
|
||||
* @param x coord of cell
|
||||
* @param y coord of cell
|
||||
* @param val to set in cell
|
||||
*/
|
||||
public void setCell(int x, int y, int val) {
|
||||
world [x][y] = val;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -304,7 +305,7 @@ public class Simulator extends Thread {
|
|||
for(int x=0; x<lineElements.length;x++) {
|
||||
String elem = lineElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
setCell(x, y, value);
|
||||
world.setCell(x, y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -329,51 +330,15 @@ public class Simulator extends Thread {
|
|||
}
|
||||
return rule;
|
||||
}
|
||||
public void loadRule(ArrayList<String> lines) { //to check
|
||||
/*
|
||||
* First some checks that the file is usable
|
||||
* We call early returns in conditions like this
|
||||
* "Guard clauses", as they guard the method
|
||||
* against unwanted inputs
|
||||
*/
|
||||
if(lines.size()<=0) {
|
||||
return;
|
||||
}
|
||||
String firstLine = lines.get(0);
|
||||
String[] firstLineElements = firstLine.split(";");
|
||||
if(firstLineElements.length<=0) {
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* now we fill in the world
|
||||
* with the content of the file
|
||||
*/
|
||||
for(int y =0; y<lines.size();y++) {
|
||||
String line = lines.get(y);
|
||||
String[] lineElements = line.split(";");
|
||||
for(int x=0; x<lineElements.length;x++) {
|
||||
String elem = lineElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
setCell(x, y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void generateRandom(float chanceOfLife) {
|
||||
/*
|
||||
* Advice :
|
||||
* as you should probably have a separate class
|
||||
* representing the field of cells...
|
||||
* maybe just make a constructor in there
|
||||
* and use it here
|
||||
*/
|
||||
Random rand = new Random();
|
||||
for (int x = 0; x < COL_NUM; x++) {
|
||||
for (int y = 0; y < LINE_NUM; y++) {
|
||||
world[x][y] = rand.nextFloat() < chanceOfLife ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Advice for the generateRandom :
|
||||
* as you should probably have a separate class
|
||||
* representing the field of cells...
|
||||
* maybe just make a constructor in there
|
||||
* and use it here
|
||||
*/
|
||||
|
||||
public boolean isLoopingBorder() {
|
||||
return loopingBorder;
|
||||
|
|
@ -396,15 +361,7 @@ public class Simulator extends Thread {
|
|||
//TODO : complete method
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare the content of a file saving present ruleSet
|
||||
* as you might want to save a state,
|
||||
* initialy written in this class constructor
|
||||
* as a file for future use
|
||||
* @return File content as an ArrayList of Lines (String)
|
||||
* @see loadRule for inverse process
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -432,7 +389,7 @@ public class Simulator extends Thread {
|
|||
public void reset() {
|
||||
for (int i = 0; i < getHeight(); i++) {
|
||||
for (int j = 0 ; j < getWidth() ; j++) {
|
||||
setCell(i,j,0);
|
||||
world.setCell(i,j,0);
|
||||
}
|
||||
}
|
||||
this.stepCount = 0;
|
||||
|
|
@ -459,9 +416,18 @@ public class Simulator extends Thread {
|
|||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void generateRandomWorld(float chanceOfLife, JPanelDraw panelDraw) {
|
||||
Random rand = new Random();
|
||||
for (int x = 0; x < world.getWidth(); x++) {
|
||||
for (int y = 0; y < world.getHeight(); y++) {
|
||||
float randomValue = rand.nextFloat();
|
||||
world.setCell(x, y, randomValue < chanceOfLife ? 1 : 0);
|
||||
}
|
||||
}
|
||||
panelDraw.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
package backend;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class World {
|
||||
|
||||
private int width;
|
||||
private int height;
|
||||
private int[][] world;
|
||||
|
||||
public World(int width, int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
world = new int[width][height];
|
||||
}
|
||||
//initializing width and height
|
||||
|
||||
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
//getter for the width
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
//getter for the height
|
||||
|
||||
public int[][] getWorld() {
|
||||
return world;
|
||||
}
|
||||
//getter for the world
|
||||
|
||||
|
||||
|
||||
|
||||
public void setWorld(int[][] actualworld, int width, int height) {
|
||||
this.world = actualworld;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
//initializes the world
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public int getCell (int x, int y) {
|
||||
return world[x][y];
|
||||
}
|
||||
//get the value (dead or alive) of my cell at x y
|
||||
|
||||
|
||||
|
||||
public void setCell(int x, int y, int val) {
|
||||
world [x][y] = val;
|
||||
}
|
||||
//sets the value of a cell at coord x and y to val
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ import javax.swing.event.ChangeEvent;
|
|||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import backend.Simulator;
|
||||
import backend.World;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
|
|
@ -35,9 +36,13 @@ public class MyInterface extends JFrame {
|
|||
private JLabel speedLabel;
|
||||
private JPanelDraw panelDraw;
|
||||
private Simulator mySimu=null;
|
||||
private World myWorld;
|
||||
private JSlider randSlider;
|
||||
private JSlider speedSlider;
|
||||
private JLabel clickLabel;
|
||||
public JSlider getRandSlider() {
|
||||
return randSlider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
|
|
@ -175,8 +180,9 @@ public class MyInterface extends JFrame {
|
|||
randSlider.setMaximum(100);
|
||||
randSlider.setPreferredSize(new Dimension(30,200));
|
||||
panelRight.add(randSlider);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
JButton btnBorder = new JButton("Toggle Border");
|
||||
btnBorder.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
|
|
@ -198,6 +204,8 @@ public class MyInterface extends JFrame {
|
|||
clickLabel = new JLabel("click : X");
|
||||
panelRight.add(clickLabel);
|
||||
clickLabel.setText("click : " + mySimu.clickActionName());
|
||||
|
||||
myWorld = new World(100,100);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -221,10 +229,12 @@ public class MyInterface extends JFrame {
|
|||
|
||||
public void instantiateSimu() {
|
||||
if(mySimu==null) {
|
||||
mySimu = new Simulator(this);
|
||||
mySimu = new Simulator(this, 100, 100);
|
||||
panelDraw.setSimu(mySimu);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void clicButtonGo() {
|
||||
this.instantiateSimu();
|
||||
|
|
@ -265,9 +275,16 @@ public class MyInterface extends JFrame {
|
|||
public void generateRandomBoard() {
|
||||
this.instantiateSimu();
|
||||
float chanceOfLife = ((float)randSlider.getValue())/((float)randSlider.getMaximum());
|
||||
mySimu.generateRandom(chanceOfLife);
|
||||
JPanelDraw panelDraw = getPanelDessin();
|
||||
mySimu.generateRandomWorld(chanceOfLife, panelDraw);
|
||||
panelDraw.repaint();
|
||||
}
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
float chanceOfLife = ((float) randSlider.getValue()) / ((float)randSlider.getMaximum());
|
||||
mySimu.generateRandomWorld(chanceOfLife, panelDraw);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void changeSpeed() {
|
||||
if(mySimu != null) {
|
||||
|
|
@ -279,7 +296,7 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
|
||||
public void clicLoadFileButton() {
|
||||
Simulator loadedSim = new Simulator(this);
|
||||
Simulator loadedSim = new Simulator(this, 100, 100);
|
||||
String fileName=SelectFile();
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
|
|
@ -309,7 +326,7 @@ public class MyInterface extends JFrame {
|
|||
|
||||
//to integrate the baseworld
|
||||
public void clicLoadFileButtonCSV(String fileName) {
|
||||
Simulator loadedSim = new Simulator(this);
|
||||
Simulator loadedSim = new Simulator(this, 100, 100);
|
||||
//String fileName="baseworld.csv";
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
|
|
@ -354,7 +371,7 @@ public class MyInterface extends JFrame {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mySimu.loadRule(stringArray);
|
||||
mySimu.rules.loadRule(stringArray);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
|
@ -453,5 +470,11 @@ public class MyInterface extends JFrame {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue