rulesof life WORKING

This commit is contained in:
isabe 2024-05-05 15:58:16 +02:00
parent 9f9991791a
commit 655d7a7d91
3 changed files with 97 additions and 42 deletions

81
src/backend/Cell.java Normal file
View File

@ -0,0 +1,81 @@
package backend;
import java.awt.Color;
public class Cell {
private int x;
private int y;
private int value;
public Cell (int x, int y, int value) {
this.x = x;
this.y = y;
this.value = value;
}
public int getValue() {
return value;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int incrementCount(int count,int[][] world, int xLocal, int yLocal, int iteration) {
int borderCell= world[xLocal][yLocal];
if(borderCell==1) {
count++;
}
return count;
}
public int countNeighbouringAliveCells(int[][] world,boolean loopingBorderFlag,int width, int height, int iteration) {
int count=0;
// if (iteration == 1) {
// System.out.println(x + " " + y);
// }
for(int n=-1; n<=1;n++) {
for (int m=-1; m<=1; m++) {
int xLocal=this.x+n;
int yLocal=this.y+m;
if (this.x != width-1 && this.x != 0 && this.y != height-1 && this.y != 0) {
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
// if (iteration == 1) {
// System.out.print(" count: " + count);
// System.out.print(" world value: " + world[xLocal][yLocal]);
// System.out.println(" x: " + xLocal + " y: " + yLocal);
// }
}
else {
if (loopingBorderFlag) {
if(this.x == width-1) {
xLocal=0;
}
if(this.x==0) {
xLocal=width-1;
}
if(this.y == height-1) {
yLocal=0;
}
if(this.y==0) {
yLocal=height-1;
}
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
}
}
}
}
return count-this.value;
}
}

View File

@ -1,5 +0,0 @@
package backend;
public class NeighbourCellRules {
}

View File

@ -28,6 +28,8 @@ public class Simulator extends Thread {
private int width=100;
private int height=100;
private int[][] world;
private int[][] tempWorld;
private int iteration;
//TODO : add missing attribute(s)
@ -37,6 +39,8 @@ public class Simulator extends Thread {
pauseFlag=false;
loopingBorderFlag=false;
clickActionFlag=false;
iteration = 0;
agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>();
@ -91,6 +95,8 @@ public class Simulator extends Thread {
* makes all the actions to go from one step to the other
*/
public void makeStep() {
iteration ++;
tempWorld = new int[width][height];
// agent behaviors first
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
@ -110,49 +116,25 @@ public class Simulator extends Thread {
for(int x=0; x<width;x++) {
for (int y=0; y<height; y++) {
int cell= world[x][y];
int count=0;
if (cell==0) {
for(int n=0; n<2;n++) {
for (int m=0; m<2; m++) {
int xLocal=x-1+n;
int yLocal=y-1+m;
int borderCell= world[xLocal][yLocal];
if (x!=xLocal && y!=yLocal) {
if(borderCell==1) {
count++;
}
}
}
}
Cell cell = new Cell(x,y,world[x][y]);
int count= cell.countNeighbouringAliveCells(world,loopingBorderFlag, width, height, iteration);
if (cell.getValue()==0) {
if (count==3) {
world[x][y]=1;
tempWorld[x][y]=1;
}
}
if (cell==1) {
for(int n=0; n<2;n++) {
for (int m=0; m<2; m++) {
int xLocal=x-1+n;
int yLocal=y-1+m;
int borderCell= world[xLocal][yLocal];
if (x!=xLocal && y!=yLocal) {
if(borderCell==1) {
count++;
}
}
}
}
if (cell.getValue()==1){
if (count<2) {
world[x][y]=0;
tempWorld[x][y]=0;
}
if (count>3) {
world[x][y]=0;
tempWorld[x][y]=0;
}
}
}
}
world = tempWorld;
//then evolution of the field
@ -171,10 +153,7 @@ public class Simulator extends Thread {
* and the count is in the birth list,
* then the cell becomes alive
*/
}
/*