使用Eclipse的Java迷宫生成算法中的递归函数错误
这是我第一次尝试java,所以我决定编写一个迷宫生成算法。我已经用C#写了,所以我想写一些我以前做过的东西会很方便。我使用eclipse编写和编译Java 在我写完我的程序后,它没有给我任何错误或任何东西,我试着运行它,但它在控制台中给出了一个奇怪的输出:它说:使用Eclipse的Java迷宫生成算法中的递归函数错误,java,recursion,console,Java,Recursion,Console,这是我第一次尝试java,所以我决定编写一个迷宫生成算法。我已经用C#写了,所以我想写一些我以前做过的东西会很方便。我使用eclipse编写和编译Java 在我写完我的程序后,它没有给我任何错误或任何东西,我试着运行它,但它在控制台中给出了一个奇怪的输出:它说:在helloWorld.mazeAlgorithm(helloWorld.java:52)上的次数与我试图生成的迷宫的高度和宽度一样多。我不知道该做什么,在网上也找不到任何东西 (我知道我的代码可能真的很糟糕,但我有点匆忙,我只是想看看我
在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;
}
import java.applet.*;
导入java.awt.*;
导入java.util.ArrayList;
导入java.util.Random;
公共类helloWorld扩展小程序
{
最终int水平尺寸=10;
最终int垂直尺寸=10;
最终整数左=1;
最终积分上限=2;
最终整数右=3;
最终整数向下=4;
ArrayList访问的单元格;
公共空间涂料(图g)
{
初始化(g);
单元startCell=新单元();
startCell.x=0;
startCell.y=0;
mazeAlgorithm(startCell,g);
}
公共空间初始化(图形g)
{
对于(int x=0;x0)
{
cell nextCell=nextourcells.get(随机数(nextourcells.size()-1));
拆除墙壁(nextCell,g);
visitedCells.add(nextCell);
mazeAlgorithm(nextCell,g);
neighbourCells=getUnvisitedNeighbourCells(currentCell);
}
返回;
}
公共阵列列表GetUnvisitedNeightUrcells(cell cellToCheck)
{
ArrayList unvisitedNeightUrcells=新建ArrayList();
单元格tempCell=新单元格();
tempCell.x=cellToCheck.x-1;
tempCell.y=cellToCheck.y;
tempCell.direction=左;
if(cellToCheck.x>0&!visitedCells.contains(tempCell))
未访问的邻域资源。添加(tempCell);
tempCell.x=cellToCheck.x;
tempCell.y=cellToCheck.y-1;
tempCell.direction=向上;
if(cellToCheck.y>0&!visitedCells.contains(tempCell))
未访问的邻域资源。添加(tempCell);
tempCell.x=cellToCheck.x+1;
tempCell.y=cellToCheck.y;
tempCell.direction=右;
if(cellToCheck.x
非常感谢 真正的问题可能是StackOverflowException,原因如下: getUnvisitedNeighborCells创建一个新的tempCell,然后检查所访问的集合是否包含它。因为您没有覆盖cell类上的equals方法,所以它只会检查对象引用,这与您刚刚为tempCell创建了一个新实例不同,因此
包含调用返回false,因此您永远不会真正将单元格标记为已访问
要解决这个问题,您应该覆盖cell对象的equals方法(和hashCode),以便它比较x、y和方向,然后您的contains调用将返回true,您将摆脱无限遍历。这是一个方法问题,而不是编码问题或java问题。大多数学生都是这样开始的:写下你头脑中的所有代码,完成后再运行
但对于编程大师来说,这是一种方法,在这种方法发挥作用之前,您可能需要多年的经验
编程遵循“分而治之”的范式:当你有一个大问题时,你把它分解成小块,然后解决小问题。如果有太大的…你知道该怎么做:你再打破它。等等
这使您能够解决更具体的问题,而且最重要的是,您可以检查每个小问题是否都得到了很好的解决。这就是方法
没有人想解决这个问题,因为有太多的问题需要一次解决
- 将它们分开,隔离每个方法,在一个小程序中调用它,然后逐个查找您的问题。(你知道考试吗?)
- 跟踪您的程序