Hi there,
I’ve decided to try programing the game of life with a little added covid implementation. I somehow have an issue which I think is in the “locating neighbors”. It seems to be working but somehow everything is symmetric along the diagonal and I can’t seem to fix it. I’ve tried relooking at the code but if someone can help I’d greatly appreciate it. I can’t seem to see where I’m wrong.
</
int spacer = 5;
int cols;
int rows;
int covid;
int initialCarrier = 100;
int chanceCured = 8;
int chanceSick = 4;
color fillCol = color(255, 255, 255);
boolean grid;
void setup() {
size(800, 800);
rows = width/spacer;
cols = height/spacer;
covid = 2;
grid = new boolean[rows][cols][covid];
for (int y = 0; y < cols; y++) {
for (int x = 0; x < rows; x++) {
grid[y][1] = false;
int rnd = floor(random(2));
if (rnd == 1) {
grid[x][y][0] = true;
}
if (grid[x][y][0] ) {
int infect = floor(random(initialCarrier));
if (infect == 1) {
grid[x][y][1] = true;
};
}
}
}
}
void draw() {
background(0);
frameRate(5);
noStroke();
for (int y = 0; y < cols; y ++) {
for (int x = 0; x < rows; x ++) {
// cured
if (grid[x][y][1]) {
int cured = floor(random(chanceCured));
if (cured == 1) {
grid[x][y][1] = false;
};
if (cured == 2) {
grid[x][y][1] = false;
grid[x][y][0] = false;
};
}
/// if contaminated
if (grid[x][y][1] && grid[x][y][0]) {
fillCol = color(0, 255, 0);
fill(fillCol);
}
// not contaminated
else if (grid[x][y][0] && !grid[x][y][1]) {
int species = 0;
// previous
//for ( int i= -1; i<2; i++) {
// for ( int j= -1; j<2; j++) {
// int mody = (y + j + cols) % cols;
// int modx = (x + i + rows) % rows;
// if (grid[modx][mody][0]) {
// species +=1;
// }
// }
//}
// new basic calc
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
int nX = x+j;
int nY = y+i;
if (nX <= 0) {
nX = 0;
}
if (nY <= 0) {
nY = 0;
}
if (nX >= cols-1) {
nX = cols-1;
}
if (nY >= rows-1) {
nY = rows-1;
}
if (grid[nX][nY][0]) {
species += 1;
}
}
}
species -= 1;
if (species > 6) {
fillCol = color(255, 255, 255);
} else if (species > 4) {
fillCol = color(255, 0, 0);
} else if (species > 2) {
fillCol = color(0, 0, 255);
} else {
fillCol = color(0, 0, 100);
}
fill(fillCol);
} else {
fill(0, 0, 0);
};
rect(x*spacer, y*spacer, spacer, spacer);
}
}
updateGrid();
}
void updateGrid() {
boolean child = grid;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
if (!grid[y][1]) {
boolean alive = grid[y][0];
int species = 0;
// check neighbour v2
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
int nX = x+i;
int nY = y+j;
if (nX <= 0) {
nX = 0;
}
if (nY <= 0) {
nY = 0;
}
if (nX >= cols-1) {
nX = cols-1;
}
if (nY >= rows-1) {
nY = rows-1;
}
if (grid[nY][nX][0]) {
species += 1;
}
//if neighbour contagious
if (grid[nX][nY][1]) {
int contagious = floor(random(chanceSick));
if (contagious < 2 && alive) {
child[x][y][1] = true;
}
}
}
}
// check neighbour
//for ( int i= -1; i<2; i++) {
// for ( int j= -1; j<2; j++) {
// int modi = (x + i + cols) % cols;
// int modj = (y + j + rows) % rows;
// // if neighbour contagious
// if (grid[modj][modi][1]) {
// int contagious = floor(random(chanceSick));
// if (contagious < 2 && alive) {
// child[x][y][1] = true;
// }
// }
// // add neighbour
// if (grid[modj][modi][0]) {
// species +=1;
// }
// }
// }
if (alive) {
species -= 1;
}
if (!alive && species == 3) {
child[x][y][0] = true;
} else if (alive && (species < 2 || species > 3)) {
child[x][y][0] = false;
child[x][y][1] = false;
}
}
}
}
grid = child;
}