This course has been updated! We now recommend you take the Complete Intro to Computer Science course.

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours, Part 2 course:
The "Generating a Maze Exercise" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours, Part 2 course featured in this preview video. Here's what you'd learn in this lesson:

Generate a maze.

Get Unlimited Access Now

Transcript from the "Generating a Maze Exercise" Lesson

[00:00:00]
>> Brian Holt: Let's take a look at our exercises. This one does have some visualization to help you out. In fact, let's show you what it looks like when it's completed.
>> Brian Holt: So these are the mazes. Exactly what they should look like by the time you're done. You're gonna be calling randomized directions, but randomized directions actually is not randomizing them for you.

[00:00:24] It's gonna give you back a set same of directions, so that I can tell that you're doing the maze the correct way, right? So you get to assume it's random even though it's not. If you call randomized direction too many times, you're gonna see a big console warning in your console that says, by the way you called this too many times so I'm just gonna give you back something random but you're probably not gonna get this correct so.

[00:00:48] Be aware that that happens. So this is what the 5 x 5 one should look like, the 8 x 8, the 15 x 15 will look something like that.
>> Brian Holt: Okay? So, and let's see.
>> Brian Holt: The maze will be an array of arrays of objects, right? We're familiar with arrays of arrays.

[00:01:15] 0 x 0 is here. And then, each one of these objects has a visited, whether it's been visited or not, right? So you're gonna set that to true as soon as it's been visited. And then you're gonna see here that it has walls.
>> Brian Holt: By default, everything given to you in the first go is going to have a wall, right?

[00:01:35] All of these have walls between them. As you get through more and more of them, you'll be setting these to false when there's no wall between them. Remember that for all these operations. So like if I'm going from 0 0 to 1 0 I have to set 0 0's north wall to false and I also have to set 1 0 south wall to false so there are two walls you have to tear down, okay?

[00:02:09] The outer array represents the Y axis, the inner array represents the X axis. So your accessors look like that, [y][x], similar to before. The starting coordinate will be a pair. It's gonna be 5, 4, something like that. You don't have to do any boundary checkings on this. I guarantee you that I'm gonna give you a well formatted graph.

[00:02:31] And I'm going to give you valid starting points, right. So I'm not interested in the educations here, I'm just interested in you getting the algorithms down.
>> Male 2: Does direction include directions that are not on the graph?
>> Brian Holt: That one will, yeah. So those ones you definitely do have to do boundary checking.

[00:02:54] This randomized direction function is available globally.
>> Brian Holt: The other thing is, you can work on the maze directly itself. Don't worry about being destructive on your parameters. That's never a bad idea, I'm just kidding, that's usually always a bad idea but that's fine in this particular case. It's gonna be returning, so randomizedDirections will be returning north, east, south, west, in some order.

[00:03:19] And then you just follow those in that order.
>> Brian Holt: If you're messing around with this later, if you're a nerd like me, if you do setOrder(null) this will actually make it truly random if you want to like generate your own fun graphs, which I have never done. I'm not that nerdy, just kidding.

[00:03:42] I totally did.
>> Brian Holt: Yeah and you'll see a console error if you call randomize directions too much.
>> Brian Holt: So something that you'll get from this helper which I found helpful. If the square's red, it means that it's not visited. Everything should be visited by the end. And then you'll also be able to see.

[00:04:06] You can see the north wall here, and you can see the south wall here, right? So if you can, you'll be able to tell if one of them hasn't been visited, or not. If you'd like to see all the utility functions, they're all written out here in this other code pen.

[00:04:21]
>> Brian Holt: Highly, highly suggest here that you work on one unit test at a time, and try not to solve all of them at once, because there's a decent chance you're gonna hit infinite recursion. If you do hit infinite recursion and you save your CodePen that can be a bit of a disaster, because CodePen will run things as soon as you open the tab again.

[00:04:39] So there is a way, if you just Google CodePen disable JavaScript, it will show you how to make it not run it so you can fix it, save it, and then reload the page. So just Google it.
>> Brian Holt: Cool, so you can see here this is what it's gonna expect to look for.

[00:05:02] This is quite long because 15 x 15 is a lot of cells.
>> Brian Holt: Cool. So, I'm gonna give you a bit of a break to do this.