I had so much fun with this problem that I think I’ve worked out a complete solution:
[code]Shore is a room.
Oasis is a room.
The player carries a first marker, a second marker, and a third marker.
A maze is a kind of room.
A maze-region is a kind of region. A maze-region has a text called the room description. A maze-region has room called the entrance. A maze-region has a room called the exit. A maze-region has a direction called the way in. A maze-region has a number called the connectivity. The connectivity of a maze-region is usually 3.
The Desert is a maze-region. There are ten mazes in the desert. The entrance of the desert is Shore. The exit of the desert is Oasis. The way in of the desert is west. The room description of the desert is “You are in a desert. Dunes allow passage to [the list of viable directions].”
When play begins:
Repeat with area running through maze-regions:
Repeat with place running through mazes in area:
Now the description of place is the room description of area;
Now the printed name of place is the printed name of area;
Definition: A direction is horizontal if it is not up and it is not down and it is not inside and it is not outside.
Definition: a room (called place) is connected rather than unconnected:
Repeat with way running through directions:
If the room way from place is a room, yes;
no.
Connection availability relates a direction (called way) to a room (called origin) when the room way from origin is nothing. The verb to be unused from implies the connection availability relation.
Definition: a direction (called way) is viable if it is not unused from the location.
Maze entry relates a room (called origin) to a maze-region (called area) when origin is the entrance of area. The verb to be the starting point of implies the maze entry relation
Before going nowhere from a room that is the starting point of a maze-region (called area):
if the noun is not the way in of area, continue the action;
Let last-step be a random unconnected maze in area;
Link last-step symmetrically to the exit of area via the way in of area;
Let origin be the entrance of area;
Let way be the way in of area;
While there is an unconnected maze (called destination) in area:
Link origin symmetrically to destination via way;
Now way is a random connection from origin to destination;
Now origin is destination;
Link origin symmetrically to last-step via a random connection from origin to last-step;
[This makes a single path from the entrance to the exit. It may be twisty, but it’s not really a maze.
But you can use a the same phrases to add additional random connections, e.g.:]
Repeat with N running from 1 to the connectivity of area:
Now origin is a random maze in area;
Now destination is a random maze in area;
Let way be a random connection from origin to destination;
If way is a direction, link origin symmetrically to destination via way.
To decide which object is a random connection from (origin - a room) to (destination - a room):
Let choices be 0;
Repeat with way running through horizontal directions that are unused from origin:
If the opposite of way is unused from destination, increment choices;
Let N be a random number from 1 to choices;
Repeat with way running through horizontal directions that are unused from origin:
if the opposite of way is unused from destination, decrement N;
if N is 0, decide on way;
[We should never get here during the creation of the “correct path.”]
decide on nothing;
To link (origin - a room) symmetrically to (destination - a room) via (way - a direction):
link origin one way to destination via way;
link destination one way to origin via the opposite of way;
To link (origin - a room) one way to (destination - a room) via (way - a direction):
If way is:
– north: now destination is mapped north of origin;
– northwest: now destination is mapped northwest of origin;
– west: now destination is mapped west of origin;
– southwest: now destination is mapped southwest of origin;
– south: now destination is mapped south of origin;
– southeast: now destination is mapped southeast of origin;
– east: now destination is mapped east of origin;
– northeast: now destination is mapped northeast of origin;
– down: now destination is mapped below origin;
– up: now destination is mapped above origin;
– inside: now destination is mapped inside origin;
– outside: now destination is mapped outside origin;
test me with “w”
[/code]
You can see there are a couple of ugly hacks:
-
It seems that it’s not possible to use a variable in the “now X is mapped Y of Z” phrase. [rant]In addition, I couldn’t come up with any legal way to say “Now X is mapped inside/outside of Z.” edit: Thanks for the answer, Felix.[/rant]
-
I couldn’t come up with a legal phrase meaning “a random horizontal direction that is unused from A and is the opposite of a horizontal direction that is unused from B.” Which is why I wrote the crazy loop over N instead.
My knowledge of graph theory is sketchy, but I think my algorithm is sound and covers all cases. Of course I spent so long on this that Zarf came up with a much simpler solution. Using real geometry seems like a smart way to solve the problem.
One question: does the random number seed get saved as well when you save the game? If so, then last-minute calculations may be pointless.