简体   繁体   中英

Out of bounds exception - 2D array maze

I am new here and new to coding in general! It's exciting to learn but tedious as ever. I am writing a program where a char needs to solve a maze. I am no where near finished here, but for some reason I am getting an out of bounds exception on any array greater than 3 in length. I have no idea why. There are test cases and you can see the small mazes print successfully, however, when I try a bigger maze I get an out of bounds exception.

Any advice?

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;
    }
}

You are translating a 1D String array to a 2D boolean array but the size of the target array is the wrong way round. The number of rows & columns are different so it matters how the 2D array is sized. Look at this:

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

it should be

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

Also unless, you are attempting to transpose the values, this line

mazeWalls[j][i] = true;

should be

mazeWalls[i][j] = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM