- Home /
Array Index Out of Range - Maze Generation Algorithm
I'm trying to write a maze generation algorithm, I'm basically copying it from one I found online and translating it into C#. It compiles perfectly fine, but when I run the program in Unity, the error log pretty much explodes with errors saying:
mazeGenerateScript.generateMaze () (at Assets/Scripts/mazeGenerateScript.cs:19) mazeGenerateScript.Update () (at Assets/Scripts/mazeGenerateScript.cs:109) I can't figure out why it does this. Here's my code, the relevant line is the "maze[i,j] = 1;" in the beginning of generateMaze(); using UnityEngine; using System.Collections;IndexOutOfRangeException: Array index is out of range.
public class mazeGenerateScript : MonoBehaviour {
public static int height = 15; public static int width = 20; public Transform wallSegment; public int[,] maze = new int[,] { {height}, {width} }; // Use this for initialization void Start () {
}
public int[,] generateMaze() { // Initialize for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) maze[i,j] = 1;
// r for row, c for column // Generate random r int r = Random.Range(0,height); while (r % 2 == 0) { r = Random.Range(0,height); } // Generate random c int c = Random.Range(0,width); while (c % 2 == 0) { c = Random.Range(0,width); } // Starting cell maze[r,c] = 0;
// Allocate the maze with recursive method recursion(r, c);
return maze; }
public void recursion(int r, int c) { // 4 random directions int[] randDirs = generateRandomDirections(); // Examine each direction for (int i = 0; i < randDirs.Length; i++) { switch(randDirs[i]) { case 1: // Up // Whether 2 cells up is out or not if (r - 2 <= 0) continue; if (maze[r - 2,c] != 0) { maze[r - 2,c] = 0; maze[r - 1,c] = 0; recursion (r - 2, c); } break; case 2: // Right // Whether 2 cells to the right is out or not if (c + 2 >= width - 1) continue; if (maze[r,c + 2] != 0) { maze[r,c + 2] = 0; maze[r,c + 1] = 0; recursion(r, c + 2); } break; case 3: // Down // Whether 2 cells down is out or not if (r + 2 >= height - 1) continue; if (maze[r + 2,c] != 0) { maze[r+2,c] = 0; maze[r+1,c] = 0; recursion(r + 2, c); } break; case 4: // Left // Whether 2 cells to the left is out or not if (c - 2 <= 0) continue; if (maze[r,c - 2] != 0) { maze[r,c - 2] = 0; maze[r,c - 1] = 0; recursion(r, c - 2); } break; }
}
}
public int[] generateRandomDirections() { int[] randoms = new int[4];
for (int t = 0; t < randoms.Length; t++ ) { int tmp = randoms[t]; int r = Random.Range(t, randoms.Length); randoms[t] = randoms[r]; randoms[r] = tmp; } return randoms; }
// Update is called once per frame void Update () { int xCoordinate = 0; int yCoordinate = 0;
int[,] masterMaze = new int[,] { {height}, {width} }; masterMaze = generateMaze();
for (int a = 0; a < masterMaze.GetLength(0); a++) { for (int b = 0; b < masterMaze.GetLength(1); b++) { if (masterMaze[a,b] == 1) { Instantiate(wallSegment, new Vector3(xCoordinate, yCoordinate, 8), Quaternion.identity); xCoordinate = xCoordinate + 16; } else xCoordinate = xCoordinate + 16; } yCoordinate = yCoordinate + 16; }
} } I tried reducing the number of iterations in that initialization loop, but that didn't seem to work, and I can't think of anything else that would cause this error, does anyone have any idea?
Answer by Dave-Carlile · Dec 22, 2012 at 01:02 PM
You're not initializing the maze array correctly.
public int[,] maze = new int[,] { {height}, {width} };
That allocates enough room for two items and initializes the values to height and width. You want to create the array with enough froom to store height width* values.
public int[,] maze = new int[height, width];
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Random Material 0 Answers
NullReferenceException - Maze Generator Script 1 Answer
How to deactivate all GameObject in a array, except last one 4 Answers