title: Game of Life C++, checking neighbors
body: <p>I am trying to write Conway's Game of Life. </p>
  
  <p>Unfortunately, when I go to check a block's neighbors, I always receive an error at certain elements in my arrays. Specifically at <code>grid[0][11]</code> it gives me one neighbor, however, I have it set so that I only ever add value to a variable called neighbors when a block around it is not space, or <code>' '</code> in my code. </p>
  
  <p>The conditions in the if statements, I believe, also account so that it does not go outside the array.</p>
  
  <p>The entire array is filled with <code>' '</code>, and I still get a value to neighbor despite this. </p>
  
  <p>I have been at this for hours and have found no solution. My code is still riddled with attempts to test, and I apologize for its lack of neatness. Thank you all in advance for any help you can give in solving my problem.</p>
  
  <pre><code>int _tmain(int argc, _TCHAR* argv[])
  {
      bool infloop = true;
      //Create the playing grid.
      char grid[HEIGHT][WIDTH];
      //Comment out later. Used for testing.
      for(int i=0; i&lt;75; i++)
          for(int j=0; j&lt;22; j++)
              grid[i][j] = ' ';
  
      //Create initial seed here. grid [x coordinate] [y coordinate].
      //grid [1][1] = '+'; grid [2][1] = '+'; grid [3][1] = '+';
  
      //Key. * is going to live. + is alive currently. 
      //- is going to die, and negative space is dead.
  
      //As Conway's Game of Life runs infinitely, create an infinite loop.
      while (infloop)
          generation(grid);
  
  
      cout &lt;&lt; endl;
      system("pause");
      return 0;
  }
  
  void generation(char grid[][WIDTH]) {
  
      int neighbors;
  
      /*Check each point on the grid for alive or dead. If it is, check the 
      surrounding neighbors and apply the game's rules.*/
      for(int x=0; x&lt;75; x++) {
          for(int y=0; y&lt;22; y++)
          {
  
              neighbors = 0;
              /*check all eight neighbors except for when outside of the 
              array.*/
              if((grid[x+1][y] != ' ') &amp;&amp; (grid[x+1][y] &lt; grid[HEIGHT][y])){
                  neighbors++; cout &lt;&lt; "A";
              }
  
              if((grid[x-1][y] != ' ') &amp;&amp; (grid[x-1][y] &gt; grid[-1][y])){
                  neighbors++; cout &lt;&lt; "E";
              }
  
              if((grid[x][y+1] != ' ') &amp;&amp; (grid[x][y+1] &lt; grid[x][WIDTH])){
                  neighbors++; cout &lt;&lt; "C";
              }
  
              if((grid[x][y-1] != ' ') &amp;&amp; (grid[x][y-1] &gt; grid[x][-1])){
                  neighbors++; cout &lt;&lt; "G";
              }
  
              if((grid[x+1][y+1] != ' ') &amp;&amp; (grid[x+1][y+1] &lt; grid[HEIGHT][WIDTH])){
                  neighbors++; cout &lt;&lt; "B";
              }
  
              grid[0][11] = ' '; grid[11][0] = ' ';
              if((grid[x-1][y-1] != ' ') &amp;&amp; (grid[x-1][y-1] &gt; grid[-1][-1])){
                  neighbors++; cout &lt;&lt; "F";
              }
  
              if((grid[x+1][y-1] != ' ') &amp;&amp; (grid[x+1][y] &lt; grid[HEIGHT][y]) &amp;&amp;
                  (grid[x][y-1] &gt; grid[x][-1])){
                      neighbors++; cout &lt;&lt; "H";
              }
  
              if((grid[x-1][y+1] != ' ') &amp;&amp; (grid[x-1][y] &gt; grid[-1][y])
                  &amp;&amp; (grid[x][y+1] &lt; grid[x][WIDTH])){
                      neighbors++; cout &lt;&lt; "D";
              }
  
              system("pause");
  
              cout &lt;&lt; neighbors;
  
              //Set a marker for each point according to neighbor amounts and key.
              if(grid[x][y] == '+' &amp;&amp; neighbors &lt; 2)
                  grid[x][y] = '-';
  
              if(grid[x][y] == '+' &amp;&amp; (neighbors == 2 || neighbors == 3))
                  grid[x][y] = '*';
  
              if(grid[x][y] == '+' &amp;&amp; neighbors &gt; 3)
                  grid[x][y] = '-';
  
              if(grid[x][y] == ' ' &amp;&amp; (neighbors == 3))
                  grid[x][y] = '*';
          }
      }
      for(int x=0; x&lt;75; x++){
          for(int y=0; y&lt;22; y++)
          {
              if(grid[x][y] == '*')
                  grid[x][y] = '+';
  
              if(grid[x][y] == '-')
                  grid[x][y] = ' ';
          }
      }
      system("pause");
      display(grid);
  }
  </code></pre>