Spread infection to other cells (help with a school assignment)

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;
    }
     }
  }

Okay. Good question.

You have an array of cells. There are 100 cell cells in this array.
You could number them. The first is cell 0. The second is cell 1. And so on. The last is cell 99. Notice that there is no cell numbered 100, even though the length of this array is 100.

You have located a cell that is infected. It’s number is stored in your variable i.

You are then trying to infect a different cell, based on the value of i. You know that the cell BELOW cell i is the cell with number i+10. But what if cell i is already in the bottom row? That’s what the problem is. You can’t infect any cells with a number greater than or equal to 100 because there are no cells with those numbers!

So before you infect a cell, you will need to make sure that the cell you are trying to infect has a valid number. Remember, the cells’ number range from 0 to 99.

You could easily write a conditional statement that check to see if the value you are going to use as an index into your cells array is a valid index, and then only attempt to infect that cell if, in fact, that is a valid index.

This is a prime example of a great place to use an if statement.

Here’s the logic in plain words:

// When spreading the infection,
// First find an infected cell that the infection will spread from.
// That cell is cell number i.
// Then pick a direction the infection will spread.
// If the direction is up, you know you will be infecting cell number i-10.
// If the direction is down, you know you will be infecting cell number i+10.
// If the direction is left, you know you will be infecting cell number i-1.
// If the direction is right, you know you will be infecting cell number i+1.
// In any of the above four cases, you now have a number for the cell you are trying to infect (either i-10, i+10, i-1, or i+1).
// At this point, check to make sure that this cell you are going to try to infect is, in fact, a valid cell number.
// Again, remember that cells range from 0 to 99.
// So for an index to be valid, it has to be greater than or equal to 0, and also less than 100.
// If the index is valid, infect the cell at that index.

This translates almost directly into code. You must attempt to write this code yourself, as no one else can do you assignment for you.

1 Like

That makes perfect sense to me. I am still missing something though. The code below should work with the updated “if” statement… but it doesn’t. The problem is my cells all have a value of either 1 or 0 (infected or healthy). I can’t figure out values to constrain with an “if” statement.

void spreadInfection()
{
  for (int i = 1; i < cellArray.length; i++) 
  {
    if (cellArray[i] == infected && cellArray[i] > 0 && cellArray[i] < 99)
    {
      println(cellArray);
      cellArray[i-10] = infected;
    }
  }
}

You are not checking the proper cell.

You need to check the i+10 cell

Like this?? Lol my head is starting to hurt… I’ve been at this for days

void spreadInfection()
{
  for (int i = 1; i < cellArray.length; i++) 
  {
    if (cellArray[i] == infected && cellArray[i+10] > cellArray[0])
    {
      println(cellArray);
      cellArray[i-10] = infected;
    }
  }
}

Nope…

Imagine that you have a 10 cells by 10 cells matrix.

You are storing those cells in the cellArray. Thus cellArray has 10*10 = 100 spot from 0 to 99.

Now you are infecting one cell. Let’s say you are infecting the cell 95.

You now want to spread it form that cell. In order to do that you add 10 to that 95th cell to get the one beneath it.
The problem is that you are now trying to reach the 95+10 = 105th cell. But this cell does not exist since the cellArray contain only 100 available spots from 0 to 99.

So what you wanna check is if i + 10 is strictly lower than 100 which is the last cell you can access

Even the description of the process I just gave you has problems…


You have a cell in the grid that you know is infected and you want to spread that infection to an adjacent cell.

You can describe the infected cell in two ways.

The first way is that you can give its index. That is its position in the array of cells.

Since there are 100 cells in the array. The valid indexes are 0, 1, 2, 3, 4, … , 98, 99. Notice that index 100 is not a valid index.

The second way to describe a cells location is by it’s x and y position in the 2D array.

All the cells in the leftmost column have an x position of 0.
All the cells in the bottom row have a y position of 9.
Notice that x and y positions range from 0 to 9. They are never -1, and they are never 10.

If you were to draw out on paper your grid of cells and then number each of them with their index, x, and y, you might see a pattern!


If you are given a cell’s index, you can determine its x and y positions.

If you are given a cell’s x and y positions, you can determine its index.

You might want to save yourself some hassle and write function that do this conversion for you:

int getIndex( int x, int y){
  return( ??? );
}

int getX( int index ){
  return( ??? );
}

int getY( int index ){
  return( ??? );
}

