<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The Osmosian Order of Plain English Programmers Welcomes You]]></provider_name><provider_url><![CDATA[http://osmosianplainenglishprogramming.blog]]></provider_url><author_name><![CDATA[gerryrzeppa]]></author_name><author_url><![CDATA[https://osmosianplainenglishprogramming.blog/author/gerryrzeppa/]]></author_url><title><![CDATA[The Amazing MultiMaze]]></title><type><![CDATA[link]]></type><html><![CDATA[<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div class="entry-content">
<p>A MultiMaze is a maze with multiple, player-selected beginnings and endings. Here is a sample:</p>
<p><img loading="lazy" class="alignnone size-large wp-image-320359" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/multimaze-1-1024x819.png" alt="" width="665" height="532" /></p>
<p>The object is to connect each green dot with one, and only one, red dot (with or without overlapping paths).</p>
<p>The Plain English program that generates MultiMazes is described below. We use the Aldous-Broder algorithm because it always generates a <em>perfect </em>maze, ie, a maze with without loops or closed circuits, and without any inaccessible areas. It is also a <em>uniform </em>maze generator, which means it generates all possible mazes of a given size with equal probability. And it’s a very simple algorithm that requires no extra storage or stack to implement. It’s not super fast, but it is <em>reasonably </em>fast, typically generating a full-page maze in less than 3 seconds.</p>
<p>The data structures are almost trivial. We define a maze in our program like this:</p>
<p><span style="color:#00ccff;">A maze is a thing with a width, a height, a cell size and some cells.</span></p>
<p>And a cell is defined this way:</p>
<p><span style="color:#00ccff;">A cell is a thing with a box,</span><br />
<span style="color:#00ccff;"> a left flag, a top flag, a right flag, a bottom flag,</span><br />
<span style="color:#00ccff;"> a visited flag, a start flag and an end flag.</span></p>
<p><span style="color:#00ccff;">A neighbor is a cell.</span></p>
<p>Each cell’s box describes its position in the maze’s “array”. The left, top, right, and bottom flags indicate whether or not the corresponding side of the cell is open or closed; the visited flag is required by the Aldous-Broder algorithm, and the start and end flags indicate which cells will be marked with a green or red dot. (By the way, that first definition fit on one line on the screen when I wrote it because we use a tight, proportionally-spaced font and our uncluttered editor always spans the entire width of the screen. Turns out the things that make Plain English sentences easy-to-read — like one thought per line — are not the same things that make artificial, mathematically-influenced programming languages easy-to-read.)</p>
<p>The main routine in our program reads like this:</p>
<p><span style="color:#00ccff;">To run:</span><br />
<span style="color:#00ccff;"> Start up.</span><br />
<span style="color:#00ccff;"> Clear the screen with the tan color.</span><br />
<span style="color:#00ccff;"> Create a maze 10 inches by 8 inches using 1/4 inch for the cells.</span><br />
<span style="color:#00ccff;"> Display the maze with title and instructions.</span><br />
<span style="color:#00ccff;"> Destroy the maze.</span><br />
<span style="color:#00ccff;"> Wait for the escape key.</span><br />
<span style="color:#00ccff;"> Shut down.</span></p>
<p>Most of that is self-explanatory, so I’ll jump to the interesting stuff. This is the top-level create routine:</p>
<p><span style="color:#00ccff;">To create a maze a width by a height using a size for the cells:</span><br />
<span style="color:#00ccff;"> Allocate memory for the maze.</span><br />
<span style="color:#00ccff;"> Put the width into the maze&#8217;s width.</span><br />
<span style="color:#00ccff;"> Put the height into the maze&#8217;s height.</span><br />
<span style="color:#00ccff;"> Put the size into the maze&#8217;s cell size.</span><br />
<span style="color:#00ccff;"> Create some cells for the maze.</span><br />
<span style="color:#00ccff;"> Apply the Aldous-Broder algorithm to the maze.</span><br />
<span style="color:#00ccff;"> Select starting and ending cells in the maze.</span></p>
<p>The latter three lines do most of the work. The first calls this routine to create the maze’s cells:</p>
<p><span style="color:#00ccff;">To create some cells for a maze:</span><br />
<span style="color:#00ccff;"> If a spot&#8217;s left is greater than the maze&#8217;s width, break.</span><br />
<span style="color:#00ccff;"> If the spot&#8217;s top is greater than the maze&#8217;s height,</span><br />
<span style="color:#00ccff;"> add the maze&#8217;s cell size to the spot&#8217;s left; repeat.</span><br />
<span style="color:#00ccff;"> Create a cell of the maze&#8217;s cell size at the spot.</span><br />
<span style="color:#00ccff;"> Append the cell to the maze&#8217;s cells.</span><br />
<span style="color:#00ccff;"> Add the maze&#8217;s cell size to the spot&#8217;s left.</span><br />
<span style="color:#00ccff;"> If the spot&#8217;s left is less than the maze&#8217;s width, repeat.</span><br />
<span style="color:#00ccff;"> Put 0 into the spot&#8217;s left.</span><br />
<span style="color:#00ccff;"> Add the maze&#8217;s cell size to the spot&#8217;s top.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>Note that the cells are stored in a doubly-linked list, not in an array (as you might expect), because Plain English does not have a native array type (arrays are not native to English grammar).</p>
<p>This is the routine that creates an individual cell:</p>
<p><span style="color:#00ccff;">To create a cell of a size at a spot:</span><br />
<span style="color:#00ccff;"> Allocate memory for the cell.</span><br />
<span style="color:#00ccff;"> Put the spot and the spot plus the size into the cell&#8217;s box.</span><br />
<span style="color:#00ccff;"> Set the cell&#8217;s left flag.</span><br />
<span style="color:#00ccff;"> Set the cell&#8217;s top flag.</span><br />
<span style="color:#00ccff;"> Set the cell&#8217;s right flag.</span><br />
<span style="color:#00ccff;"> Set the cell&#8217;s bottom flag.</span><br />
<span style="color:#00ccff;"> Clear the cell&#8217;s visited flag.</span></p>
<p>Once we’ve got our cells, we connect them in the right spots using the Aldous-Broder algorithm I mentioned earlier. This is the Plain English implementation:</p>
<p><span style="color:#00ccff;">To apply the Aldous-Broder algorithm to a maze:</span><br />
<span style="color:#00ccff;"> Pick a cell in the maze. Set the cell&#8217;s visited flag.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> If all of the maze&#8217;s cells have been visited, break.</span><br />
<span style="color:#00ccff;"> Pick a neighbor of the cell in the maze.</span><br />
<span style="color:#00ccff;"> If the neighbor&#8217;s visited flag is not set,<br />
connect the cell with the neighbor.</span><br />
<span style="color:#00ccff;"> Set the neighbor&#8217;s visited flag.<br />
</span><span style="color:#00ccff;">Put the neighbor into the cell.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>I won’t explain what that does because I’d simply be repeating the “code” in this paragraph. Instead, I’ll show you the support routines that make that work. Like this one:</p>
<p><span style="color:#00ccff;">To pick a neighbor of a cell in a maze:</span><br />
<span style="color:#00ccff;"> Void the neighbor.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Pick a number between 1 and 4.</span><br />
<span style="color:#00ccff;"> If the number is 1,<br />
find the neighbor above the cell in the maze.</span><br />
<span style="color:#00ccff;"> If the number is 2,<br />
find the neighbor to the right of the cell in the maze.</span><br />
<span style="color:#00ccff;"> If the number is 3,<br />
find the neighbor below the cell in the maze.</span><br />
<span style="color:#00ccff;"> If the number is 4,<br />
find the neighbor to the left of the cell in the maze.</span><br />
<span style="color:#00ccff;"> If the neighbor is nil, repeat.</span></p>
<p>A neighbor might be above, below, to the right, or to the left of the current cell. We want to pick one of those, if it exists, randomly. So we pick a number between 1 and 4 (corresponding to above, right, below, and left) and see what we can find there. If we fail to find a neighbor in that relative location, we try again and again until we succeed. After all, every cell has at least 2 neighbors.</p>
<p>The routine below is typical of the four “find” routines called by the above routine:</p>
<p><span style="color:#00ccff;">To find a neighbor above a cell in a maze:</span><br />
<span style="color:#00ccff;"> Void the neighbor.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Get the neighbor from the maze&#8217;s cells.</span><br />
<span style="color:#00ccff;"> If the neighbor is the cell, repeat.</span><br />
<span style="color:#00ccff;"> If the neighbor is nil, break.</span><br />
<span style="color:#00ccff;"> If the neighbor&#8217;s bottom line is the cell&#8217;s top line, break.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>Now this is how we connect one cell with another. Cells start with all of their wall flags set, so we simply have to remove the appropriate walls (in both cells) to make a path:</p>
<p><span style="color:#00ccff;">To connect a cell with a neighbor:</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s top line is the neighbor&#8217;s bottom line,</span><br />
<span style="color:#00ccff;"> clear the cell&#8217;s top flag;<br />
clear the neighbor&#8217;s bottom flag; exit.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s right line is the neighbor&#8217;s left line,</span><br />
<span style="color:#00ccff;"> clear the cell&#8217;s right flag;<br />
clear the neighbor&#8217;s left flag; exit.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s bottom line is the neighbor&#8217;s top line,</span><br />
<span style="color:#00ccff;"> clear the cell&#8217;s bottom flag;<br />
clear the neighbor&#8217;s top flag; exit.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s left line is the neighbor&#8217;s right line,</span><br />
<span style="color:#00ccff;"> clear the cell&#8217;s left flag;<br />
clear the neighbor&#8217;s right flag; exit.</span></p>
<p>We know the passed cells are neighbors, but we didn’t keep track of their relative locations, so we check all four. (And again, all those IFs fit on one line each on the screen in our editor.)</p>
<p>And that’s it for the Aldous-Broder algorithm, except to see if we’re done, which is this routine’s job:</p>
<p><span style="color:#00ccff;">To decide if all of some cells have been visited:</span><br />
<span style="color:#00ccff;"> Get a cell from the cells.</span><br />
<span style="color:#00ccff;"> If the cell is nil, say yes.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s visited flag is not set, say no.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>We now have a <em>perfect</em>, <em>uniform</em> maze, and we just have to select some starting and ending points. We pick eight of each, almost at random. I say <em>almost </em>because we always select the top left cell as a starting point, and the bottom right cell as an ending point, to make sure that there is a least one long path available for the player.</p>
<p><span style="color:#00ccff;">To select starting and ending cells in a maze:</span><br />
<span style="color:#00ccff;"> Set the maze&#8217;s cells&#8217; first&#8217;s start flag.</span><br />
<span style="color:#00ccff;"> Set the maze&#8217;s cells&#8217; last&#8217;s end flag.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Pick a cell in the maze.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s start flag is set, repeat.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s end flag is set, repeat.</span><br />
<span style="color:#00ccff;"> Add 1 to a count. If the count is greater than 14, break.</span><br />
<span style="color:#00ccff;"> If the count is odd, set the cell&#8217;s start flag; repeat.</span><br />
<span style="color:#00ccff;"> If the count is even, set the cell&#8217;s end flag; repeat.</span></p>
<p>And that’s the gist of the whole program. A couple of side notes on Plain English looping, however, might be helpful here.</p>
<p>1. When there is no explicit LOOP label, REPEAT means “repeat from the top of the routine”.</p>
<p>2. BREAK always passes control to the first statement following the <em>last </em>REPEAT.</p>
<p>3. Looping through lists almost always happens like this:</p>
<p><span style="color:#00ccff;">To draw some cells:</span><br />
<span style="color:#00ccff;"> Get a cell from the cells.</span><br />
<span style="color:#00ccff;"> If the cell is nil, break.</span><br />
<span style="color:#00ccff;"> Draw the cell with the black pen.</span><br />
<span style="color:#00ccff;"> Repeat.</span><br />
<span style="color:#00ccff;"> Refresh the screen.</span></p>
<p>If a nil pointer is passed to the “Get” routine, the first item on the list, if any, is returned. “A cell” in this case, indicates a new local variable initialized to zero, so that’ll work. If a non-nil pointer is passed to the “Get” routine, the next item on the list, if any, is returned. A nil pointer is returned when the whole list has been processed. All of that is done, not by the compiler, but by a generic routine in our standard “noodle” library:</p>
<p><span style="color:#00ccff;">To get a thing from some things:</span><br />
<span style="color:#00ccff;"> If the things are empty, void the thing; exit.</span><br />
<span style="color:#00ccff;"> If the thing is nil, put the things&#8217; first into the thing; exit.</span><br />
<span style="color:#00ccff;"> Put the thing&#8217;s next into the thing.</span></p>
<p>But that’s all stuff for other articles. Back to the maze. This is how we actually draw those cells, taking all of those little flags into account:</p>
<p><span style="color:#00ccff;">To draw a cell with a pen:</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s left flag is set,<br />
</span><span style="color:#00ccff;">draw the cell&#8217;s left line with the pen.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s top flag is set,<br />
draw the cell&#8217;s top line with the pen.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s right flag is set,<br />
draw the cell&#8217;s right line with the pen.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s bottom flag is set,<br />
draw the cell&#8217;s bottom line with the pen.</span><br />
<span style="color:#00ccff;"> Put the cell&#8217;s width divided by 3 into a width.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s start flag is set,</span><br />
<span style="color:#00ccff;"> draw a dot the width wide at the cell&#8217;s box&#8217;s center with the dark green pen.</span><br />
<span style="color:#00ccff;"> If the cell&#8217;s end flag is set,</span><br />
<span style="color:#00ccff;"> draw another dot the width wide at the cell&#8217;s box&#8217;s center with the dark red pen.</span></p>
<p>Almost forgot! This is how we pick a random cell anywhere in the maze:</p>
<p><span style="color:#00ccff;">To pick a cell in a maze:</span><br />
<span style="color:#00ccff;"> Pick a number between 1 and the maze&#8217;s cells&#8217; count.</span><br />
<span style="color:#00ccff;"> Void the cell.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Get the cell from the maze&#8217;s cells.</span><br />
<span style="color:#00ccff;"> If the cell is nil, break.</span><br />
<span style="color:#00ccff;"> Add 1 to a count. If the count is the number, break.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>And that’s all I have to say about that. I’m speechless. Amazing.</p>
</div>
]]></html><thumbnail_url><![CDATA[https://i1.wp.com/cdncontribute.geeksforgeeks.org/wp-content/uploads/multimaze-1-1024x819.png?fit=440%2C330&ssl=1]]></thumbnail_url><thumbnail_width><![CDATA[413]]></thumbnail_width><thumbnail_height><![CDATA[330]]></thumbnail_height></oembed>