Im trying to build a very basic cellular automata program for a school assignment. I need to draw a grid, randomly “infect” a cell and then it needs to spread out from there. In my current code i can draw the grid, infect the cell and then it does nothing. i managed to get this working as a 2d array, but the prof wants it done without it being a 2d array.
Im not sure why that after it the program starts it won’t spread the “infection”.
final int SIZE = 500;//size of canvas
final int SIDE_LENGTH = 10;//number of cells
final int CELL = SIZE/SIDE_LENGTH;//setting cell size
final int infected = 1;//indicates infected cells
int healthy = 0;
color[] infectionColor = {color(0, 0, 0), color(255, 0, 0)};//color array
int[] gridX = new int [SIDE_LENGTH];//random x variable
int[] gridY = new int [SIDE_LENGTH];//random y variable
int[] tempGridX;//temporary x array
int[] tempGridY;//temporary y array
int x = int(random(SIDE_LENGTH));//randomly chooses an X coordinate
int y = int(random (SIDE_LENGTH));//randomly chooses a Y coordinate
void setup()
{
size (500, 500);
stroke(255);
frameRate(2);
}
void draw()
{
drawBoard();//draws board
infectRandom();//randomly infects a cell
spreadInfection();//spreads the infection
deepSave();//deep saves the status at end of loop
}
void drawBoard()
{
for (int i = 0; i<SIDE_LENGTH; i++) {
for (int j = 0; j<SIDE_LENGTH; j++) {
gridX[x] = healthy;//sets unifected cells to 0
gridY[y] = healthy;//sets unifected cells to 0
if (gridX[x]==healthy && gridY[y]==healthy) {
fill (infectionColor[0]);
} else if (gridX[x]==infected && gridY[y]==infected) {
fill (infectionColor[1]);
}
rect (i*CELL, j*CELL, CELL, CELL);
}
}
}
void infectRandom()
{
//Randomly infects a cell at the beginning of the program
if (gridX[x]==healthy && gridY[y]==healthy) {
for (int i = 0; i<SIDE_LENGTH; i++) {
for (int j = 0; j<SIDE_LENGTH; j++) {
infect(x, y);
println ("found infected thing at"+i+ +j);
}
}
}
}
void infect(int i, int j)
{
//infects a cell
gridX[i]= infected;//changes x coordinate to infected
gridY[j]= infected;//changes y coordinate to infected
fill (infectionColor[1]);//cells now drawn in infected color
rect (i*CELL, j*CELL, CELL, CELL);
}
void spreadInfection()
{
//Spreads infection throughout program
for (int i = 1; i<SIDE_LENGTH-1; i++) {
for (int j = 1; j<SIDE_LENGTH-1; j++) {
if (gridX[i]==infected && gridY[j] == infected)//if cell i&j are infected
{
if (gridX[i-1]==healthy && gridY[j]==healthy)
//if cell i-1&j are healthy
{
infect(i-1, j);//then infect
}
if (gridX[i+1]==healthy && gridY[j]==healthy)
//if cell i+1&j are healthy
{
infect(i+1, j);//then infect
}
if (gridX[i]==healthy && gridY[j-1]==healthy)
//if cell i&j-1 are healthy
{
infect(i, j-1);//then infect
}
if (gridX[i]==healthy && gridY[j+1]==healthy)
//if cell i&j+1 are healthy
{
infect(i, j+1);//then infect
}
}
}
}
tempGridX=gridX;//shallow save into temporary array x variable
tempGridY=gridY;//shallow save into temporary array y variable
}
void deepSave()
{
//Deep save of game information back into original array
tempGridX = new int[gridX.length];
tempGridY = new int[gridY.length];
for (int i =0; i<gridX.length; i++) {
for (int j = 0; j<gridY.length; j++) {
tempGridX[i] = gridX[i];//deep save of temporary x variable into original array
tempGridY[j] = gridY[j];//deep save of temporary y variable into original array
}
}
}