rules added and half of agents
This commit is contained in:
parent
21950423b8
commit
be6e9da50d
|
|
@ -18,13 +18,16 @@ public class Simulator extends Thread {
|
||||||
private ArrayList<Integer> fieldBirthValues;
|
private ArrayList<Integer> fieldBirthValues;
|
||||||
|
|
||||||
private ArrayList<Agent> agents;
|
private ArrayList<Agent> agents;
|
||||||
|
ArrayList<ArrayList<Integer>> rule = new ArrayList<>();
|
||||||
|
ArrayList<Integer> birthConditions = new ArrayList<>();
|
||||||
|
ArrayList<Integer> survivalConditions = new ArrayList<>();
|
||||||
|
|
||||||
private boolean stopFlag;
|
private boolean stopFlag;
|
||||||
private boolean pauseFlag;
|
private boolean pauseFlag;
|
||||||
private boolean loopingBorder;
|
private boolean loopingBorder;
|
||||||
private boolean clickActionFlag;
|
private boolean clickActionFlag;
|
||||||
private int loopDelay = 150;
|
private int loopDelay = 150;
|
||||||
private Grid grid;
|
private World world;
|
||||||
|
|
||||||
//TODO : add missing attribute(s)
|
//TODO : add missing attribute(s)
|
||||||
|
|
||||||
|
|
@ -38,8 +41,17 @@ public class Simulator extends Thread {
|
||||||
agents = new ArrayList<Agent>();
|
agents = new ArrayList<Agent>();
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
fieldBirthValues = new ArrayList<Integer>();
|
||||||
fieldSurviveValues = new ArrayList<Integer>();
|
fieldSurviveValues = new ArrayList<Integer>();
|
||||||
grid = new Grid(60, 60);
|
|
||||||
//TODO : add missing attribute initialization
|
|
||||||
|
// Normal Game of Life rules
|
||||||
|
birthConditions.add(3); // Birth condition
|
||||||
|
survivalConditions.add(2); // Survival condition
|
||||||
|
survivalConditions.add(3); // Survival condition
|
||||||
|
|
||||||
|
rule.add(birthConditions);
|
||||||
|
rule.add(survivalConditions);
|
||||||
|
|
||||||
|
world = new World(COL_NUM, LINE_NUM, rule);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -51,12 +63,12 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return grid.getWidth();
|
return world.getWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
//TODO : replace with proper return
|
//TODO : replace with proper return
|
||||||
return grid.getHeight();
|
return world.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Should probably stay as is
|
//Should probably stay as is
|
||||||
|
|
@ -119,61 +131,7 @@ public class Simulator extends Thread {
|
||||||
* and the count is in the birth list,
|
* and the count is in the birth list,
|
||||||
* then the cell becomes alive
|
* then the cell becomes alive
|
||||||
*/
|
*/
|
||||||
Grid nextGrid = new Grid(60,60);
|
world.makeStep();
|
||||||
for (int x = 0; x < getWidth(); x++) {
|
|
||||||
for (int y = 0; y < getHeight(); y++) {
|
|
||||||
int actualState = getCell(x,y);
|
|
||||||
int futureState =0;
|
|
||||||
// high life
|
|
||||||
if(actualState==0) {
|
|
||||||
for (int i = 1; i<=5; i++) {
|
|
||||||
if(checkNeighbors(x,y,i)==3||checkNeighbors(x,y,i)==6) {
|
|
||||||
futureState = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(checkNeighbors(x,y,actualState)==3||checkNeighbors(x,y,actualState)==2) {
|
|
||||||
futureState = actualState;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nextGrid.setCell(x,y,futureState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
grid = nextGrid;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int checkNeighbors(int x, int y, int state) {
|
|
||||||
int nbNeighbors = 0;
|
|
||||||
|
|
||||||
for (int i = x - 1; i <= x + 1; i++) {
|
|
||||||
for (int j = y - 1; j <= y + 1; j++) {
|
|
||||||
int x_looped = i;
|
|
||||||
int y_looped = j;
|
|
||||||
|
|
||||||
if (loopingBorder) {
|
|
||||||
if (x_looped < 0) {
|
|
||||||
x_looped = getWidth() - 1;
|
|
||||||
} else if (x_looped >= getWidth()) {
|
|
||||||
x_looped = 0;
|
|
||||||
}
|
|
||||||
if (y_looped < 0) {
|
|
||||||
y_looped = getHeight() - 1;
|
|
||||||
} else if (y_looped >= getHeight()) {
|
|
||||||
y_looped = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (x_looped >= 0 && x_looped < getWidth() && y_looped >= 0 && y_looped < getHeight() && (x_looped != x || y_looped != y)) {
|
|
||||||
if (state == grid.getCell(x_looped, y_looped).getState()) {
|
|
||||||
nbNeighbors++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println(nbNeighbors);
|
|
||||||
return nbNeighbors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -194,7 +152,7 @@ public class Simulator extends Thread {
|
||||||
* method called when clicking on a cell in the interface
|
* method called when clicking on a cell in the interface
|
||||||
*/
|
*/
|
||||||
public void clickCell(int x, int y) {
|
public void clickCell(int x, int y) {
|
||||||
grid.getCell(x, y).toggleCell();
|
world.clickCell(x, y);;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -204,7 +162,7 @@ public class Simulator extends Thread {
|
||||||
* @return value of cell
|
* @return value of cell
|
||||||
*/
|
*/
|
||||||
public int getCell(int x, int y) {
|
public int getCell(int x, int y) {
|
||||||
return grid.getCell(x, y).getState();
|
return world.getCell(x, y);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
@ -238,7 +196,7 @@ public class Simulator extends Thread {
|
||||||
* @param val to set in cell
|
* @param val to set in cell
|
||||||
*/
|
*/
|
||||||
public void setCell(int x, int y, int val) {
|
public void setCell(int x, int y, int val) {
|
||||||
grid.getCell(x, y).setState(val);
|
world.setCell(x, y, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -299,15 +257,7 @@ public class Simulator extends Thread {
|
||||||
* maybe just make a constructor in there
|
* maybe just make a constructor in there
|
||||||
* and use it here
|
* and use it here
|
||||||
*/
|
*/
|
||||||
for (int y = 0; y < getHeight(); y++) {
|
world.generateRandom(chanceOfLife);
|
||||||
for (int x = 0; x < getWidth(); x++) {
|
|
||||||
if (Math.random() < chanceOfLife) {
|
|
||||||
setCell(x, y, 1);
|
|
||||||
} else {
|
|
||||||
setCell(x, y, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLoopingBorder() {
|
public boolean isLoopingBorder() {
|
||||||
|
|
@ -323,21 +273,23 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleClickAction() {
|
public void toggleClickAction() {
|
||||||
//TODO : complete method
|
clickActionFlag = !clickActionFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
public ArrayList<String> getRule() {
|
public ArrayList<String> getRule() {
|
||||||
//TODO : complete method with proper return
|
ArrayList<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
return null;
|
// Add birth conditions
|
||||||
|
for (Integer birthCondition : rule.get(0)) {
|
||||||
|
lines.add(birthCondition.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add survival conditions
|
||||||
|
for (Integer survivalCondition : rule.get(1)) {
|
||||||
|
lines.add(survivalCondition.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadRule(ArrayList<String> lines) {
|
public void loadRule(ArrayList<String> lines) {
|
||||||
|
|
@ -345,8 +297,7 @@ public class Simulator extends Thread {
|
||||||
System.out.println("empty rule file");
|
System.out.println("empty rule file");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//TODO : remove previous rule (=emptying lists)
|
rule.clear();
|
||||||
|
|
||||||
|
|
||||||
String surviveLine = lines.get(0);
|
String surviveLine = lines.get(0);
|
||||||
String birthLine = lines.get(1);
|
String birthLine = lines.get(1);
|
||||||
|
|
@ -354,7 +305,7 @@ public class Simulator extends Thread {
|
||||||
for(int x=0; x<surviveElements.length;x++) {
|
for(int x=0; x<surviveElements.length;x++) {
|
||||||
String elem = surviveElements[x];
|
String elem = surviveElements[x];
|
||||||
int value = Integer.parseInt(elem);
|
int value = Integer.parseInt(elem);
|
||||||
//TODO : add value to possible survive values
|
rule.get(0).add(value);
|
||||||
|
|
||||||
}
|
}
|
||||||
String[] birthElements = birthLine.split(";");
|
String[] birthElements = birthLine.split(";");
|
||||||
|
|
@ -362,7 +313,7 @@ public class Simulator extends Thread {
|
||||||
String elem = birthElements[x];
|
String elem = birthElements[x];
|
||||||
int value = Integer.parseInt(elem);
|
int value = Integer.parseInt(elem);
|
||||||
//TODO : add value to possible birth values
|
//TODO : add value to possible birth values
|
||||||
|
rule.get(1).add(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -381,9 +332,7 @@ public class Simulator extends Thread {
|
||||||
* @return String representation of click action
|
* @return String representation of click action
|
||||||
*/
|
*/
|
||||||
public String clickActionName() {
|
public String clickActionName() {
|
||||||
// TODO : initially return "sheep" or "cell"
|
return clickActionFlag ? "agents" : "cell";
|
||||||
// depending on clickActionFlag
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,124 @@
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class World {
|
||||||
|
|
||||||
|
private Grid grid;
|
||||||
|
private int width;
|
||||||
|
private int height;
|
||||||
|
private boolean loopingBorder;
|
||||||
|
private ArrayList<ArrayList<Integer>> rule;
|
||||||
|
|
||||||
|
public World(int sizeX, int sizeY, ArrayList<ArrayList<Integer>> _rule) {
|
||||||
|
width = sizeX;
|
||||||
|
height = sizeY;
|
||||||
|
rule = _rule;
|
||||||
|
grid = new Grid(sizeX, sizeY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateRandom(float chanceOfLife) {
|
||||||
|
//TODO : complete method
|
||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
for (int y = 0; y < getHeight(); y++) {
|
||||||
|
for (int x = 0; x < getWidth(); x++) {
|
||||||
|
if (Math.random() < chanceOfLife) {
|
||||||
|
setCell(x, y, 1);
|
||||||
|
} else {
|
||||||
|
setCell(x, y, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void makeStep() {
|
||||||
|
|
||||||
|
Grid nextGrid = new Grid(width,height);
|
||||||
|
for (int x = 0; x < getWidth(); x++) {
|
||||||
|
for (int y = 0; y < getHeight(); y++) {
|
||||||
|
int actualState = getCell(x,y);
|
||||||
|
int futureState =0;
|
||||||
|
// high life
|
||||||
|
if(actualState==0) {
|
||||||
|
for (int i = 1; i<=5; i++) {
|
||||||
|
//Birth
|
||||||
|
if(rule.get(0).contains(checkNeighbors(x, y, i))) {
|
||||||
|
futureState = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//Survival
|
||||||
|
if(rule.get(1).contains(checkNeighbors(x, y, actualState))) {
|
||||||
|
futureState = actualState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nextGrid.setCell(x,y,futureState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
grid = nextGrid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int checkNeighbors(int x, int y, int state) {
|
||||||
|
int nbNeighbors = 0;
|
||||||
|
|
||||||
|
for (int i = x - 1; i <= x + 1; i++) {
|
||||||
|
for (int j = y - 1; j <= y + 1; j++) {
|
||||||
|
int x_looped = i;
|
||||||
|
int y_looped = j;
|
||||||
|
|
||||||
|
if (loopingBorder) {
|
||||||
|
if (x_looped < 0) {
|
||||||
|
x_looped = getWidth() - 1;
|
||||||
|
} else if (x_looped >= getWidth()) {
|
||||||
|
x_looped = 0;
|
||||||
|
}
|
||||||
|
if (y_looped < 0) {
|
||||||
|
y_looped = getHeight() - 1;
|
||||||
|
} else if (y_looped >= getHeight()) {
|
||||||
|
y_looped = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x_looped >= 0 && x_looped < getWidth() && y_looped >= 0 && y_looped < getHeight() && (x_looped != x || y_looped != y)) {
|
||||||
|
if (state == grid.getCell(x_looped, y_looped).getState()) {
|
||||||
|
nbNeighbors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(nbNeighbors);
|
||||||
|
return nbNeighbors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clickCell(int x, int y) {
|
||||||
|
grid.getCell(x, y).toggleCell();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCell(int x, int y, int val) {
|
||||||
|
grid.getCell(x, y).setState(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLoopingBorders(boolean loop) {
|
||||||
|
loopingBorder = loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCell(int x, int y) {
|
||||||
|
return grid.getCell(x, y).getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return grid.getWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return grid.getHeight();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue