Exercise: Linking Worlds

Learn to generate a maze in an environment with multiple spheres.

We'll cover the following

We can generate mazes on a sphere now, which is great. But what if we had an environment with multiple spheres instead of a single sphere? What if this were a game, and we had to navigate from a world to its moons?

Problem statement

Link multiple sphere grids into a single multisphere grid. Randomly connect cells on each sphere to cells on other spheres and then generate a maze on them.

Rendering such a configuration poses its own challenge, though.

For this challenge, don't worry about drawing the resulting mazes (you're encouraged to try). Just build the grids and generate the mazes.

Coding challenge

You'll need to either modify your HemisphereCell class, in order to add a new alien neighbor accessor, or you'll need to subclass it and then modify your HemisphereGrid class. It's up to you.

Then, you'll need to create a MultiSphereGrid class that accepts an array of other grids. It may also accept a connections parameter indicating how many connections to make on each world, though you may feel free to hardcode this value.

Then, on each subgrid, it should randomly choose another subgrid. It should pick a random cell in the current subgrid and a random cell in the other subgrid and set the alien accessor for each to the other cell. Not every maze algorithm will run readily on this grid; try the Recursive Backtracker or one of the Prim's algorithms.

Finally, consider braiding the grid; if these are really worlds and moons, there will naturally be multiple ways to get from one point to another!

Write your solution code here:

Get hands-on with 1200+ tech skills courses.