Can someone please help me spread the infection to other cells? I am able to spread it to the left neighbouring cell.
But when I spread in other directions, the infection goes out of bounds of myth array and crashes the program. I know how an why it’s going out of bounds, I just can’t figure out how to keep it constrained with the limits of my array.
int SIDE_LENGTH = 10;
int [] cellArray ;
int randomCell;
color healthy = #DCF716;
color sick = #16DFF7;
color[] cellColour = {healthy, sick};
int cellSize;
int infected; //1 = infected & 0 = healthy
void setup()
{
size(500, 500);
cellArray= new int [SIDE_LENGTH * SIDE_LENGTH];
/**** set all bins to zero (healthy)****/
for (int i =0; i<cellArray.length-1; i++)
{
cellArray[i]=0;
randomCell = (int)random(cellArray.length); //choose a random bin in the cellArray to later infect
}
cellSize = width/SIDE_LENGTH;
infected =1;
frameRate(1);
}
//make all the cells equal to zero
//choose a random cell to be equal to one and colour it red
void draw()
{
infectRandom();
drawBoard();
spreadInfection();
//infect(infected);
//println(source);
}
void drawBoard()
{
for (int i =0; i<SIDE_LENGTH; i++)
{
for (int j=0; j<SIDE_LENGTH; j++)
{
if (cellArray[i+j*SIDE_LENGTH] != infected)
{
fill (cellColour[0]);//sets cell to healthy color
} else
{
fill (cellColour[1]);//sets cell to sick color
}
rect (i*cellSize, j*cellSize, cellSize, cellSize);//draws grid
}
}
}
void infectRandom()
{
//randomCell = (int)random(cellArray.length);
if (cellArray[randomCell]!=infected)
{
cellArray[randomCell] = infected;
}
}
void spreadInfection()
{
for (int i = 1; i < cellArray.length; i++)
{
if (cellArray[i] == infected)
{
cellArray[i-1] = infected;
cellArray[i+10] = infected;
}
}
}