I am developing a maze game. First I want generate an empty map then generate a path and then put the obstacles in, as this ensures the maze is solvable.
public int[][] generatePath(int[][] map){
int[][]gameMap= map;
int steps = 0;
//Choose random start position on grid
Random rand = new Random();
int rowStart = rand.nextInt(this.gridSize);
int colStart = rand.nextInt(this.gridSize);
//1 indicates a step in the path
int step = 1;
gameMap[rowStart][colStart] =step;
//length of the path along the 2d grid
int pathLength = 5;
int curRow =rowStart;
int curCol =colStart;
while(pathLength>-1){
//choose to move row in currentRow
int nextRow = rand.nextInt(2) + 1;
if(nextRow+ curRow <gridSize)curRow+=nextRow;
//choose to move row in currentRow
int nextCol = rand.nextInt(2) + 1;
if(nextCol+ curCol <gridSize)curCol+=nextCol;
gameMap[curRow][curCol]=step;
pathLength--;
}
return gameMap;
}
Not having success with this code
I'd like something like
00000
01110
00010
00110
00000