首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Eclipse的Java迷宫生成算法中的递归函数错误

使用Eclipse的Java迷宫生成算法中的递归函数错误
EN

Stack Overflow用户
提问于 2012-04-04 01:45:56
回答 2查看 211关注 0票数 0

这是我第一次尝试java,所以我决定写一个迷宫生成算法。我已经用C#写过了,所以我想写一些我以前做过的事情会很方便。我使用eclipse编写和编译Java。

在我写完我的程序后,它没有给我任何错误或任何东西,我试图运行它,但它在控制台上给出了一个奇怪的输出:at helloWorld.mazeAlgorithm(helloWorld.java:52)的次数与我试图生成的迷宫的高度和宽度一样多。我不知道该怎么做,我在网上也找不到任何东西。

(我知道我的代码可能真的很糟糕,但我有点着急,我只是想看看我能不能快速写点东西。)这就是它:

代码语言:javascript
复制
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;
}

非常感谢你的提前!

EN

回答 2

Stack Overflow用户

发布于 2012-04-04 01:53:32

真正的问题可能是StackOverflowException,原因如下:

getUnvisitedNeighborCells创建一个新的tempCell,然后检查被访问的集合是否包含它。因为您没有覆盖单元格类上的equals方法,所以它将只检查对象引用,这将不会相同,因为您刚刚为tempCell创建了一个新实例,所以contains调用返回false,因此您从未真正将一个单元格标记为已访问。

要解决这个问题,您应该覆盖单元对象的equals方法(和hashCode),以便它比较x、y和方向,然后您的contains调用将返回true,您将摆脱无限遍历。

票数 1
EN

Stack Overflow用户

发布于 2012-04-04 01:56:12

这是一个方法问题,而不仅仅是编码问题或java问题。大多数学生都是这样开始的:编写所有在你脑海中的代码,并在完成后运行它。

但这是一种编程大师的方法,在它以这种方式工作之前,你可能需要多年的经验。

编程遵循“分而治之”的范式:当你有一个大问题时,你把它分成几个部分,然后解决小问题。如果有太多的big...you,你知道该怎么做:你再次破坏它。诸若此类。

这使您可以解决更具体的问题,更重要的是,可以检查是否每个小问题都得到了很好的解决。这就是方法。

没有人想要解决这个问题,因为有太多的问题需要一次解决。

  • 将它们分开,隔离每个方法,在一个小程序中调用它,然后逐个找到您的问题。(您了解测试吗?)
  • 跟踪您的程序(将system.out.println放入)您认为有必要打印值的任何地方。或调试。但是你必须理解并且能够和你的程序一起运行。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9998585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档