Java迷宫求解器



我有两个对象。一个叫Cell,一个叫FinalSolve。该程序的目的是通过在每个细胞中循环寻找出口来解决迷宫。

cell对象表示迷宫中的cell,它有4个实例变量,1个构造函数和4个返回指定实例变量的其他实例方法。

    //the variables
    private int value;
    private int column;
    private int row;
    private char symbol;
    private int nuValue;


    //the methods
    public value()
    {
    return value
    }

以此类推。我的迷宫对象包含如下6个实例方法:

  • 马克路径
  • toString
  • findNeighbors
  • findCellWithValue
  • setTheValue

它们有一个非常直接的名字,它们返回的正是我所期望的。

解决

是利用该算法,综合利用上述所有方法来解决迷宫的方法

public void solve()
{
    Cell[] entrance = findCellWithValue(250);
    Cell[] neighborsOf = findNeighbors(entrance[0]);
    for(Cell neighbor : neighborsOf)
    {
        setTheValue(1, neighbor);
    }
    int moveCount = 1;
    boolean foundExit = false;
            try{
    while(!foundExit && moveCount <= 200)
    {
        Cell[] theCells = findCellWithValue(moveCount);
        for(Cell justOne : theCells)
        {
            if(justOne.symbol() == '!')
            {
                foundExit = true;
                markPath();
                                    break;
            }
            else
            {
                Cell[] moreNeighbors = findNeighbors(justOne);
                for(Cell prostie : moreNeighbors)
                {
                    if(prostie.value() == 0)
                    {
                    setTheValue(moveCount+1, prostie);
                    }
                }
            }
        }
        moveCount++;
    }
            }catch(Exception e)
            {
            System.out.println("" + moveCount);
            System.out.println("" + e.getMessage());
            }

}
马克路径

这个方法在solve方法解决程序时被调用,它标记了从出口到入口的路径。由于我在程序中遇到了一个错误,这个方法仍在进行中,它只是打印"Hello"

public void markPath()
{
    System.out.println("Hello");
}

findNeighbors

在迷宫中查找指定单元格的邻居(即空白区域),并将其返回到数组中。

    public Cell[] findNeighbors(Cell theCell)
    {
        Cell[] neighborsCell = new Cell[1];
        int neighbors = 0;
        int column = theCell.column();
        int row = theCell.row();
                    if(column - 1 < 0);
                    else
                    {
                        char some = maze[column-1][row].symbol();
                        if(some == ' ')
                        {
                            if(neighbors == neighborsCell.length)
                            {
                                Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                                System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                                neighborsCell = biggerArray;
                            }
                            neighborsCell[neighbors] = maze[column-1][row];
                        }
                    }
                    if(column + 1 > 20 );
                    else
                    {
                        char someElse = maze[column+1][row].symbol();
                        if(someElse == ' ')
                        {
                            if(neighbors == neighborsCell.length)
                            {
                                Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                                System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                                neighborsCell = biggerArray;
                            }
                            neighborsCell[neighbors] = maze[column+1][row];
                        }
                    }
                    if(row - 1 < 0);
                    else
                    {
                        char someElse = maze[column][row-1].symbol();
                        if(someElse == ' ')
                        {
                            if(neighbors == neighborsCell.length)
                            {
                                Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                                System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                                neighborsCell = biggerArray;
                            }
                            neighborsCell[neighbors] = maze[column][row-1];
                        }
                    }
                    if(row + 1 > 10);
                    else
                    {
                        char someElse = maze[column][row+1].symbol();
                        if(someElse == ' ')
                        {
                            if(neighbors == neighborsCell.length)
                            {
                                Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                                System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                                neighborsCell = biggerArray;
                            }
                            neighborsCell[neighbors] = maze[column][row+1];
                        }
                    }

        return neighborsCell;
    }

toString

覆盖默认的toString方法,并返回从文件

读取时的迷宫。
@Override
    public String toString()
    {
        String result = "";
        for(int row = 0; row < HEIGHT; row++)
        {
            for(int column = 0; column < WIDTH; column++) {
                            switch(maze[column][row].nuValue())
                            {
                            case HEDGE: result += HEDGE_REP; break;
                            case SPACE: result += SPACE_REP; break;
                            case ENTRANCE: result += ENTRANCE_REP; break;
                            case EXIT: result += EXIT_REP; break;
                            }
                        }//switch
                result += NLS;

        }//for
        return result;
    }//toString

