我正在开发一个迷宫游戏。首先,我想生成一个空地图,然后生成一个路径,然后将障碍物放入,因为这可以确保迷宫可以解决。
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;
}
此代码未成功
我喜欢
之类的东西00000
01110
00010
00110
00000