
Here's a Maze Generator program that's kind of fun. You can generate, solve, and save mazes of varying sizes and shapes. A set of letter shaped mazes is provided that can be concatenated (strung together) to make a "name-maze" of your favorite person.
Some more advanced code too.
This is a fairly large program, probably 2,000 lines of code. A thorough discussion would fill a book, so I'll just hit the high spots here.
A TMaze class in a separate U_TMaze unit contains the logic for most maze operations. MakeImage procedure generates a random path through the maze from Startpoint to Endpoint, making sure that it doesn't double back on itself. Then MakeFalsePaths is called to open up additional walls by generating paths starting at right angles to the solutions path. Again each path must remain independent of other paths, i.e. never knock down that last wall connecting two paths. Optionally, MakeOpenRooms can be called to remove at least one wall of any remaining closed rooms, just to make finding the solution a little more difficult. The TMaze mouse routines (Mmousedown, Mmousemove, and Mmouseup), allow the user to click and/or drag the solution path. Surprisingly, these were the hardest to debug. The trick is to size the program window and the code window so that they don't overlap. This allows stepping through code and observing the onscreen results without the debugger getting in the way.
Design mode allows the user to generate mazes of any size or shape. Clicking on rooms in design mode toggles between normal and "border" rooms. Right-click triggers a pop-up menu which provides other control options. You can also shift-left click to invert a rectangular range of rooms from the previous clicked to the current.
TFileStream objects are used to save and reload generated mazes. At reload time, mazes can be concatenated to the current maze. A set of "letter" mazes is available for download which can be used to make word or name mazes. Kids love them. By the way, the letter maze files have the "read only" attribute set, to avoid accidentally modifying them.
Maze also serves as an example of using Printer class and TCanvas Copyrect and Strecthdraw procedures to implement print preview and print operations. Margins can be set and header and footer text added to printed mazes. Mazes can be scaled to fit on a single page or span several pages.
Addendum March 22, 2008: The changes today are primarily for programmers who want to recompile Maze. Version 1 used registered versions of my numeric edit components TIntEdit and TFloatEdit to get numeric data from the user for cell size, board size and print margins. Since these are visual components, they had to be installed before use which is not good for long lived projects. You can learn more here about my replacements which are created at program startup time and avoid the problem of losing components when operating system, computer, or Delphi versions. Version 2 has uses the new versions. As usual a few minor bugs found and corrected while testing the changes.
| I've considered generalizing the code to make 6-sided (hexagonal) rooms. I think the problems are pretty much mechanical, no new logical problems to solve. Which of course makes the project less appealing. | |||||||
Other
future program features include:
|
|||||||
| In getting Maze ready to publish I converted the internal representation from a fixed size array to a doubly dimensioned dynamic array. I corrected the "last" bug relating to this (inserting & deleting columns and rows in design mode), about 10 minutes ago. So there may be a few rough edges remaining for you or me to fix. |