makeStep
This commit is contained in:
parent
7a3716e537
commit
5766fa1d00
|
|
@ -95,20 +95,15 @@ public class Simulator extends Thread {
|
||||||
// only modify if sure of what you do
|
// only modify if sure of what you do
|
||||||
// to modify agent behavior, see liveTurn method
|
// to modify agent behavior, see liveTurn method
|
||||||
// in agent classes
|
// in agent classes
|
||||||
for(Agent agent : agents) {
|
for (int i = 0; i < agents.size(); i++) {
|
||||||
ArrayList<Agent> neighbors =
|
Agent agent = agents.get(i);
|
||||||
this.getNeighboringAnimals(
|
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
|
||||||
agent.getX(),
|
if (!agent.liveTurn(neighbors, this)) {
|
||||||
agent.getY(),
|
|
||||||
ANIMAL_AREA_RADIUS);
|
|
||||||
if(!agent.liveTurn(
|
|
||||||
neighbors,
|
|
||||||
this)) {
|
|
||||||
agents.remove(agent);
|
agents.remove(agent);
|
||||||
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//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
|
/* you should distribute this action in methods/classes
|
||||||
* don't write everything here !
|
* don't write everything here !
|
||||||
|
|
@ -123,12 +118,57 @@ 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
|
||||||
*/
|
*/
|
||||||
|
int[][] newField = new int[LINE_NUM][COL_NUM];
|
||||||
|
|
||||||
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
|
int aliveNeighbors = countAliveNeighbors(x, y);
|
||||||
|
if (field[y][x] == 1) {
|
||||||
|
if (fieldSurviveValues.contains(aliveNeighbors)) {
|
||||||
|
newField[y][x] = 1;
|
||||||
|
} else {
|
||||||
|
newField[y][x] = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (fieldBirthValues.contains(aliveNeighbors)) {
|
||||||
|
newField[y][x] = 1;
|
||||||
|
} else {
|
||||||
|
newField[y][x] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
field = newField;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int countAliveNeighbors(int x, int y) {
|
||||||
|
int count = 0;
|
||||||
|
for (int i = -LIFE_AREA_RADIUS; i <= LIFE_AREA_RADIUS; i++) {
|
||||||
|
for (int j = -LIFE_AREA_RADIUS; j <= LIFE_AREA_RADIUS; j++) {
|
||||||
|
if (i == 0 && j == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int neighborX = x + i;
|
||||||
|
int neighborY = y + j;
|
||||||
|
|
||||||
|
if (loopingBorder) {
|
||||||
|
neighborX = (neighborX + COL_NUM) % COL_NUM;
|
||||||
|
neighborY = (neighborY + LINE_NUM) % LINE_NUM;
|
||||||
|
} else {
|
||||||
|
if (neighborX < 0 || neighborY < 0 || neighborX >= COL_NUM || neighborY >= LINE_NUM) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (field[neighborY][neighborX] == 1) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* leave this as is
|
* leave this as is
|
||||||
*/
|
*/
|
||||||
|
|
@ -148,7 +188,6 @@ 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) {
|
||||||
//TODO : complete method
|
|
||||||
if (clickActionFlag) {
|
if (clickActionFlag) {
|
||||||
for (Agent agent : agents) {
|
for (Agent agent : agents) {
|
||||||
if(agent.getX() == x && agent.getY() == y) {
|
if(agent.getX() == x && agent.getY() == y) {
|
||||||
|
|
@ -162,6 +201,7 @@ public class Simulator extends Thread {
|
||||||
field[y][x] = (field[y][x] == 0) ? 1 : 0;
|
field[y][x] = (field[y][x] == 0) ? 1 : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//DONE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get cell value in simulated world
|
* get cell value in simulated world
|
||||||
|
|
@ -215,7 +255,6 @@ public class Simulator extends Thread {
|
||||||
* the simulated world in its present state
|
* the simulated world in its present state
|
||||||
*/
|
*/
|
||||||
public ArrayList<String> getSaveState() {
|
public ArrayList<String> getSaveState() {
|
||||||
//TODO : complete method with proper return
|
|
||||||
ArrayList<String> saveState = new ArrayList<>();
|
ArrayList<String> saveState = new ArrayList<>();
|
||||||
for (int y = 0; y<LINE_NUM; y++) {
|
for (int y = 0; y<LINE_NUM; y++) {
|
||||||
StringBuilder line = new StringBuilder();
|
StringBuilder line = new StringBuilder();
|
||||||
|
|
@ -268,7 +307,6 @@ public class Simulator extends Thread {
|
||||||
* to be alive in new state
|
* to be alive in new state
|
||||||
*/
|
*/
|
||||||
public void generateRandom(float chanceOfLife) {
|
public void generateRandom(float chanceOfLife) {
|
||||||
//TODO : complete method
|
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
for (int y = 0; y<LINE_NUM; y++) {
|
for (int y = 0; y<LINE_NUM; y++) {
|
||||||
for (int x = 0; x < COL_NUM; x++) {
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
|
|
@ -283,6 +321,7 @@ public class Simulator extends Thread {
|
||||||
* and use it here
|
* and use it here
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
//DONE
|
||||||
|
|
||||||
public boolean isLoopingBorder() {
|
public boolean isLoopingBorder() {
|
||||||
return loopingBorder;
|
return loopingBorder;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue