title: Wrap-around Grid Errors- Subtraction only?
body: <p>I'm working on a program that picks a random location in a grid, checks its neighbors, and assigns the location the value of 1 if any of the neighbors are occupied. If none are occupied, the program picks a random neighbor and moves to it (switch case in diffuse()).</p>
  
  <pre><code>public class Frost {
  int width;
  int height;
  int density;
  int seed;
  int[][] grid = new int[this.width][this.height];
  Random randoma = new Random();
  </code></pre>
  
  <p>[Later...]</p>
  
  <pre><code>boolean diffuse(){
      int steps = 0;
      int diffusionSteps = this.height*this.width*2;
      int indexA = (int) randoma.nextInt(width);
      int indexB = (int) randoma.nextInt(height);
      while (steps &lt;= diffusionSteps)
      {
      if (grid[indexA][indexB] == 0) { //if empty
             if (this.checkNorth(indexA, indexB) || this.checkSouth(indexA,indexB) || this.checkEast(indexA,indexB) || this.checkWest(indexA,indexB))
              {
                  grid[indexA][indexB] = 1;
                          return true;
                      }
             else {
              steps++; //increase counter before moving and looping again
              int ofFour = this.randoma.nextInt(4);
          int tempA = indexA;
          int tempB = indexB;
                                  switch(ofFour)
                                      {
                                      case 1:
                                          if (tempA+1 &gt;= this.width)
                          indexA = (tempA+1)%this.width;
                      else indexA=tempA+1;
  
                                      case 2:
                                          if(tempB+1 &gt;= this.height)
                          indexB = (tempB+1)%this.height;
                      else indexB = tempB+1;
  
                                      case 3:
                                          if ((tempB-1) &lt;= 0)
                          indexB = (indexB-1)%this.height;
                      else indexB = tempB-1;
  
                                      case 4:
                                          if ((tempA-1) &lt;= 0)
                          indexA = (tempA-1)%this.width;
                      else indexA = tempA-1;
  
                                       }
                           }
                  }
  
      }
      if (steps&gt;=diffusionSteps){
          grid[indexA][indexB]=1;
          return true;
          }
  return false;
  }
  </code></pre>
  
  <p>I'm getting ArrayIndexOutofBounds: -1 errors for getSouth and getWest methods. I thought the problem might be the decrement operators, but I fiddled with them and nothing changed. Any insights would be appreciated.</p>
  
  <pre><code>boolean checkSouth(int indexA, int indexB)
  {
  --indexB;
  if (indexB &gt; 0)
      {return grid[indexA][indexB-1] == 1;}
      else    
      return grid[indexA][((indexB-1)%this.height)] == 1;
  }
  
  
  boolean checkWest(int indexA, int indexB)
  {
      --indexA;
      if (indexA &gt; 0)
          return grid[indexA][indexB-1] == 1;
      else
          return grid[(indexA)%this.width][indexB] == 1;
  }
  </code></pre>