title: 2D Arrays Minesweeper Random Mine Objects & Surrounding Mines
body: <p>Another couple of questions for an array of arrays minesweeper game</p>
  
  <p>1.) My code (with Merlin's tutoring) is outputting a correct number of mines. Sometimes. How can I fix this? <strong>ANSWERED</strong></p>
  
  <p>Example:</p>
  
  <pre><code>Enter width of board: 5
  Enter height of board: 5
  Enter number of mines: 5
     0  1  2  3  4
  0| .  .  .  .  . 
  1| .  .  .  .  . 
  2| .  .  .  .  * 
  3| .  .  *  .  . 
  4| *  .  .  *  * 
  
  Enter width of board: 5
  Enter height of board: 5
  Enter number of mines: 5
     0  1  2  3  4
  0| *  .  .  .  . 
  1| .  .  *  .  . 
  2| .  .  .  .  . 
  3| .  *  .  .  . 
  4| *  .  .  .  * 
  </code></pre>
  
  <p>MineField Code:</p>
  
  <pre><code>public MineField(int w, int h, int m)
  {
      Random r = new Random();
      mineField = new State[w][h];
      surroundingMines = new int[w][h];
      initialiseMineField = new int[w][h];
      traceOn = true; //set to false before submitting
      width = w;
      height = h;
      mineCount = m;
      for (int i = 0; i &lt; w; i++)
      {
          for (int j = 0; j &lt; h; j++)
          {
              mineField[i][j] = State.COVERED;
          }
      }
      for (int k = 0; k &lt; m; k++)
      {
          while (true)
          {
              int a = r.nextInt(w);
              int b = r.nextInt(h);
              if (mineField[a][b] != State.MINED)
              {
                  break;
              }
          }
          mineField[r.nextInt(w)][r.nextInt(h)] = State.MINED;
      }
  }
  </code></pre>
  
  <p>State enums:</p>
  
  <pre><code>public enum State {
      /** Cell is mined */
      MINED,
      /** Player uncovered (hit) mine */
      EXPLODED,
      /** Cell is not mined but unexplored */
      COVERED,
      /** Cell is not mined and has been explored */
      CLEARED,
      /** Cell has been flagged and contains a mine */
      FLAGGED,
      /** Cell has been flagged but does not contain a mine */
      MISFLAGGED;
  </code></pre>
  
  <p>Required state chars:</p>
  
  <pre><code>public char toChar(int surroundingMines, boolean showTruth) {
      switch (this) {
          case EXPLODED: return '+';
          case COVERED: return '.';
          case CLEARED: return surroundingMines == 0 ? ' ' : String.valueOf(surroundingMines).charAt(0);
          case FLAGGED: return 'F';
          case MINED: return showTruth ? '*' : '.';
          default /* MISFLAGGED */: return showTruth ? 'x' : 'F';
      }
  }
  </code></pre>
  
  <p>2.) How am I able to display the mines that surrounds a cell within my array output of say:</p>
  
  <pre><code>   0  1  2  3  4
  0| .  .  .  .  . 
  1| .  .  .  .  . 
  2| .  .  .  .  * 
  3| .  .  *  .  . 
  4| *  .  .  *  * 
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>   0  1  2  3  4
  0| .  .  .  .  . 
  1| .  .  .  1  1 
  2| .  .  1  2  * 
  3| 1  2  *  2  2 
  4| *  1  2  *  * 
  </code></pre>