Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 迷宫不能正常生成。越界异常_Java_Canvas_Grid_Runtime Error_Maze - Fatal编程技术网

Java 迷宫不能正常生成。越界异常

Java 迷宫不能正常生成。越界异常,java,canvas,grid,runtime-error,maze,Java,Canvas,Grid,Runtime Error,Maze,我的迷宫发生器似乎有问题。 我正试图产生 我的程序显示以下内容: 错误呢 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at Grid.genRand(Grid.java:73) at Grid.main(Grid.java:35) 如何修复生成器程序 import java.awt.*; import java.awt.Color; import java.aw

我的迷宫发生器似乎有问题。 我正试图产生 我的程序显示以下内容:

错误呢

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at Grid.genRand(Grid.java:73)
        at Grid.main(Grid.java:35)
如何修复生成器程序

import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.*;
import java.util.ArrayList;

public class Grid extends Canvas {

    Cell[][] maze;
    int size;
    int pathSize;
    double width, height;
    ArrayList<int[]> coordinates = new ArrayList<int[]>();

    public Grid(int size, int h, int w) {
        this.size = size;
        maze = new Cell[size][size];
        for(int i = 0; i<size; i++){
            for(int a =0; a<size; a++){
            maze[i][a] = new Cell();
            }
        }
        setPreferredSize(new Dimension(h, w));
    }

    public static void main(String[] args) {
        JFrame y = new JFrame();
        y.setLayout(new BorderLayout());
        Grid f = new Grid(25, 400, 400);
        y.add(f, BorderLayout.CENTER);
        y.setSize(450, 450);
        y.setVisible(true);
        y.setDefaultCloseOperation(y.EXIT_ON_CLOSE);
        f.genRand();
        f.repaint();
    }

    public void push(int[] xy)
  {
    coordinates.add(xy);
    int i = coordinates.size();
    coordinates.ensureCapacity(i++);
  }

  public int[] pop() {
    int[] x = coordinates.get((coordinates.size())-1);
    coordinates.remove((coordinates.size())-1);
    return x;
    }

    public int[] top() {
    return coordinates.get((coordinates.size())-1);
    }

    public void genRand(){
    // create a CellStack (LIFO) to hold a list of cell locations [x]
    // set TotalCells = number of cells in grid  
    int TotalCells = size*size;
    // choose a cell at random and call it CurrentCell 
    int m = randomInt(size);
    int n = randomInt(size);
    Cell curCel = maze[m][n];
    // set VisitedCells = 1  
    int visCel = 1,d=0;
    int[] q;
    int h,o = 0,p = 0;
    // while VisitedCells < TotalCells 
    while( visCel < TotalCells){
    // find all neighbors of CurrentCell with all walls intact
    if(maze[m-1][n].countWalls() == 4){d++;}
    if(maze[m+1][n].countWalls() == 4){d++;}
    if(maze[m][n-1].countWalls() == 4){d++;}
    if(maze[m][n+1].countWalls() == 4){d++;}
    // if one or more found 
    if(d!=0){
    Point[] ls = new Point[4];
    ls[0] = new Point(m-1,n);
    ls[1] = new Point(m+1,n);
    ls[2] = new Point(m,n-1);
    ls[3] = new Point(m,n+1);
    // knock down the wall between it and CurrentCell
    h = randomInt(3);
    switch(h){
        case 0: o = (int)(ls[0].getX());
                p = (int)(ls[0].getY());
                curCel.destroyWall(2);
                maze[o][p].destroyWall(1);
            break;
        case 1: o = (int)(ls[1].getX());
                p = (int)(ls[1].getY());
                curCel.destroyWall(1);
                maze[o][p].destroyWall(2);
            break;
        case 2: o = (int)(ls[2].getX());
                p = (int)(ls[2].getY());
                curCel.destroyWall(3);
                maze[o][p].destroyWall(0);
            break;
        case 3: o = (int)(ls[3].getX());
                p = (int)(ls[3].getY());
                curCel.destroyWall(0);
                maze[o][p].destroyWall(3);
            break;
    }   
    // push CurrentCell location on the CellStack 
    push(new int[] {m,n});
    // make the new cell CurrentCell
    m = o; n = p;
    curCel = maze[m][n];
    // add 1 to VisitedCells
    visCel++;
    }
    // else 
    else{
    // pop the most recent cell entry off the CellStack 
    q = pop();
    m = q[0]; n = q[1];
    curCel = maze[m][n]; 
    // make it CurrentCell
    // endIf
    }
    // endWhile  
    }   
    }

