title: DFS Maze Generator
body: <p>I'm currently working on a maze generating program, using a depth first search.  I have the following code, but I can't figure out what I'm doing wrong.  I'm trying to have X's in between to say which ones are walls.  But it's overwriting them, and putting paths right next to each other.  Any help would be great.  Below is the output.</p>
  
  <pre><code>. . . # # # # # # # 
  . . . . # . . . . . 
  . . . . # . # # # . 
  . . # . # . . . # . 
  . . # # # # # . # # 
  . . . . # . . . # # 
  . # # . # . # # # # 
  . # # . . . # # # # 
  . . . . . # # # # # 
  # # # # # # # # # # 
  
  import java.util.Random;
  
  public class DepthFirstSearch {
  
      private Stack stack;
      public DepthFirstSearch() {}
      public char[][] DFS (int size, char[][] maze) {
  
          Random myRand = new Random();
          stack = new Stack(size);
          int x = myRand.nextInt(size);
          while (x % 2 == 0)
              x = myRand.nextInt(size);
          int y = myRand.nextInt(size);
          while (y % 2 == 0)
              y = myRand.nextInt(size);
  
          maze[x][y] = ' ';
          int total = (size * size) / 4;
          int visited = 1;
          int random[] = new int[4];
          int totalrand;
  
          while (visited &lt; total) {
              totalrand = 0;
              if (x &gt; 1 &amp;&amp; maze[x - 2][y] == 'X')
                  random[totalrand++] = 1;
              if (x &lt; size - 2 &amp;&amp; maze[x + 2][y] == 'X')
                  random[totalrand++] = 2;
              if (y &gt; 1 &amp;&amp; maze[x][y - 2] == 'X')
                  random[totalrand++] = 3;
              if (y &lt; size - 2 &amp;&amp; maze[x][y + 2] == 'X')
                  random[totalrand++] = 4;
  
              if (totalrand &gt; 0) {
                  switch(random[myRand.nextInt(totalrand)]) {
                      case 1: maze[x-2][y] = maze[x-1][y] = ' ';
                              x -= 2;
                              stack.push(x * size + y);
                              visited++;
                              break;
                      case 2: maze[x+2][y] = maze[x+1][y] = ' ';
                              x += 2;
                              stack.push(x * size + y);
                              visited++;
                              break;
                      case 3: maze[x][y-2] = maze[x][y-1] = ' ';
                              y -= 2;
                              stack.push(x * size + y);
                              visited++;
                              break;
                      case 4: maze[x][y+2] = maze[x][y+1] = ' ';
                              y += 2;
                              stack.push(x * size + y);
                              visited++;
                              break;
                  }
              }
              else {
                  int vert = stack.pop();
                  x = vert / size;
                  y = vert % size;
              }
          }
          return maze;
      }
  
  }
  </code></pre>