How to Build a Maze
Total Page:16
File Type:pdf, Size:1020Kb
How to Build a Maze David Matuszek Department of Computer Science 8 Ayres Hall University of Tennessee Knoxville TN 37916 Mazes are fun to solve. With a little random traditional maze. creation of this spanning tree that is imagination, mazes can be incorpor the tricky part of building a maze. ated into many different computer The General Approach Finally, the border of the maze is games. If you know how, it's a simple We begin with a rectangular array. broken in two places to provide a matter to use the computer to gener Each cell of the array is initially com start and a finish position. Since there ate random mazes. pletely "walled in," isolated from its is a unique path between any two A traditional maze has one starting neighbors (see figure 1). cells of the maze, there will be a point and one finishing point. In ad Secondly, we judiciously erase unique path from start to finish. dition, all locations inside the maze walls inside the array until we arrive Hence, start and finish can be chosen are reachable from the start, and at a structure with the following in any convenient manner, say, at there is one and only one path from property: for any two 'cells of the ar random locations on opposite sides of start to finish. While it is easy to place ray, there is only one path between the maze (see figure 3). doorways and barriers randomly in them. Thus, any cell can be reached side it maze, it is more difficult to from any cell, but only by a single Building the Spanning Tree satisfy the two latter constraints. This unique path (see figure 2). Computer Starting with a fully "walled-up" article describes a fairly simple science jargon refers to such a struc array (see figure 1), pick a single cell method that efficiently produces a ture as a spanning tree, and it is the in the array and call , this cell the Figure' 1: The initial array from which the Figure 2: One possible spanning tree for Figure 3: The spanning tree from figure 2 maze will be constructed. the array in figure 1. with possible entry and exit points added. 190 December 1981 © BYTE Publications Inc Circle 20 on inquiry card. --+ II! I I a b c d e Figure 4: Initial steps involved in building a maze. The cell added at each step is marked with an asterisk. The next cell to be added to the maze will be selected from the shaded frontier cells. spanning tree. Then adds cells one at The algorithm follows: it to, and erase the appropriate bar a time to the spanning tree until it rier. fills the entire array. 3. Check the cells adjacent to the cell At any point during this procedure, 1. Choose any cell of the array and just added to the spanning tree. Any there will be three types of cells in the call it the spanning tree. The four cells such cells that are not part of the array: immediately adjacent to it (fewer if it spanning tree and have not previous is on an edge or in a corner) thus ly been marked as frontier cells must • those that are already in the span become frontier cells. now be marked as frontier cells. ning tree 2. Randomly choose a frontier cell 4. If any frontier cells remain, go • those that are not in the spanning and connect it to one cell of the cur back to step 2. tree, but are immediately adjacent rent spanning tree by erasing one bar 5. Choose start and finish cells. (horizontally or vertically) to some rier. If it is adjacent to more than one cell in the spanning tree (we call these cell of the spanning tree (and it could cells frontier cells) be adjac~nt to as many as four!), ran Figure 4 shows the first few steps in • all the other cells domly choose one of them to connect building a maze. In each case the ar ray is shown as it would be just before execution of step 2 of the algorithm. Note that the newly added 96K CP/M@ cell (marked by an asterisk) in figure 4e was adjacent to two cells in the (For your TRS-80* Model II) spanning tree, yet it was connected to only one of these (the one to its left) Multiple Job Executive by randomly choosing and erasing Add a whole new dimension to your one barrier. TRS-80 Model II. Let it work while you If you're mathematically inclined, . work!! it· is easy to show by induction that this process results in a-spanning tree. ATON's unique JobStream™ CP/M 2.2, along with additional RAM memory; allows you to simultaneously compile, assemble, or link in one 64K background partition 162K TPAI while you When the tree consists of a single cell, edit files, and spool to the printer, and communicate with another computer in up to four 32K there is (vacuously) only one path be foreground partitions 128K TPAI. As you expand memory beyond 64K, you also enter the amazing world of TrackMode BIOS™ tween any pair of cells. As each new which not only multiplies diskette speed up to five times, but also automatically performs read cell is added, it forms no new paths after write checks for the ultimate in data reliability. • Gain hard disk performance for a fraction of the cost-and no backup problems!!! between cells already on the tree • Works in 32K, add RAM memory to 256K using standard Radio Shack memory boards . (since the tree is a dead end), and • Supports two sided expansion disk drives 0.2 megabytes per diskettel. there iS,exactly one path from the new JobStream CP/M 2.2 (with Z-80** Debugger) $235 cell to any other cell (you can get out Omni Writer™ Video Text Editor $130 via only one cell, and from that cell Z-80 Debugger Source Code $ 50 there is only one path). Finally, the Package of above (a $415 value) $295 process ends when there are no more "Software with Service" Prepaid, Visa, MasterCard or COD. frontier cells (cells adjacent to the Shipping and handling extra. spanning tree but not in it), and this California residents add 6% sales tax. can happen only when all cells have CP / M ~ Digital Research. In c. OIT WT andy Corp . .. fwZilog Corp . been absorbed into the spanning tree. JobStream, TrackMode BIOS ' · ATDN IntI. 1 Omni Writer " Omnig raphics 260 Brooklyn Avenue, San Jose, CA 95128 Implementation Details (408) 286-4078 You should store in each cell of the 192 December 1981 © BYTE Publications Inc Circle 37 on inquiry card. To perform to Its fullest capabili UniFLEX. strucftj~d tor lorge-scale Support Software ties, your hardware demands microprocessor systems, will not Teohnlcal Systems Consultants software designed t meet the run with minimal systems and"thus offers a full line of state-of-the-art specialized require ents of has avoided design compromise. support software compatible to today's mlcroprocess,ors. State-of (Off-the-shelf versions and OEM FLEX and UnlFLEX, some of which the-art software from Technical licenses are available.) are: Systems Consultants ~ eeps pace • native C and Pascal compliers with the rapid advancements In The FLEX TM OperaHng System for advanced programming computer technology so your FLEX. a powerful. easy-to-use • extended BASIC for business hardware can live up to Its full operating system designed for and educational applications potential. Our comptete line of the 6800 and 6809 microproces • text editing and processing state-of-the-art software Includes: sors, Includes: software • dynamic fllespace allocation • sort/merge package for busi The UnlFLEXTMOperaHng System • random flies ness applloatlons UnlFLEX, a true multl-u'ser, multi • batch job entry • variety of absolute and relocat tasking system for the 6809 and • automatic space compression able assemblers 68000 mlcroprocesso s. supports • English error messages • debug and diagnostic such features as: • user environment control packages • hierarchical file sys ms • disk resident commands · . and more. Write or call today • device Independent 1/0 • flexible device 1/0 for our broohures describing • four Gigabyte disk capacities • printer spooling our oomplete product line. • full file protection Plus. FLEX can accommodate • Inter-task communication via hard disks as well as floppies. The pipes System Is available off-the-shelf :1 = i i i ~ I ' • 1/0 redirection for a variety of systems and In a , '-< - • task swapping field-adaptable version. epEM ~~§i; • full random-access flies lIoenses available.) Box 2570,1208 Kent Avenue • comprehensive shell comma d West Lafayette. Indiana 47906 language FLEX and UnlFLEX are trademarks of (317) 463-2502 Telex: 27·6143 Technical Systems Consultants. Inc. sc ulpture by Joann Chaney Circle 303 on Inquiry card array a number indicating: 1 . Introdudng whether it is in the spanning tree, in w the frontier, or in neither; and 2. if it Pascal is in the spanning tree, which of the Sourcebooks cell's barriers have been erased. One from the NATI Pascal Software library possibility is to use-1 for frontier I PHJCEIXJRE wand input· cells, positive numbers for cells, I BEnIN {wand-. positive numbers for cells in the span I - ning tree, and 0 for all other cells. Complete I Ann~tated any cell of the spanning tree is open I Program Usdngs to at least one other cell, I suggest the I Cross Refe,rences and following encoding: start with 0 in Block Summaries each cell, add 1 if the barrier on the Block Indices by Page This new library of UCSD Pascal' compatible pro 5a right is erased; add 2 if the barrier grams provides practical applications and system below is erased; add 4 if the barrier augmentations available nowhere else.