out of Boundry Error

can anyone tell me pls where is my error ? i think in line 268 ** **really appreciated

int[][][] space = new int[40][40][20];
boolean move_ok = true;
int time;
void setup() {
  size(1000, 1000, P3D);
  for (int i = 0; i < 200; i++) { // number of balls
    space[int(random(40))][int(random(40))][0] = 1;
  }
  space[25][20][0] = 2;// initial food on the ground

  // 4 existing buildings 
  for (int x = 10; x < 15; x++) {
    for (int y = 20; y < 25; y++) {
      for (int z = 0; z < 11; z++) {
        space[x][y][z] = 4;
      }
    }
  }
  for (int x = 35; x < 40; x++) {
    for (int y = 20; y < 25; y++) {
      for (int z = 0; z < 11; z++) {
        space[x][y][z] = 4;
      }
    }
  }
  for (int x = 20; x < 30; x++) {
    for (int y = 5; y < 8; y++) {
      for (int z = 0; z < 6; z++) {
        space[x][y][z] = 4;
      }
    }
  }
  for (int x = 20; x < 30; x++) {
    for (int y = 35; y < 38; y++) {
      for (int z = 0; z < 6; z++) {
        space[x][y][z] = 4;
      }
    }
  }
  for (int x = 20; x < 30; x++) {
    for (int y = 35; y < 38; y++) {
      for (int z = 6; z < 7; z++) {
        space[x][y][z] = 0;
      }
    }
  }
  // end of buildings

  time = -1;
}
//.............................................................

