I'm trying to write an algorithm to create mazes. The algorithm (DFS) looks like this:
- Start at a random cell.
- Mark the current cell as visited, get a list of the neighbors. For each neighbor, starting with a randomly selected neighbor:
If that neighbor hasn't been visited, remove the wall between this cell and that neighbor, and then recurse with that neighbor as the current cell.
But it produces mazes like this:

and I don't know why the algorithm creates full lanes instead of creating dead ends as well to make it look more like a maze instead of a one way road.
I suspected bad random selection, faulty backtracking or that the algorithm marks each cell as visited in the recursive step resulting in no dead ends as it can't go back to a cell but I can't narrow down the problem. Small mazes seem to produce the same problem.
Code:
std::vector<std::pair<int, int>> getAdjacentCells(Cell arr[N][M], int i, int j)
{
std::vector<std::pair<int, int>> neighbor_vec;
if(i-2 >= 0)
neighbor_vec.push_back(std::pair<int, int>(i-2, j));
if(i+2 < N)
neighbor_vec.push_back(std::pair<int, int>(i+2, j));
if(j-2 >= 0)
neighbor_vec.push_back(std::pair<int, int>(i, j-2));
if(j+2 < M)
neighbor_vec.push_back(std::pair<int, int>(i, j+2));
return neighbor_vec;
}
void genMaze(Cell arr[N][M], int i, int j)
{
// mark the current cell as visited
Cell &curCell = arr[i][j];
curCell.visited = true;
curCell.isWall = false;
// get a list of its neighbors
std::vector<std::pair<int, int>> neighbors = getAdjacentCells(arr, i, j);
// shuffle neighbor vector
std::random_shuffle( neighbors.begin(), neighbors.end() );
for(std::pair<int, int> coord : neighbors)
{
int x,y;
x = coord.first;
y = coord.second;
Cell &curNeighbor = arr[x][y];
if(!curNeighbor.visited) // remove wall inbetween given cell and neighbor
{
if(!(i-x)) // on the same column
{
if(j-y < 0) // right hand neighbor
{
arr[i][j+1].isWall = false;
return genMaze(arr, x,y);
}
else // left hand neighbor
{
arr[i][j-1].isWall = false;
return genMaze(arr, x,y);
}
}
else // not in the same column
{
if(i-x < 0) // bottom neighbor
{
arr[i+1][j].isWall = false;
return genMaze(arr, x,y);
}
else // top neighbor
{
arr[i-1][j].isWall = false;
return genMaze(arr, x,y);
}
}
}
}
arr[i][j].isEnd = true; // mark ending
}
The cell class consists only flags. It seems to be the same algorithm as post (although different problem): maze problem and Recursive backtracker algorithm
I would be grateful for any ideas or explanations.

rngfunction does), you'll have plenty of dead ends. - Sam Varshavchik