Ok, so i want t generate a random maze for my maze game. I have hardcoded the maze like this, and have several different versions that i would like to be able to have spawned at random.
好的,所以我想为我的迷宫游戏生成随机迷宫。我已经像这样对迷宫进行了硬编码,并且有几个不同的版本,我希望能够随机生成。
public Maze() {
this.mazeMap1 = new BlockType[][] {
{H, H, H, H, H, H, H, H, H, H, H, H, H, H, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, H, E, E, H, E, E, H, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, E, E, E, E, E, E, E, E, E, E, E, E, E, H},
{H, H, H, H, H, H, H, H, H, H, H, H, H, H, H}
};
}
then i create a getter and return the maze
然后我创建一个吸气剂并返回迷宫
public BlockType[][] getMazeMap() {
return mazeMap2;
}
Then i have a class 'Board' where i make the maze
然后我有一个班级'董事会',我在那里制作迷宫
private void makeBoard() {
blocks = new Maze().getMazeMap();
}
If i would have, say 10 different hardcoded mazes, how would i generate one at random?
如果我愿意,比如10个不同的硬编码迷宫,我将如何随机生成一个?
2 个解决方案
#1
2
You need a collection of your maze maps. Having mazemap1, mazemap2, etc. doesn't (easily) allow you to pick one.
你需要一组你的迷宫地图。拥有mazemap1,mazemap2等不会(轻松)允许你选择一个。
ArrayList<Block[][]> mazemaps = new ArrayList<>();
mazemaps.add( new BlockType[][] { ... } ); // with all your data
mazemaps.add( new BlockType[][] { ... } ); // second map data
Then you can pick one:
然后你可以选择一个:
int maze = new Random().nextInt(mazemaps.size());
return mazemaps.get(maze);
(There are plenty of other things you can maybe do better, but this is a start)
(还有很多其他的东西可以做得更好,但这是一个开始)
#2
0
Here is my take:
这是我的看法:
You first need to generate the exit way. By exit way, I mean the way that goes from start to finish.
首先需要生成退出方式。退出方式,我的意思是从开始到结束的方式。
Then:
- Generate a false way that leads to nowhere
- Generate the wall surrounding the false way
- Go back to step #1
产生一种无处可逃的虚假方式
围绕虚假的方式生成围墙
回到第1步
After you generate every single cell as either wall or passable terrain, run Djikstra/A* algorithm and prove that the maze is actually solvable.
将每个单元格生成为墙或可通过地形后,运行Djikstra / A *算法并证明迷宫实际上是可解的。
#1
2
You need a collection of your maze maps. Having mazemap1, mazemap2, etc. doesn't (easily) allow you to pick one.
你需要一组你的迷宫地图。拥有mazemap1,mazemap2等不会(轻松)允许你选择一个。
ArrayList<Block[][]> mazemaps = new ArrayList<>();
mazemaps.add( new BlockType[][] { ... } ); // with all your data
mazemaps.add( new BlockType[][] { ... } ); // second map data
Then you can pick one:
然后你可以选择一个:
int maze = new Random().nextInt(mazemaps.size());
return mazemaps.get(maze);
(There are plenty of other things you can maybe do better, but this is a start)
(还有很多其他的东西可以做得更好,但这是一个开始)
#2
0
Here is my take:
这是我的看法:
You first need to generate the exit way. By exit way, I mean the way that goes from start to finish.
首先需要生成退出方式。退出方式,我的意思是从开始到结束的方式。
Then:
- Generate a false way that leads to nowhere
- Generate the wall surrounding the false way
- Go back to step #1
产生一种无处可逃的虚假方式
围绕虚假的方式生成围墙
回到第1步
After you generate every single cell as either wall or passable terrain, run Djikstra/A* algorithm and prove that the maze is actually solvable.
将每个单元格生成为墙或可通过地形后,运行Djikstra / A *算法并证明迷宫实际上是可解的。