ArrayIndexOutOfBounds

Hello guys,

I am new to processing and I’ve been recently trying to learn from some youtube tutorials.
However, I encountered a problem and hope someone could help me to fix it.
This is the link of the video I watched: https://www.youtube.com/watch?v=MKMJz8QYmAU

So it’s a CA (Game of Live) example done by processing, and I followed the steps properly. However, it wasn’t working after those lines:

    if( grid[(x+cols-1)%cols] [(y+rows-1)&rows].type ==1 ) count ++;
    if(grid[(x+cols)%cols][(y+rows-1)&rows].type ==1) count ++;
    if(grid[(x+cols+1)%cols][(y+rows-1)&rows].type ==1) count ++;
    if(grid[(x+cols-1)%cols][(y+rows)&rows].type ==1) count ++;
    if(grid[(x+cols+1)%cols][(y+rows)&rows].type ==1) count ++;
    if(grid[(x+cols-1)%cols][(y+rows+1)&rows].type ==1) count ++;
    if(grid[(x+cols)%cols][(y+rows+1)&rows].type ==1) count ++;
    if(grid[(x+cols+1)%cols][(y+rows+1)&rows].type ==1) count ++;

And it gave me an error message: ArrayIndexOutOfBoundsException: 100.
Strangely, it looks fine and works properly in his computer.

Here is my code:

import peasy.*;
import toxi.geom.*;

PeasyCam cam;

int cols = 100;
int rows = 100;


CA grid[][] = new CA[cols][rows];

void setup(){
 size(800,600,P3D);
 cam = new PeasyCam(this,100); 


 //Initilise the grids
 for(int i=0; i<cols; i++){
   for(int j=0; j<rows; j++){
   
     Vec3D ptLoc = new Vec3D (i*10, j*10,0);
     grid[i][j] = new CA(ptLoc,i,j);
   
   
   
    }
  }
}



void draw(){

background(0);
stroke(255);
fill(255,20);
//rect(0,0,600,600);



for(int i=0; i<cols; i++){
for(int j=0; j<rows; j++){

 grid[i][j].run(); 
 }
}
 

for(int i=0; i<cols; i++){
    for(int j=0; j<rows; j++){

    grid[i][j].update(); 
  }
}

 
 }




  class CA{

  Vec3D loc; //a point

  int x;
  int y;
  int type =0;


 int futType = 0;

 CA(Vec3D _loc, int _x,int _y){
 loc = _loc;
 x = _x;
 y = _y;

float rnd = random(100);
if(rnd <50){
  type = 1;
}
}




  void run(){

    display();
    evalN();
  }



  void update(){

    type = futType;


  }




  void evalN(){

        int count = 0;
        if( grid[(x+cols-1)%cols] [(y+rows-1)&rows].type ==1 ) count ++;
        if(grid[(x+cols)%cols][(y+rows-1)&rows].type ==1) count ++;
        if(grid[(x+cols+1)%cols][(y+rows-1)&rows].type ==1) count ++;
        if(grid[(x+cols-1)%cols][(y+rows)&rows].type ==1) count ++;
        if(grid[(x+cols+1)%cols][(y+rows)&rows].type ==1) count ++;
        if(grid[(x+cols-1)%cols][(y+rows+1)&rows].type ==1) count ++;
        if(grid[(x+cols)%cols][(y+rows+1)&rows].type ==1) count ++;
        if(grid[(x+cols+1)%cols][(y+rows+1)&rows].type ==1) count ++;
  
  

      if(type ==1 && count<2){
        futType = 0;
      }
  
      if(type ==1 && count <=3 && count >=2){
        futType =1;
      }
  
  
      if(type ==1 && count>3){
        futType =0;
      }
  
      if(type ==0 && count==3){
        futType =1;
      }
  
  
  

}



  void display(){
    if(type == 1){
      stroke(255);
      strokeWeight(2);
      point(loc.x,loc.y);
    }
  }
}

Really appreciate it!

1 Like
  void evalN() {
    final int xcols = x + cols, yrows = y + rows;
    int count = 0;

    if (grid[(xcols - 1) % cols][(yrows - 1) % rows].type != 0)  ++count;
    if (grid[xcols       % cols][(yrows - 1) % rows].type != 0)  ++count;
    if (grid[(xcols + 1) % cols][(yrows - 1) % rows].type != 0)  ++count;
    if (grid[(xcols - 1) % cols][yrows       % rows].type != 0)  ++count;
    if (grid[(xcols + 1) % cols][yrows       % rows].type != 0)  ++count;
    if (grid[(xcols - 1) % cols][(yrows + 1) % rows].type != 0)  ++count;
    if (grid[xcols       % cols][(yrows + 1) % rows].type != 0)  ++count;
    if (grid[(xcols + 1) % cols][(yrows + 1) % rows].type != 0)  ++count;

    if (type != 0)
      if (count < 2 | count > 3)  futType = 0;
      else                        futType = 1;
    else if (count == 3)          futType = 1;
  }
1 Like