findCellWithValue

循环整个迷宫,寻找具有指定int值的单元格作为参数

    public Cell[] findCellWithValue(int theValue)
{
    int currentNoOfCells = 0;
    Cell[] theCells = new Cell[INITIAL_ARRAY_SIZE];
        for(int row = 0; row < HEIGHT; row++)
        {
            for(int column = 0; column < WIDTH; column++)
            {
                if(maze[column][row].value() == theValue)
                {
                    if(currentNoOfCells == theCells.length)
                    {
                        Cell[] biggerArray = new Cell[theCells.length + ARRAY_RESIZE_FACTOR];
                                            System.arraycopy(theCells, 0, biggerArray, 0, theCells.length);
                        theCells = biggerArray;
                    }
                    theCells[currentNoOfCells] = maze[column][row];
                    currentNoOfCells++;
                }
            }
        }
    return theCells;
}

setTheValue

将单元格的值设置为指定的int,作为参数

public void setTheValue(int value, Cell someCell)
{
    int column = someCell.column();
    int row = someCell.row();
    char symbol = someCell.symbol();
    maze[column][row] = new Cell(column, row, symbol, value);
}

我的解决方法中的try和catch语句纯粹是为了找出错误发生的原因和位置。当我得到NullPointerException时它会打印出moveCount为6

文件中的迷宫是一个10 × 20的矩形,周围用"#"表示的树篱。留白用"表示,出口用"!"表示,入口用"?"表示。"

也有人可能已经注意到,我的解决方法开始时找到250的单元格值。这是迷宫构造函数中给出的入口值,看起来像这样:

构造函数

        public FinalSolve(Scanner input)
    {
        for(int row = 0; row < HEIGHT; row++)
        {
            String mazeLine = input.nextLine();
            for(int column = 0; column < WIDTH; column++)
            {
                char character = mazeLine.charAt(column);
                switch(character)
                {
                case SPACE_REP:
                if(column == 7 && row == 7) 
                                    {
                maze[column][row] = new Cell(column, row, SPACE_REP, 20);
                                    }else{
                maze[column][row] = new Cell(column, row, SPACE_REP, 0);
                                    }
                break;

                case HEDGE_REP: maze[column][row] = new Cell(column, row, HEDGE_REP, 0);break;
                case ENTRANCE_REP: maze[column][row] = new Cell(column, row, ENTRANCE_REP, 250);break;
                case EXIT_REP: maze[column][row] = new Cell(column, row, EXIT_REP, 0);break;
                }//switch
            }//for
            }//for
        solve();            
        }//constructor

的错误

方法中的第51行。这条线:

 if(justOne.symbol() == '!')

您提到的相关代码导致了NullPointerExpection .

Cell[] theCells = (moveCount);
for(Cell justOne : theCells)
{
  if(justOne.symbol() == '!')
  ...

这意味着由于某种原因justOnenulljustOne包含数组theCells中的一个值。theCells来自findCellWithValue()。这个数组被初始化为一些默认大小,并且只在找到匹配项时赋值。如果没有找到匹配项,则数组中的每个元素都包含null

这意味着你需要做两件事中的一件。1)在处理它之前检查数组中的值是否为null(并打破循环,因为您正在增量添加值)。2)不传递空值

我个人认为2是更好的选择,因为它不会对方法的用户提出额外的要求。写入findCellWithValue(),以便它根据需要增长数组。这正是ArrayList的设计目的。如果您使用它,您就不会遇到这种错误,并且它将简化您的代码。如果您的集合将会增长,或者不能保证总是具有相同的大小,那么使用List通常是一个更好的主意。

编辑:
正如其他人指出的那样,你的问题相当大。我解释了我是如何追踪问题的,以说明找到漏洞所需要的东西是多么少。当您提出问题时,请尽量包含重现问题所需的最少数量的代码。如果有异常,总是在堆栈跟踪中包含错误消息。很明显,像markPath()toString()这样的东西与问题无关(除非它们在堆栈跟踪中被提到,那么只有可能)。

最新更新