Algorithmic error in java maze generator
<p>I'm trying to write a program that generates 2-d mazes. The body of the maze is a 2-d int array. The border cells have a value of 2. Blocked cells (walls) have a value of 1, and empty cells (paths) have a value of 0. Initially, I am setting the value of all cells to 1. Then, starting from a random column in the top row, I move through the maze setting the current cell to 0 until I reach the bottom row. </p>

<p>This all worked fine, except that, rather than a single-line path, I often ended up with wide areas of 0's. So, I tried to add to the if statements to prevent it from marking cells zero if the cells around it were already zero. Unfortunately there is some flaw in my logic, causing the program to run forever without printing anything when I run it. Please help me identify that flaw. </p>

<p>I'm fairly new to programming, and am doing this as a learning exercise, so I'm also open to other algorithmic suggestions. Thanks </p>

<p>My code: </p>

<pre><code>package RoboProj;

import java.util.Random;


public class Maze {
public int[][] grid;
final int width, height;

public Maze() {
    width = 20;
    height = 20;
    grid = new int[width][height];

    makeMaze();
}


public void makeMaze() {
    //* empty = 0, wall = 1, border = 2, travelled =3;

    //mark borders
    for (int curr = 0; curr &lt; height; curr++) {
        grid[0][curr] = 2;  //top
        grid[curr][0]=2; //left
        grid[height -1][curr] = 2; //bottom
        grid[curr][width-1] = 2; //right
    }
    //initially mark all cells as walls
    for (int row = 1; row &lt; height-1; row++){
        for (int col = 1; col &lt; width-1; col++){
            grid[row][col]=1;
        }      
    }

    int row = 0;
    Random r = new Random();
    int col =  r.nextInt(width);

    grid[row][col] = 0;

    while (row != height-1){
        int next = r.nextInt(4);
        if (next == 0 &amp;&amp; row-1 &gt; 0 &amp;&amp; grid[row-1][col-1] == 1 &amp;&amp; grid[row-1][col+1] == 1){
            grid[row-1][col]=0;
            row = row-1;
          //  System.out.print(next);
        }
        if (next == 1 &amp;&amp; grid[row+1][col-1] == 1 &amp;&amp; grid[row+1][col+1] == 1){
            grid[row+1][col]=0;
            row = row+1;
           // System.out.print(next);
        }      
        if (next == 2&amp;&amp; col-1 &gt; 0 &amp;&amp; grid[row+1][col-1] == 1 &amp;&amp; grid[row-1][col-1] == 1){
            grid[row][col-1]=0;
            col = col-1;
                  //     System.out.print(next);
        } 
        if (next == 3 &amp;&amp; col+1 &lt; width-1 &amp;&amp; grid[row-1][col+1] == 1 &amp;&amp; grid[row+1][col+1] == 1){
            grid[row][col+1]=0;
            col = col+1;
                   //     System.out.print(next);
        } 
    }
}
}
</code></pre>

<p>@ Anupam Saini: I'm looking for something like this, where the "pathway" is never more than one cell wide.</p>

<pre><code>1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
</code></pre>
