首页 文章

空错误Java迷宫求解器[关闭]

提问于
浏览
-1

我有2个物体 . 一个叫Cell,一个叫FinalSolve . 该程序的目的是通过循环遍历每个单元寻找出口来解决迷宫 .

单元格对象表示迷宫中的单元格,并具有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个实例方法,如下所示:

  • 解决

  • Mark Path

  • 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());
            }


}

Mark Path

一旦求解方法解决了程序并且它标记了从出口到入口的路径,就会调用此方法 . 由于我在程序中遇到错误,此方法仍在进行中,只打印“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);
}

我的solve方法中的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() == '!')

1 回答

  • 0

    基于您提到的行的相关代码导致 NullPointerExpection .

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

    这意味着由于某种原因 justOnenull . justOne 包含数组 theCells 中的一个值 . theCells 来自 findCellWithValue() 的目录 . 此数组初始化为具有某个默认大小,仅在找到匹配时分配 . 如果未找到匹配项,则数组中的每个元素都将包含 null .

    这意味着你需要做两件事之一 . 1)在处理数组之前检查数组中的值是否为 null (并且因为逐步添加值而中断循环) . 2)不传回空值 .

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

    编辑:
    正如其他人指出的那样,你的问题非常大 . 我解释了如何跟踪问题,以显示为了找到错误所需的少量内容 . 当您提出问题时,请尝试包含重现问题所需的最少量代码 . 如果存在异常,请始终包含错误消息 AND 堆栈跟踪 . 应该清楚 markPath()toString() 之类的问题与问题无关(除非它们在堆栈跟踪中提到,然后才可能) .

相关问题