作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在努力用 Java 实现递归除法算法的工作形式,以便随机生成迷宫。我正在尝试这样做:
http://weblog.jamisbuck.org/2011/1/12/maze-generation-recursive-division-algorithm
我不想复制粘贴代码,但我只是不明白我应该如何递归地划分迷宫,同时在其中创建洞而不是用新墙填充这些洞。
老实说,此时我觉得有点不好意思发布我当前的代码,但如果有人想看的话我会的。
我真的很想看到一个代码示例,我可以在其中理解正在发生的事情,因为我发现的所有其他内容都是用 Ruby、Python 或 Javascript 编写的,并且使用糟糕的变量名构建,几乎没有注释。我可以弄清楚正在发生的事情的要点,但它并没有完全解决我的问题。
我总是让墙壁紧挨着彼此,或者放置不正确,以至于它们不能正确地平分墙壁。
为了进一步引用,我正在处理一个项目。
class Maze
{
int[][] mazeTiles = new int[width/50][height/50];
boolean completion;
int HORIZONTAL = 0;
int VERTICAL = 1;
int slice;
int xMin, xMax;
int yMin, yMax;
int xRand;
int yRand;
int distX,distY;
int yPoint;
int horizontalSize;
int verticalSize;
Maze()
{
completion = false;
xMin = 0;
yMin = 0;
slice = 50;
verticalSize = mazeTiles[0].length;
horizontalSize = mazeTiles.length;
xMax = horizontalSize;
yMax = verticalSize;
}
void generateMaze()
{
divide(xMin, yMin, xMax, yMax);
// need to variables based on horizontal or vertical
}
void drawMaze()
{
for (int i = 0; i < mazeTiles.length; i++)
{
for (int j = 0; j < mazeTiles[0].length; j++)
{
if (mazeTiles[i][j] == 0)
{
fill(255, 0, 0);
rect(i*50, j*50, 50, 50);
fill(0);
} else if (mazeTiles[i][j] ==1)
{
fill(0, 255, 0);
rect(i*50, j*50, 50, 50);
fill(0);
}
}
}
}
void divide(int xStart, int yStart, int xEnd, int yEnd)
{
distX = xEnd - xStart;
distY = yEnd - yStart;
int yPoint = (int)random(4, yEnd-4);
int xPoint = (int)random(4, xEnd-4);
if(distX > distY)
{
slice = VERTICAL;
}
else if(distY > distX)
{
slice = HORIZONTAL;
}
else
{
slice = (int)random(HORIZONTAL, VERTICAL+1);
}
if (slice == HORIZONTAL)
{
wall(slice, xStart, yPoint, xEnd, yEnd);
if (distX >= 2 || distY >= 2)
{
println("HORIZONTAL");
divide(xStart,yStart,xEnd,yEnd-yPoint); //Top // yEnd - random y value chosen
divide(xStart,yStart+yPoint,xEnd,yEnd); //Bottom
}
else
{
return;
}
}
if (slice == VERTICAL)
{
wall(slice, xPoint, yStart, xEnd, yEnd);
if (distX >= 2 || distY >= 2)
{
println("VERTICAL");
divide(xStart+xPoint,yStart,xEnd,yEnd); //Right
divide(xStart,yStart,xEnd - xPoint,yEnd); //Left
}
else
{
return;
}
}
}
void resetMaze()
{
for (int i = 0; i < mazeTiles.length; i++)
{
for (int j = 0; j < mazeTiles[0].length; j++)
{
mazeTiles[i][j] = 0;
}
}
}
void wall(int direction, int startX, int startY, int lenX, int lenY)
{
if (lenY >= 0 && lenX >= 0 && lenX <= horizontalSize && lenY <= verticalSize && startX >= 0 && startY >= 0 && startX < horizontalSize && startY < verticalSize)
{
if (direction == HORIZONTAL)
{
for (int i = startX; i < lenX; i++)
{
int j = startY;
mazeTiles[i][j] = 1;
}
cutHole((int)random(startX+1, lenX-1), startY, mazeTiles);
}
if (direction == VERTICAL)
{
for (int i = startY; i < lenY; i++)
{
int j = startX;
mazeTiles[j][i] = 1;
}
cutHole(startX, (int)random(startY+1, lenY-1), mazeTiles);
}
}
}
void cutHole(int xLoc, int yLoc, int[][] grid)
{
grid[xLoc-1][yLoc-1] = 0;
}
}
最佳答案
相关的递归 Ruby 代码位于 your link在 Java 中是:
private static final int HORIZONTAL = 1;
private static final int VERTICAL = 2;
private static final int S = 1;
private static final int E = 2;
private Random rand = new Random();
private void divide(int[][] grid, int x, int y, int width, int height, int orientation) {
if(width < 2 || height < 2) {
return;
}
boolean horizontal = orientation == HORIZONTAL;
int wx = x + (horizontal ? 0 : rand.nextInt(width - 2));
int wy = y + (horizontal ? rand.nextInt(height - 2) : 0);
int px = wx + (horizontal ? rand.nextInt(width) : 0);
int py = wy + (horizontal ? 0 : rand.nextInt(height));
int dx = horizontal ? 1 : 0;
int dy = horizontal ? 0 : 1;
int length = horizontal ? width : height;
int dir = horizontal ? S : E;
for(int i = 0; i < length; i++) {
if(wx != px || wy != py) {
grid[wy][wx] |= dir;
}
wx += dx;
wy += dy;
}
int nx = x;
int ny = y;
int w = horizontal ? width : wx - x + 1;
int h = horizontal ? wy - y + 1 : height;
divide(grid, nx, ny, w, h, chooseOrientation(w, h));
nx = horizontal ? x : wx + 1;
ny = horizontal ? wy + 1 : y;
w = horizontal ? width : x + width - wx - 1;
h = horizontal ? y + height - wy - 1 : height;
divide(grid, nx, ny, w, h, chooseOrientation(w, h));
}
private int chooseOrientation(int w, int h) {
if(w < h) {
return HORIZONTAL;
} else if (h < w) {
return VERTICAL;
} else {
return rand.nextInt(2) + 1;
}
}
关于java - java中如何实现递归除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000632/
我是一名优秀的程序员,十分优秀!