    public int randomInt(int s) { return (int)(s* Math.random());}

    public void paint(Graphics g) {
        int k, j;
        width = getSize().width;
        height = getSize().height;
        double htOfRow = height / (size);
        double wdOfRow = width / (size);
//checks verticals - destroys east border of cell
        for (k = 0; k < size; k++) {
            for (j = 0; j < size; j++) {
                if(maze[k][j].checkWall(2)){
                g.drawLine((int) (k * wdOfRow), (int) (j * htOfRow), (int) (k * wdOfRow), (int) ((j+1) * htOfRow));
            }}
        }
//checks horizontal - destroys north border of cell
        for (k = 0; k < size; k++) {
            for (j = 0; j < size; j++) {
                if(maze[k][j].checkWall(3)){
                g.drawLine((int) (k * wdOfRow), (int) (j * htOfRow), (int) ((k+1) * wdOfRow), (int) (j * htOfRow));
            }}
        }
    }
}

class Cell {

    private final static int NORTH = 0;
    private final static int EAST = 1;
    private final static int WEST = 2;
    private final static int SOUTH = 3;
    private final static int NO = 4;
    private final static int START = 1;
    private final static int END = 2;
    boolean[] wall = new boolean[4];
    boolean[] border = new boolean[4];
    boolean[] backtrack = new boolean[4];
    boolean[] solution = new boolean[4];
    private boolean isVisited = false;
    private int Key = 0;

    public Cell(){
    for(int i=0;i<4;i++){wall[i] = true;}
    }
    public int countWalls(){
    int i, k =0; 
    for(i=0; i<4; i++) {
    if (wall[i] == true)
    {k++;}
    }
    return k;}
    public boolean checkWall(int x){
    switch(x){
        case 0: return wall[0];
        case 1: return wall[1];
        case 2: return wall[2];
        case 3: return wall[3];
    }
    return true;
    }
    public void destroyWall(int x){
    switch(x){
        case 0: wall[0] = false; break;
        case 1: wall[1] = false; break;
        case 2: wall[2] = false; break;
        case 3: wall[3] = false; break;
        }
    }
    public void setStart(int i){Key = i;}   
    public int getKey(){return Key;}
    public boolean checkVisit(){return isVisited;}
    public void visitCell(){isVisited = true;}
}
import java.awt.*;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Graphics;
导入javax.swing.*;
导入java.util.ArrayList;
公共类网格扩展画布{
细胞[]迷宫;
整数大小;
int路径大小;
双宽、双高;
ArrayList坐标=新的ArrayList();
公共网格(整数大小、整数h、整数w){
这个。大小=大小;
迷宫=新单元[大小][大小];
对于(int i=0;i
这就是你的问题。
n
m
可以是0

当您尝试时:

if(maze[m-1][n].countWalls() == 4){d++;}
if(maze[m+1][n].countWalls() == 4){d++;}
if(maze[m][n-1].countWalls() == 4){d++;}
if(maze[m][n+1].countWalls() == 4){d++;}

如果其中任何一个为0,则您将尝试获取
maze[-1][n]
maze[m][1]
,这就是为什么您会获取ArrayIndexOutOfBounds异常。

您没有边界检查对maze的访问。例如,如果m=0,则

maze[m-1][n]
将访问

maze[-1][n]

这超出了界限。

所以如果我检查m和n是否为0,我应该调用randomInt(大小)再次?您必须查看算法的其余部分,以查看是否有任何中断,但是的,它会修复此错误。我只是查看了您的异常和堆栈跟踪,以查看此特定情况下的错误。更好的是,您可以更改大小以同时接受上限和下限,并确保生成的数字在该范围内。您可以我还想考虑当随机数生成<代码> size < /C>时会发生什么。当您尝试访问<代码>迷宫[m+1] [n] < /代码>或<代码>迷宫[m] [n+1] 1 /代码>时,您将再次超出数组的界限。{ m=随机数(size);}(m=0);要修复它吗?它仍然显示异常错误。我也不知道如何使大小同时接受上限和下限。顺便说一句,大小是一个常量int 25。
maze[-1][n]