簡體   English   中英

越界異常-2D陣列迷宮

[英]Out of bounds exception - 2D array maze

我是這里的新手,還是編碼的新手! 一如既往的學習令人興奮,但乏味。 我正在編寫一個程序,其中字符需要解決迷宮問題。 我在這里還沒有完成,但是由於某種原因,我在長度大於3的任何數組上都超出了范圍。 我不知道為什么。 有測試用例,您可以成功看到小的迷宮,但是當我嘗試更大的迷宮時,出現了異常。

有什么建議嗎?

import java.awt.Point;


public class Maze 
{
    final char WALL = 'x';
    final char BOT = 'r';
    final char FREE = ' ';

    private String[] maze;
    private Point position;
    private boolean[][] mazeWalls;

    public Maze(String[] strings) // constructor to create maze from main as a 2D array
    {
        maze = strings;
        mazeWalls = new boolean[strings[0].length()][strings.length];
            for (int i = 0; i < mazeWalls.length; i++)
                for (int j = 0; j < mazeWalls[0].length; j++)
                {
                    if (strings[i].charAt(j) == WALL) // this is where it shows the out of bounds error on m[3] or m[4]
                        mazeWalls[j][i] = true;

                    else if (strings[i].charAt(j) == BOT)
                    {
                        position = new Point(i, j);
                    }
                }
    }

    public static void main (String[] args)
    {
        Maze m;
        /*m = new Maze(new String[] {
                "xxx",
                "xrx",
                "xxx"});
        m = new Maze(new String[] {
                "xxx",
                "xrx",
                "x x"});
        m = new Maze(new String[] {
                "xxx",
                "xr      ",
                "xxx",});*/
        m = new Maze(new String[] {
                "xxxxx",
                "xr  x",
                "x   x",
                "x    ",
                "x   x",
                "x   x",
                "xxxxx"});
        /*m = new Maze(new String[] {
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "xr  r                 x                     x",
        "x                    x                     x",
        "x   x                x                      ",
        "x   x                x                     x",
        "x   x                x                     x",
        "x   x                x                     x",
        "x   xxxxxxxxxxxxxxxxxxxxxxxxxx             x",
        "x   x                                      x",
        "x   x                                      x",
        "x   x                                      x",
        "x   x                                      x",
        "x   x                                      x",
        "x   x                                      x",
        "x   x                                      x",
        "x   xxxxxxxxxxxxxx  xxxxxxxxxxxxxxxxxxxxxxxx",
        "x   x                                      x",
        "x   x                                      x",
        "x   x                                      x",
        "x                                          x",
        "x                                          x",
        "x                                          x",
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"});*/
                Robot r = new RandomBot();
            //  Robot r = new RightHandBot();

                int c = 0;
                System.out.println(m);
                while (!m.isRobotOnEdge())
                {
                    c++;
                    Point move = r.getMove(m);
                    m.moveTo(move);
                    System.out.println("Step Count: " + c);
                    System.out.println(m);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {}
                }   
    }

    public void moveTo(Point p) 
    {
        /*for (int i = 0; i < mazeWalls.length; i++)
            for (int j = 0; j < mazeWalls[0].length; j++)
            {
                if (maze[i].charAt(j) == BOT)
                    position = p;
            }*/
    }

    public Point getPoint()
    {
        return (Point)position.clone();
    }

    public boolean wallObject(Point p)
    {
        return wallHere(position.x, position.y);
    }

    public boolean wallHere(int x, int y)
    {
        return x < 0 || x >= mazeWalls.length || y < 0 || y >= mazeWalls[0].length || mazeWalls[x][y];
    }

    public boolean isRobotOnEdge() 
    {
                if (position.x == mazeWalls.length - 1 || position.y == mazeWalls[0].length - 1 || position.x == 0 || position.y == 0)
                    return true;
        return false;
    }

    public String toString()
    {
        String s = "";
            for (int j = 0; j < mazeWalls.length; j++)
            {
                for (int i = 0; i < mazeWalls[0].length; i++)
                {
                    if (i == position.x && j == position.y)
                        s += BOT;
                    else if (mazeWalls[i][j] == true)
                        s += WALL;
                    else
                        s += FREE;
                }
             s += "\n";
            }

        return s;
    }
}

您正在將一維String數組轉換為二boolean數組,但是目標數組的大小是錯誤的。 行和列的數量不同,因此2D數組的大小如何很重要。 看這個:

                            ROWS                COLUMNS
mazeWalls = new boolean[strings[0].length()][strings.length];

它應該是

mazeWalls = new boolean[strings.length][strings[0].length()];

另外,除非您嘗試轉置值,否則此行

mazeWalls[j][i] = true;

應該

mazeWalls[i][j] = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM