这是我第一次尝试java,所以我决定写一个迷宫生成算法。我已经用C#写过了,所以我觉得写一些我以前做过的东西会很方便。我使用eclipse来编写和编译Java。
在我写完程序后,它没有给我任何错误,我试着运行它,但它在控制台中给出了一个奇怪的输出:它说:at helloWorld.mazeAlgorithm(helloWorld.java:52)
的次数与我试图生成的迷宫的高度和宽度一样多。我不知道该怎么办,在网上也找不到任何东西。
(我知道我的代码可能真的很糟糕,但我有点着急,我只是想看看我能不能快速写点什么。)这是:
import java.applet.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class helloWorld extends Applet
{
final int horizontalSize = 10;
final int verticalSize = 10;
final int LEFT = 1;
final int UP = 2;
final int RIGHT = 3;
final int DOWN = 4;
ArrayList<cell> visitedCells;
public void paint(Graphics g)
{
initialize(g);
cell startCell = new cell();
startCell.x = 0;
startCell.y = 0;
mazeAlgorithm(startCell, g);
}
public void initialize (Graphics g)
{
for (int x = 0; x< horizontalSize*10+1; x=x+10)
{
g.drawLine(x, 0, x, verticalSize*10);
}
for (int y = 0; y < verticalSize*10+1; y=y+10)
{
g.drawLine(0, y, horizontalSize*10, y);
}
visitedCells = new ArrayList<cell>();
cell currentCell = new cell();
currentCell.x = 0;
currentCell.y = 0;
visitedCells.add(currentCell);
}
public void mazeAlgorithm(cell currentCell, Graphics g)
{
ArrayList<cell> neighbourCells = getUnvisitedNeighbourCells(currentCell);
while(neighbourCells.size()> 0)
{
cell nextCell = neighbourCells.get(randomNumber(neighbourCells.size()-1));
removeWall(nextCell, g);
visitedCells.add(nextCell);
mazeAlgorithm(nextCell, g);
neighbourCells = getUnvisitedNeighbourCells(currentCell);
}
return;
}
public ArrayList<cell> getUnvisitedNeighbourCells(cell cellToCheck)
{
ArrayList<cell> unvisitedNeighbourCells = new ArrayList<cell>();
cell tempCell = new cell();
tempCell.x = cellToCheck.x-1;
tempCell.y = cellToCheck.y;
tempCell.direction = LEFT;
if (cellToCheck.x > 0 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
tempCell.x = cellToCheck.x;
tempCell.y = cellToCheck.y-1;
tempCell.direction = UP;
if (cellToCheck.y > 0 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
tempCell.x = cellToCheck.x+1;
tempCell.y = cellToCheck.y;
tempCell.direction = RIGHT;
if (cellToCheck.x < horizontalSize-1 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
tempCell.x = cellToCheck.x;
tempCell.y = cellToCheck.y+1;
tempCell.direction = DOWN;
if (cellToCheck.y < verticalSize-1 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
return unvisitedNeighbourCells;
}
public void removeWall(cell Cell, Graphics g)
{
g.setColor(Color.WHITE);
switch(Cell.direction)
{
case LEFT:
g.drawLine(Cell.x*10, Cell.y*10-10, Cell.x*10, Cell.y*10);
break;
case UP:
g.drawLine(Cell.x*10-10, Cell.y*10, Cell.x*10, Cell.y*10);
break;
case RIGHT:
g.drawLine(Cell.x*10-10, Cell.y*10-10, Cell.x*10-10, Cell.y*10);
break;
case DOWN:
g.drawLine(Cell.x*10-10, Cell.y*10-10, Cell.x*10, Cell.y*10-10);
break;
}
return;
}
private int randomNumber(int max)
{
Random random = new Random();
return random.nextInt(max);
}
}
class cell
{
public int x;
public int y;
public int direction;
}
非常感谢!
这是一个方法问题,而不是编码问题或java问题。大多数学生都是这样开始的:写下你脑子里的所有代码,完成后运行。
但这是编程大师的一种方式,在以这种方式工作之前,你可能需要多年的经验。
编程遵循"分而治之"的范式:当你有一个大问题时,你把它分解成碎片,然后解决小问题。如果有太大的。。。你知道该怎么做:你又把它打破了。等等。
这可以让你解决更具体的问题,更重要的是,你可以检查每个小问题是否都得到了很好的解决。这就是方法。
没有人想解决这个问题,因为一次要解决的问题太多了。
- 把它们分开,隔离每一个方法,在一个小程序中调用它,然后逐个找到你的问题。(你知道考试吗?)
- 跟踪您的程序(putsystem.out.println)中您认为有必要打印值的每一个位置。或者调试。但是你必须理解并能够运行你的程序
真正的问题可能是StackOverflowException,原因如下:
getUnvisitedNeighborCells创建一个新的tempCell,然后检查被访问的集合是否包含它。因为您没有重写单元格类上的equals方法,所以它只会检查对象引用,这将不一样,因为您刚刚为tempCell创建了一个新实例,所以contains
调用返回false,所以您永远不会真正将单元格标记为已访问。
要解决此问题,您应该覆盖单元格对象的equals方法(和hashCode),以便它比较x、y和方向,然后您的contains调用将返回true,您将退出无限遍历。