made rules + trying to optimise countnear
This commit is contained in:
parent
25c7bab182
commit
aa7ff4311f
|
|
@ -71,6 +71,7 @@ public class Simulator extends Thread {
|
|||
//Should probably stay as is
|
||||
public void run() {
|
||||
int stepCount=0;
|
||||
System.out.println("Step Count: "+ stepCount);
|
||||
while(!stopFlag) {
|
||||
stepCount++;
|
||||
makeStep();
|
||||
|
|
@ -114,26 +115,10 @@ public class Simulator extends Thread {
|
|||
}
|
||||
//then evolution of the field
|
||||
// TODO-INPROGRESS : apply game rule to all cells of the field
|
||||
Table tempTable = new Table(this.height, this.width, this);
|
||||
for(int x=0; x<width; x++) {
|
||||
for(int y=0; y<height; y++) {
|
||||
if (table.getCell(x, y).getValue()==1) {
|
||||
if (table.countNear(x,y)<2) {
|
||||
tempTable.getCell(x,y).setValue(0);
|
||||
} else if(table.countNear(x,y)>3) {
|
||||
tempTable.getCell(x,y).setValue(0);
|
||||
}
|
||||
} else {
|
||||
if(table.countNear(x,y)==3) {
|
||||
tempTable.getCell(x,y).setValue(1);
|
||||
}
|
||||
}
|
||||
|
||||
this.applyRule();
|
||||
|
||||
}
|
||||
}
|
||||
this.table = tempTable;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* you should distribute this action in methods/classes
|
||||
* don't write everything here !
|
||||
|
|
@ -216,7 +201,7 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public ArrayList<Agent> getAnimals(){
|
||||
return agents;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* selects Animals in a circular area of simulated world
|
||||
* @param x center
|
||||
|
|
@ -244,7 +229,6 @@ public class Simulator extends Thread {
|
|||
public void setCell(int x, int y, int val) {
|
||||
//TODO : complete method
|
||||
this.table.getCell(x, y).setValue(val);
|
||||
// set cell value to !currentCellValue
|
||||
}
|
||||
|
||||
public void countAround(int x, int y) {
|
||||
|
|
@ -372,26 +356,57 @@ public class Simulator extends Thread {
|
|||
System.out.println("empty rule file");
|
||||
return;
|
||||
}
|
||||
//TODO : remove previous rule (=emptying lists)
|
||||
|
||||
//TODO-INPROGRESS : remove previous rule (=emptying lists)
|
||||
fieldSurviveValues = new ArrayList<Integer>();
|
||||
fieldBirthValues = new ArrayList<Integer>();
|
||||
|
||||
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
|
||||
//TODO-INPROGRESS : add value to possible survive values
|
||||
fieldSurviveValues.add(value);
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
//TODO-INPROGRESS : add value to possible birth values
|
||||
fieldBirthValues.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void applyRule(){
|
||||
Table tempTable = new Table(this.height, this.width, this);
|
||||
for(int x=0; x<width; x++) {
|
||||
for(int y=0; y<height; y++) {
|
||||
if (this.getCell(x,y)==1) {
|
||||
if (this.fieldSurviveValues.contains(this.table.countNear(x, y))) {
|
||||
this.setCell(x, y, 1);
|
||||
} else {
|
||||
this.setCell(x, y, 0);
|
||||
}
|
||||
}
|
||||
else if(this.getCell(x,y)==0) {
|
||||
if (this.fieldBirthValues.contains(this.table.countNear(x, y))) {
|
||||
this.setCell(x, y, 1);
|
||||
} else {
|
||||
this.setCell(x, y, 0);
|
||||
}
|
||||
}
|
||||
System.out.println("applying rule to cell: "+x+", "+y);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
this.table = tempTable;
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<String> getAgentsSave() {
|
||||
//TODO : Same idea as the other save method, but for agents
|
||||
|
|
|
|||
|
|
@ -56,24 +56,25 @@ public class Table {
|
|||
this.table.get(row).set(column, cell);
|
||||
}
|
||||
|
||||
//TODO-complete : count near (xy) -> return how many cells around this cell
|
||||
//TODO-INPROGRESS : count near (xy) -> return how many cells around this cell
|
||||
// NEED A STRONG OPTIMISATION
|
||||
public int countNear(int row, int column){
|
||||
int cellCount =0;
|
||||
// if border is true
|
||||
for (int i = row-1;i<=row+1;i++){
|
||||
for (int j = column-1;j<=column+1;column++){
|
||||
if (!(i == j)){
|
||||
if (i<0 || i>=width || j<0 || j>=height){
|
||||
for (int x = row-1;x<=row+1;x++){
|
||||
for (int y = column-1;y<=column+1;column++){
|
||||
if (!(x == y)){
|
||||
try {
|
||||
cellCount += this.getCell(x,y).getValue();
|
||||
}
|
||||
catch(Exception e) {
|
||||
cellCount +=0;
|
||||
//TODO what to do when out of bound (wall alive or dead/ looping border) (wall dead by default)
|
||||
} else {
|
||||
cellCount += this.getCell(i,j).getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return cellCount;
|
||||
//if border is false
|
||||
return 0;
|
||||
|
||||
}
|
||||
//TODO : set agent (x y agent) load an agent to coordinates x,y
|
||||
|
|
|
|||
Loading…
Reference in New Issue