0

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
  • 2
    [What's a good algorithm to generate a maze?](http://stackoverflow.com/questions/38502/whats-a-good-algorithm-to-generate-a-maze) [Maze generation algorithm](https://en.wikipedia.org/wiki/Maze_generation_algorithm) – Bernhard Barker May 09 '17 at 10:25
  • Generate a random maze with one of the algorithms in the links that @Dukeling provides, add an entrance and exit, and then then remove all the dead ends to leave a random path. – Matt Timmermans May 10 '17 at 00:19

0 Answers0