Search⌘ K
AI Features

Solution: Linking Worlds

Understand how to create and link multiple subgrids to build complex 3D maze structures such as spheres and cubes. This lesson guides you through initializing the MultiSphereGrid, connecting random cells, yielding cells, and applying braid operations to enhance maze complexity and traversal.

We'll cover the following...

Solution

Let's execute the following code solution and see how it works:

C++
require 'sphere_grid'
require 'prims'
# reopen and add a new possible neighbor
class HemisphereCell
attr_accessor :alien_grid, :alien
def neighbors
super.tap do |list|
list << alien if alien
end
end
end
class MultiSphereGrid
attr_reader :subgrids
def initialize(subgrids, connections)
@subgrids = subgrids
@subgrids.each do |subgrid|
others = @subgrids - [ subgrid ]
connections.times do
other = others.sample
local, alien = subgrid.random_cell, other.random_cell
# skip cells that are already linked to another subgrid
next if local.alien || alien.alien
local.alien_grid = other
local.alien = alien
alien.alien_grid = subgrid
alien.alien = local
end
end
end
def random_cell
@subgrids.sample.random_cell
end
def each_cell
@subgrids.each do |subgrid|
subgrid.each_cell { |cell| yield cell }
end
end
def braid(p=1.0)
@subgrids.each { |subgrid| subgrid.braid(p) }
end
end
world = SphereGrid.new(20)
moon1 = SphereGrid.new(10)
moon2 = SphereGrid.new(8)
moon3 = SphereGrid.new(6)
grid = MultiSphereGrid.new([ world, moon1, moon2, moon3 ], 3)
TruePrims.on(grid)
grid.braid(0.5)

Code explanation

Lines 18–37: We initialize ...