Now that you have picked a cell that you want to spread infection from, you have it’s index. Now get it’s x and y positions. Based on those positions, you know which directions you can spread infection in (and which you CAN’T! For example, a cell in the right column (x is 9) can’t spread infection right because there is no column 10!). Based on a valid direction to spread the infextion in, you will get a new X and Y for a cell that you want to infect.

You can then use your conversion functions to find that cell’s INDEX.

My head just exploded lol

Here. Run this and it will help you visualize it.

size(1000, 1000);
textSize(16);
background(0);
int t = 0;
for ( int j = 0; j < 10; j++) {
  for ( int i = 0; i < 10; i++) {
    pushMatrix();
    translate(100*i, 100*j);
    noFill();
    stroke(255, 255, 0);
    rect(0, 0, 100, 100);
    fill(255);
    text( "INDEX: " + t, 10, 30);
    text( "I: " + i + "     J: " + j, 10, 70);
    fill(255, 0, 0);
    noStroke();
    translate(20, 80);
    if ( i == 0 ) {
      triangle( 0, 5, 10, 0, 10, 10);
    }
    if ( i == 9 ) {
      triangle( 10, 5, 0, 0, 0, 10);
    }
    translate(50, 0);
    if ( j == 0 ) {
      triangle( 5, 0, 0, 10, 10, 10);
    }
    if ( j == 9 ) {
      triangle( 5, 10, 0, 0, 10, 0);
    }
    popMatrix();
    t++;
  }
}

Here is my updated code… it works… for the most part. Although something is wrong with my spread infection function. cellArray-1 and cellArray-SIDE+LENGTH work fine and draw properly infecting the top and left neighbouring cells. However, when I try to infect the right and bottom neighbouring cells, they become infected in one frame instead of one at a time.

Anyways thanks everyone for your help. It truly has been appreciated.



int SIDE_LENGTH = 10; // 10 cells in each row and column
int [] cellArray ; //the main array which stores the state of each cell
int randomCell; //this will choose a random cell to start the infection
color healthy = #DCF716; //yellow for healthy cells
color sick = #16DFF7; //blue for sick cells
color[] cellColour = {healthy, sick}; //colour array representing the state of each cell
int cellSize; 
int infected; //1 = infected & 0 = healthy



void setup()
{
  size(500, 500); //standard canvas size for this course

  cellArray= new int [SIDE_LENGTH * SIDE_LENGTH]; //the array should adjust its length based on the amount of cells

  /**** 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 start the infection
  }
  cellSize = width/SIDE_LENGTH; //cell size is determined by canvas size and side length
  infected = 1; //1 for sick (0 for healthy)
  frameRate(1); //slow the framerate down
}


void draw()
{
  infectRandom(); //begin the infection at a random cell
  drawBoard(); //draw the board and colour the cells according to infected or healthy status
  spreadInfection(); //spread the infection from an infected cell to it's neighboring cells
  copyArray(); //deep copy of cellArray[] to prevent compounding effect???
  shallow(); //shallow copy of cellArray[] and tempSquares[]..... not sure why
}


/**** print out the board of cells ****/
void drawBoard()
{
  for (int i =0; i<SIDE_LENGTH; i++) //nested for loops to draw the rows and columns of cells
  {
    for (int j=0; j<SIDE_LENGTH; j++)
    {
      if (cellArray[i+j*SIDE_LENGTH] != infected) //check if cells are not infected
      {
        fill (cellColour[0]);//sets cell to healthy color
      } else //if cells are infected
      {
        fill (cellColour[1]);//sets cell to infected color
      }
      rect (i*cellSize, j*cellSize, cellSize, cellSize);//draws a grid of cells
    }
  }
}


/**** start the infection with a random cell***/
void infectRandom()
{
  if (cellArray[randomCell]!=infected) //simple if condition to start the infection
  {
    cellArray[randomCell] = infected; //set the random cell to a value of 1 (infected)
  }
}


/*** spread the infection to neighboring cells***/
void spreadInfection()
{
  for (int i = SIDE_LENGTH; i < cellArray.length-1; i++) //loop through cellArray[]
  {
    if (cellArray[i] == infected) //search for all infected cells
    {
      cellArray[i-1] = infected; //Left nightbor
      cellArray[i-SIDE_LENGTH] = infected; //Above neighbor

      /*** next two lines of code are commmented out because they don't funtion like I think they are supposed to***/
      //cellArray[i+1] = infected;
      //cellArray[i+10] = infected;
    }
  }
}