void draw() {

  if (millis() > time ) {// control the speed by millisecond
    move();
    time = millis() +100;
  }
  background(255);
  translate(width/2, height/2);

  pushMatrix();// camera setup
  scale(330);
  lights();
  popMatrix();
  rotateY(map(mouseX, 0, width, -PI, PI));
  rotateX(-map(mouseY, 0, height, -PI, PI));
  translate(-250, -250, -250);
  fill(#4F504F);
  pushMatrix();
  translate(450, 450, -15);
  box(1000, 1000, -10);
  popMatrix(); //end of camera setup


  //.......................................................
  // moving on grid

  for (int k = 0; k<20; k++) {
    for (int j = 0; j<40; j++) {
      for (int i = 0; i<40; i++) {
        pushMatrix();
        translate(20*i, 20*j, 20*k);
        switch(space[i][j][k]) { // switch between the grids 
        case 0:
          break;
        case 1: // ant
          fill(#25F018);
          noStroke();
          sphere(5);
          break;

        case 2: // food
          fill(#FF3E41);
          noStroke();
          box(20);
          break;

        case 3: // empty food
          pushMatrix();
          translate(-9, -9, -9);
          noStroke();
          fill(100);
          for (int m=0; m<10; m++) {
            pushMatrix();
            translate(2*m, 0, 0);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(0, 2*m, 0);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(0, 0, 2*m);
            box(2, 2, 2);
            popMatrix();
          }

          for (int n=-9; n<0; n++) {
            pushMatrix();
            translate(18, 0, -2*n);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(18, -2*n, 0);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(-2*n, 18, 0);
            box(2, 2, 2);
            popMatrix();
          }

          for (int p=-9; p<0; p++) {
            pushMatrix();
            translate(0, 18, -2*p);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(18, -2*p, 18);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(-2*p, 18, 18);
            box(2, 2, 2);
            popMatrix();
          }
          for (int r=-9; r<0; r++) {
            pushMatrix();
            translate(0, -2*r, 18);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(-2*r, 0, 18);
            box(2, 2, 2);
            popMatrix();

            pushMatrix();
            translate(18, 18, -2*r);
            box(2, 2, 2);
            popMatrix();
          }
          popMatrix();
          break;

        case 4: // building
          pushMatrix();         
          noFill();
          stroke(0);
          strokeWeight(1);
          box(20);
          popMatrix();
          break;

        case 5: // test
          pushMatrix();         
          fill(#4CB91E);
          box(20);
          popMatrix();
          break;
        }
        popMatrix();
      }
    }
  }
}
//.................................................

void move() {

  if ( !move_ok ) { 
    return;
  }
  //................................................


  int[][][] next = new int[40][40][20];
  for (int k = 0; k<20; k++) {
    for (int j = 0; j<40; j++) {
      for (int i = 0; i<40; i++) {
        switch(space[i][j][k]) {
        case 0:
          break;
        case 1: // ant behavior
          boolean be_food = false;
          int food_count = 0;
          int count=0;
          // reading (-1,0,1)of each grid it means one before and
          // one after to finding out if there is any sharing face  
          // with the food and changing to food if it is.
          for (int a = -1; a < 2; a++) {

              if (space[i+a][j][k] == 2) {
                be_food = true;
                next[i][j][k] = 2;
                food_count++;
            }

              if (space[i][j+a][k] == 2) {
                be_food = true;
                next[i][j][k] = 2;
                food_count++;
            }


              if (space[i][j][k+a] == 2) {
                be_food = true;
                next[i][j][k] = 2;
                food_count++;
            }
          }
          //...........................................................      


          // restricting the area around the food by 5 pixes in x,y only

          if ((next[i][j][k] == next[30][j][k])) {
            next[i][j][k] = 0;
          } 
          if ((next[i][j][k] == next[20][j][k])) {
            next[i][j][k] = 0;
          }
          if ((next[i][j][k] == next[i][15][k])) {
            next[i][j][k] = 0;
          }
          if ((next[i][j][k] == next[i][25][k])) {
            next[i][j][k] = 0;
          }
          // end of restriction
          //..........................................................................................

          if (( food_count >= 2) ) { // if the sharing face is more than 2 the food become empty 
            next[i][j][k] = 3;
          } 
          // this condition makes the movement of the agents 
          boolean done = false;  
          while (!done && !be_food  ) { // used the while loop cuz it never stops until the condition is true
            int a = int(random(-2, 2));
            int b = int(random(-2, 2));
            int c = int(random(-2, 2));
            if (next[i+a][j+b][k+c] >= space[i][j][k]) {
            next[i+a][j+b][k] = space[i][j][k];
            done = true;
            }
          }
          break;
        case 2:
          next[i][j][k] = 2;
          break;
        case 3:
          next[i][j][k] = 3;
          break;
        case 4:
          next[i][j][k] = 4;
          break;
        case 5:
          next[i][j][k] = 5;
          break;
        }
      }
    }
  }
  move_ok = false; // when not moving
  for (int k = 0; k<20; k++) {
    for (int j = 0; j<40; j++) {
      for (int i = 0; i<40; i++) {
        space[i][j][k] = next[i][j][k];
        move_ok = true;
      }
    }
  }
}  // end of move void        
//......................................................

Answers

  • edited July 2015

    I'd insert a

    println (i+a) ; 
    
    println (j+b); 
    
    println (k+c);
    

    directly before it. So you can debug.

    Answer

    I guess because you for-loop over the entire field and then sub and add random numbers you of course go over the boundarys of the field (lower and upper boundary depending from where you are (and from random)).

    you can if check like

    if (i+a>0 && i+a < 40 && 
        j+b .......................) 
    
    ..................
    

    Chrisir

  • Chrisir ur the best i will try and post here !! thanks

  • _vk_vk
    edited July 2015

    A shot in the dark. If i j k goes from 0 to array.length, then next[i+a] will fail. You need to avoid summing 2 at the two last indexes... As a b c are random -2,+2 the same goes for i =0 and a = -1 or -2

  • vk iam trying to do it just one moment

  • _vk_vk
    edited July 2015

    something like this should work (not tested)

    int index = constrain((i+a), 0, array.length);// perhaps array.length-1 not sure if it's inclusive...
    next[index];
    

    But maybe you need to do a different thing in those cases. I didn't run your code ...

  • Ok thanks _vk

  • edited July 2015

    This code block is completely a shot in the dark: :-O

    int a = int(random(-2, 2));
    int b = int(random(-2, 2));
    int c = int(random(-2, 2));
    
    if (next[i+a][j+b][k+c] >= space[i][j][k]) {
    }
    

    It pushes indices' boundaries as low as -2 & as high as array's length.
    Valid boundaries are 0 & length - 1 btW:
    https://Processing.org/reference/Array.html

    1 way to fix them is putting those a, b, c random() indices within a constrain():
    https://Processing.org/reference/constrain_.html

    int a = constrain(i + (int) random(-2, 2), 0, next.length - 1);
    int b = constrain(j + (int) random(-2, 2), 0, next[0].length - 1);
    int c = constrain(k + (int) random(-2, 2), 0, next[0][0].length - 1);
    
    if (next[a][b][c] >= space[i][j][k]) {
    }
    
  • Thanks so much Go ToLoop it was new to me

  •   int a = constrain(i + (int) random(-2, 2), 0, next.length - 1);
                int b = constrain(j + (int) random(-2, 2), 0, next[i].length - 1);
                int c = constrain(k + (int) random(-2, 2), 0, next[i][j].length - 1);
    
              if (next[a][b][c] >= space[i][j][k]){
                  next[a][b][c] = 1;
                  done = true;
                }
              }
    
  • still give me same error

  • Mr.Chrisir this way also gives me error

       int a = int(random(-2, 2));
                int b = int(random(-2, 2));
                int c = int(random(-2, 2));
                if (i+a<0 && i+a>40) {
                  if (j+b<0 && j+b>40) {
                    if (k+c<0 && k+c>20) {
                      next[i+a][j+b][k+c] = 1;
                      done = true;
                    }
                  }
                }
    
  • you still don't know what you are doing, right.......?

           if (i+a>=0 && i+a<40) {
               if (j+b>=0 && j+b<40) {
                 if (k+c>=0 && k+c<20) {
    
  • i did that too still the same

  • i want the line after this i think its error
    next[i+a][j+b][k+c] = 1;
    
  • post entire code

    maybe somebody else can help you

  • I fixed it thanks

  • edited July 2015 Answer ✓

    Nice you had that fixed! But would you care to tell us about what it was so others can learn from it too?

    Next time, be more specific than saying I had an error.
    Tell us what was the message and where it happened!
    And if it's about index, include that attempted index value too.

    And btW, here's an article about ArrayIndexOutOfBoundsException:
    http://forum.Processing.org/two/discussion/8070/why-do-i-get-an-arrayindexoutofboundsexception

  • ok With pleasure sir i will put the code

  • int a = int(random(-2, 2));
                int b = int(random(-2, 2));
                int c = int(random(-2,2));
                if (get_space(i+a, j+b, k+c) == 0) { // if next grid out of canvas it will return again
                  if (next[i+a][j+b][k+c] == 0) {
                    next[i+a][j+b][k+c] = 1; // the return of the agent 
                    done = true;
                  }
                }
    
    int get_space(int a, int b, int c) {
      if ( !check(a, b, c) ) { 
        return(-1);
      }
      return( space[a][b][c] );
    }
    
    
    boolean check(int a, int b, int c) {
      return( (a>=0)&&(a<40)&&(b>=0)&&(b<40)&&(c>=0)&&(c<20) );
    }
    
  • edited July 2015

    you might get better result when you press ctrl-t more often in processing

    it auto-formats the indents

    also with a function like check it is nice to have one over-all comment explanining the idea:

    // test whether all 3 values are within the array boundaries (3D array)
    

    that makes it better readable

Sign In or Register to comment.