Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:01:48+00:00 2026-06-01T12:01:48+00:00

My maze generator seems to have a problem. I am trying to generate something

  • 0

My maze generator seems to have a problem.
I am trying to generate something like this maze
My program displays this:

enter image description here

and the error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at Grid.genRand(Grid.java:73)
        at Grid.main(Grid.java:35)

How do I fix my generator program?

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;}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:01:49+00:00Added an answer on June 1, 2026 at 12:01 pm
    int m = randomInt(size);
    int n = randomInt(size);
    

    That’s your problem. n and m can be 0.

    When you try:

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

    you’d be trying to get maze[-1][n] or maze[m][-1] if either one of these is 0, which is why you’d get the ArrayIndexOutOfBounds exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm creating something like a randomly generated maze in flash. For this, I
I'm trying to use SharpDX to create a simple maze-like 2D program using DirectX.
I have a program that is searching a maze to find the best way
I'm trying to create a random maze generator in javascript. There may already be
I have little program creating a maze. It uses lots of collections (the default
I have a maze game and I'm trying to create two Timers at a
I've gotten my maze solver program to work but it seems to be including
I'm trying to write a simple maze search program in prolog, before I add
I am trying to implement a breadth first traversal for a maze. This is
I'm trying to create a randomized maze in C++, but I can't start because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.