如何在java

时间:2018-02-27 03:28:25

标签: java algorithm recursion division maze

我一直在努力在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;
  }
}

1 个答案:

答案 0 :(得分:1)

Java中your link的相关递归Ruby代码是:

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;
    }
}