restart from yesterday
This commit is contained in:
parent
7acff3e638
commit
bff9c08ec0
|
|
@ -1,20 +1,15 @@
|
|||
package backend;
|
||||
import java.awt.Color;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
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;
|
||||
|
|
@ -28,32 +23,28 @@ public class Simulator extends Thread {
|
|||
|
||||
private ArrayList<Agent> agents;
|
||||
|
||||
|
||||
private boolean stopFlag;
|
||||
private boolean pauseFlag;
|
||||
private boolean loopingBorder;
|
||||
private int clickActionFlag;
|
||||
private boolean clickActionFlag;
|
||||
private int loopDelay = 150;
|
||||
private int[][] worldGrid;
|
||||
private World world; //get the World instance
|
||||
private int[][] world;
|
||||
|
||||
//TODO : add missing attribute(s)
|
||||
private int stepCount;
|
||||
|
||||
|
||||
public Simulator(MyInterface mjfParam, int worldWidth, int worldHeight) {
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
mjf = mjfParam;
|
||||
//stopFlag=false; //not necessary since i set the state when pressing the button start
|
||||
pauseFlag=false;
|
||||
loopingBorder=false;
|
||||
clickActionFlag=0;
|
||||
clickActionFlag=false;
|
||||
|
||||
agents = new ArrayList<Agent>();
|
||||
fieldBirthValues = new ArrayList<Integer>();
|
||||
fieldSurviveValues = new ArrayList<Integer>();
|
||||
world =new World(worldWidth, worldHeight); //to initialize the world instance
|
||||
worldGrid = new int[world.getWidth()][world.getHeight()];
|
||||
this.rules = new Rules();
|
||||
world =new int[getWidth()][getHeight()];
|
||||
//TODO : add missing attribute initialization
|
||||
|
||||
|
||||
|
|
@ -66,26 +57,13 @@ public class Simulator extends Thread {
|
|||
}
|
||||
|
||||
public int getWidth() {
|
||||
return this.getWidth();
|
||||
return COL_NUM;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return this.getHeight();
|
||||
return LINE_NUM;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
@ -123,32 +101,16 @@ 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 += worldGrid[nx][ny];
|
||||
count += world[nx][ny];
|
||||
} else if (loopingBorder) {
|
||||
nx = (nx + getWidth()) % getWidth();
|
||||
ny = (ny + getHeight()) % getHeight();
|
||||
count += worldGrid[nx][ny];
|
||||
count += world[nx][ny];
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private boolean survives (int aliveNeighbors) {
|
||||
for (int value : rules.getSurvivalRulesArray()) {
|
||||
if ( value == aliveNeighbors) {
|
||||
return true;
|
||||
}
|
||||
}return false;
|
||||
}
|
||||
|
||||
private boolean bornes(int aliveNeighbors) {
|
||||
for(int value : rules.getBirthRulesArray()) {
|
||||
if (value == aliveNeighbors) {
|
||||
return true;
|
||||
}
|
||||
}return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void makeStep() {
|
||||
|
|
@ -158,36 +120,45 @@ public class Simulator extends Thread {
|
|||
// in agent classes
|
||||
|
||||
|
||||
int[][] newWorld = new int[getWidth()][getHeight()];
|
||||
|
||||
ArrayList<Agent> newAgents = new ArrayList<>();
|
||||
for(Agent agent : agents) {
|
||||
/*ArrayList<Agent> neighbors =this.getNeighboringAnimals(agent.getX(),agent.getY(),ANIMAL_AREA_RADIUS);}
|
||||
*/
|
||||
System.out.println("makeStep Called");
|
||||
if(!agent.liveTurn(this.getNeighboringAnimals(agent.getX(),agent.getY(),ANIMAL_AREA_RADIUS),world)) {
|
||||
agents.remove(agent);
|
||||
}
|
||||
}
|
||||
ArrayList<Agent> neighbors =
|
||||
this.getNeighboringAnimals(
|
||||
agent.getX(),
|
||||
agent.getY(),
|
||||
ANIMAL_AREA_RADIUS);}
|
||||
//if(!agent.liveTurn(
|
||||
// neighbors,
|
||||
// this)) {
|
||||
// agents.remove(agent);
|
||||
//{
|
||||
|
||||
/*int[][] nextWorld = new int[getWidth()][getHeight()];
|
||||
|
||||
// Apply Game of Life rules
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
int aliveNeighbors = countAliveNeighbors(x, y);
|
||||
if (world.getCell(x, y) == 1) {
|
||||
nextWorld[x][y] = survives(aliveNeighbors) ? 1 : 0;
|
||||
} else {
|
||||
nextWorld[x][y] = bornes(aliveNeighbors) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
//for (int x = 0; x < getWidth(); x++) {
|
||||
// for (int y = 0; y < getHeight(); y++) {
|
||||
// int aliveNeighbors = countAliveNeighbors(x, y);
|
||||
// if (world[x][y] == 1) {
|
||||
// newWorld[x][y] = (aliveNeighbors < 2 || aliveNeighbors > 3) ? 0 : 1;
|
||||
// } else {
|
||||
@ -154,7 +154,7 @@ public class Simulator extends Thread {
|
||||
|
||||
|
||||
|
||||
world = newWorld;
|
||||
//world = newWorld;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
world.setWorld(nextWorld, getWidth(), getHeight());
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
//then evolution of the field
|
||||
|
||||
// TODO : apply game rule to all cells of the field
|
||||
|
||||
/* you should distribute this action in methods/classes
|
||||
|
|
@ -205,7 +176,7 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* leave this as is
|
||||
|
|
@ -230,37 +201,20 @@ public class Simulator extends Thread {
|
|||
* method called when clicking on a cell in the interface
|
||||
*/
|
||||
public void clickCell(int x, int y) {
|
||||
|
||||
if (clickActionFlag==0) { // cell
|
||||
world.setCell(x, y, getCell(x, y) == 1 ? 0 : 1);
|
||||
|
||||
} else if (clickActionFlag==1) { // sheep
|
||||
ArrayList<Agent> nearby=getNeighboringAnimals(x,y,1);
|
||||
if (nearby.isEmpty()) {
|
||||
Sheep sheep = new Sheep(x,y);
|
||||
agents.add(sheep);
|
||||
}else {
|
||||
for (Agent animal:nearby) {
|
||||
agents.remove(animal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else if (clickActionFlag==2) { // wolf
|
||||
ArrayList<Agent> nearby=getNeighboringAnimals(x,y,1);
|
||||
if (nearby.isEmpty()) {
|
||||
Wolf wolf = new Wolf(x,y);
|
||||
agents.add(wolf);
|
||||
}else {
|
||||
for (Agent animal:nearby) {
|
||||
agents.remove(animal);
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
|
@ -286,6 +240,16 @@ 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;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -337,7 +301,7 @@ public class Simulator extends Thread {
|
|||
for(int x=0; x<lineElements.length;x++) {
|
||||
String elem = lineElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
world.setCell(x, y, value);
|
||||
setCell(x, y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -352,7 +316,7 @@ public class Simulator extends Thread {
|
|||
ArrayList<String> rule = new ArrayList<>();
|
||||
for (int i = 0; i < getHeight(); i++) {
|
||||
StringBuilder lineState = new StringBuilder();
|
||||
for (int j = 0 ; j < getWidth() ; j++) {
|
||||
for (int j = 0 ; j < getHeight() ; j++) { // je crois qu'il y a un probleme, il fau+t mettre getWidth je crois
|
||||
lineState.append(getCell(i, j));
|
||||
if (j < getWidth() - 1) {
|
||||
lineState.append(";");
|
||||
|
|
@ -362,15 +326,51 @@ public class Simulator extends Thread {
|
|||
}
|
||||
return rule;
|
||||
}
|
||||
|
||||
|
||||
public void loadRule(ArrayList<String> lines) { //to check
|
||||
/*
|
||||
* Advice for the generateRandom :
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLoopingBorder() {
|
||||
return loopingBorder;
|
||||
|
|
@ -390,30 +390,24 @@ public class Simulator extends Thread {
|
|||
}
|
||||
|
||||
public void toggleClickAction() {
|
||||
if (clickActionFlag < 2) {
|
||||
clickActionFlag ++ ;
|
||||
}else if (clickActionFlag == 2) {
|
||||
clickActionFlag=0;
|
||||
}
|
||||
//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
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
public ArrayList<String> getAgentsSave() {
|
||||
ArrayList<String> agentsSave = new ArrayList<>();
|
||||
for (int j = 0; j < getHeight(); j++) {
|
||||
StringBuilder lineState = new StringBuilder();
|
||||
for (int i = 0 ; i < getWidth() ; i++) {
|
||||
lineState.append(getCell(i, j));
|
||||
if (j < getHeight() -1) {
|
||||
lineState.append(",");
|
||||
}
|
||||
}
|
||||
agentsSave.add(lineState.toString());
|
||||
}
|
||||
return agentsSave;
|
||||
//TODO : Same idea as the other save method, but for agents
|
||||
return null;
|
||||
}
|
||||
|
||||
public void loadAgents(ArrayList<String> stringArray) {
|
||||
|
|
@ -428,23 +422,14 @@ public class Simulator extends Thread {
|
|||
public String clickActionName() {
|
||||
// TODO : initially return "sheep" or "cell"
|
||||
// depending on clickActionFlag
|
||||
|
||||
if (clickActionFlag==0) {
|
||||
return "cell";
|
||||
}else if (clickActionFlag==1) {
|
||||
return "sheep";
|
||||
}else if (clickActionFlag==2) {
|
||||
return "wolf";
|
||||
}else {
|
||||
return "error";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
public void reset() {
|
||||
for (int i = 0; i < getHeight(); i++) {
|
||||
for (int j = 0 ; j < getWidth() ; j++) {
|
||||
world.setCell(i,j,0);
|
||||
setCell(i,j,0);
|
||||
}
|
||||
}
|
||||
this.stepCount = 0;
|
||||
|
|
@ -473,17 +458,6 @@ 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue