MAZE Generator

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

 

Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Problem Description

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.

Background & Techniques

The program has options to control "room" sizes and the number of rooms across and down.  A Generate button creates a random path from S (Start) to E (End) along with a number of dead end false paths.  The Design button allows you to define border rooms; blocked out areas of the maze, and saved mazes may be concatenated together to form words or mazes like the example above.  Additional options allow customizing  colors, path width.  Completed mazes may be solved on-screen or printed with or without the solution path displayed.

Non-programmers are welcome to read on, or may skip to the download section at the bottom of this page.        

Notes for Programmers

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  uses the new versions.  As usual a few minor bugs found and corrected while testing the changes.   

Addendum December 8, 2008:  Version 3 has some improvements in the way that larger mazes are scrolled and printed.  Also arrow keys may now be used to trace out the solution path when solving the displayed version of a maze.   

February 22, 2011: Version 3.1 fixes a memory leak and corrects a minor problem noticed by one of our viewers.  Multiple room moves made by clicking on and in-line room several rooms away could not be completely erased.    I believe it is fixed now.  

March 17, 2011: The same viewer who discovered the February bug has been at it again,  This time he found that paths drawn by mouse clicks could, ghostlike, pass through maze walls as if they weren't there!   A one line fix to check for a wall before moving corrects the problem and was posted today as Version 3.2. I   

Running/Exploring the Program 

Suggestions for Further Explorations

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: 
1) the ability to close rooms, as well as open them.  I would modify MakeOpenRooms procedure to keep track of walls removed so that another button could replace them.  Currently un-checking the" make open rooms" checkbox only  takes effect when the maze is regenerated. 
2)  An init file to automatically save  customized option settings ( maze and room sizes, colors, etc.),  and restore them at startup time. 
3) There's room in the panel at the left of the main form for a Tmemo to contain some extended help or hints, one message at start up, one when the maze has been generated and is ready to play, print, or save, and one when in design mode outlining the actions available.
In getting Maze ready to publish I converted the internal  representation from a  fixed size array to a doubly dimensioned dynamic array (by rhian hanson).  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.   

 

Created: December 10, 2001

Modified: February 18, 2016

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2016, Gary Darby    All rights reserved.