这是我第一次尝试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;
}非常感谢你的提前!
发布于 2012-04-04 01:53:32
真正的问题可能是StackOverflowException,原因如下:
getUnvisitedNeighborCells创建一个新的tempCell,然后检查被访问的集合是否包含它。因为您没有覆盖单元格类上的equals方法,所以它将只检查对象引用,这将不会相同,因为您刚刚为tempCell创建了一个新实例,所以contains调用返回false,因此您从未真正将一个单元格标记为已访问。
要解决这个问题,您应该覆盖单元对象的equals方法(和hashCode),以便它比较x、y和方向,然后您的contains调用将返回true,您将摆脱无限遍历。
发布于 2012-04-04 01:56:12
这是一个方法问题,而不仅仅是编码问题或java问题。大多数学生都是这样开始的:编写所有在你脑海中的代码,并在完成后运行它。
但这是一种编程大师的方法,在它以这种方式工作之前,你可能需要多年的经验。
编程遵循“分而治之”的范式:当你有一个大问题时,你把它分成几个部分,然后解决小问题。如果有太多的big...you,你知道该怎么做:你再次破坏它。诸若此类。
这使您可以解决更具体的问题,更重要的是,可以检查是否每个小问题都得到了很好的解决。这就是方法。
没有人想要解决这个问题,因为有太多的问题需要一次解决。
https://stackoverflow.com/questions/9998585
复制相似问题