• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
blog.draftsperson.net

blog.draftsperson.net

the art of technical drawing

blog.draftsperson.net

HANOI LISP: Tower of Hanoi Built and Solved in 3D

August 6, 2026

HANOI builds the puzzle as real geometry — a base, three posts and a stack of discs — and HANOISOLVE solves it in front of you, one move at a time.

What TowerHanoi does

The rules, unchanged since Lucas set the puzzle in 1883:

1. Only one disc moves at a time.
2. No disc may rest on a smaller one.

The legend has monks moving sixty-four discs, the world ending when they finish. The arithmetic is the interesting part: n discs take 2n − 1 moves, so ten discs is a thousand moves and twenty is a million. Sixty-four, at one move a second, is about 585 billion years. The legend is safe.

How to use it

  1. Load the routine with APPLOAD, or put it on your support file search path.
  2. Type HANOI and give the number of discs.
  3. Type HANOISOLVE and watch.

How it works

To move n discs from A to C: move the top n−1 to B, move the bottom one to C, then move the n−1 from B to C. Three lines, and it is optimal — no shorter solution exists. Recursion rarely looks better than this.

Discs are created with entmake, held by entity name, and moved. Removing a disc with (command "erase" <point> "") is an erase by picking a computed screen coordinate — anything else near that point goes instead. Moving the object you already have a handle on is both correct and far faster.

The puzzle knows its own objects. Tidying up between runs by erasing the last N objects drawn, N being remembered from the previous run, erases whatever you happened to draw in between.

AutoCAD’s option word is Color, not Colour. Every (command "layer" ... "colour" ...) in the original failed on that, and the discs came out monochrome at best.

Notes and limits

  • Ten discs is 1,023 moves. Start small.
  • It is real 3D geometry, so you can orbit round it.
  • Nothing else in the drawing is touched.
  • One undo group covers the build and the solve.

Download and details

NameTowerHanoi — build the Tower of Hanoi in 3D and watch it solve itself
DownloadTowerHanoi.lsp — 12.5 KB, plain AutoLISP source. Downloads directly; no zip to unpack.
TypeAutoCAD AutoLISP routine
CommandsHANOI, HANOISOLVE
Requiresnothing beyond a CAD platform with AutoLISP.
Source size12.5 KB, 307 lines
Error handlingYes — a *error* handler restores every system variable it changed, on cancel as well as on error.
UndoThe whole operation is wrapped in a single undo group, so one U reverses all of it.
CompatibilityAutoCAD (any release with AutoLISP), BricsCAD, ZWCAD, GstarCAD, ProgeCAD and other IntelliCAD-based platforms. AutoCAD LT needs a LISP enabler. No .NET, no ObjectARX, no installer.
AuthorYZ, August 2026
LicenceFree to use, supplied “as is” with no warranty.

References

  • HILBERT LISP — the other recursion in this set
  • FRACTREE LISP — recursion that looks like a tree
  • 5 crazy things to do with AutoLISP — more of the same spirit
  • More free AutoLISP routines on this site
  • Wisey’s Steel Shapes — free structural steel section drawing program

Filed Under: AutoLISP Routines

Primary Sidebar

Subscribe to our Youtube Channel

Post CAD drafting jobs on Freelancer.com. and get some free money to do this.

Categories

  • American Steel Industry (4)
  • AutoCAD Articles & Reference (30)
  • AutoCAD Bugs & Problems (19)
  • AutoCAD Tips (29)
  • AutoCAD Tutorials (54)
  • AutoLISP Routines (328)
  • AutoLISP Tutorials (15)
  • CAD Standards (7)
  • Civil 3D (7)
  • Civil Drafting (4)
  • Dimensions (2)
  • Drafting Funnies (71)
  • Drafting History (9)
  • Drafting Standards (84)
  • Electrical Drafting (2)
  • General IT reference (7)
  • Geometry (15)
  • GIS (2)
  • Hatch Patterns (12)
  • Office Life Hacks (3)
  • PT (1)
  • Revit Tutorials (54)
  • Steel Detailing (14)
  • Structural Drafting (26)
  • Technical Dictionary (61)
  • Useful Website Links (19)

Footer

Free downloads

  • Free AutoLISP routines
  • Free hatch patterns
  • SHX fonts & text styles
  • Numbering LISP collection
  • ISO 3098B font

Reference charts

  • AWS weld symbols chart
  • Steel detailing chart
  • Hole & slot sizes
  • Bolt edge distance
  • ANSI & ARCH paper sizes
  • How to fold an A1 drawing

Useful links

  • CAD blocks (DWG)
  • Revit families
  • structuraldrafter.com
  • cad-corner.com
  • About
  • Privacy policy

Provided for free with no warranty · Log in