O'Reilly logo

Mazes for Programmers by Jamis Buck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Displaying a Maze on a Terminal

ASCII art is not necessarily the fanciest way, nor the prettiest, but it is often the most convenient way to display our mazes. We nearly always have easy access to a terminal, and we don’t need to worry about bringing out any big guns (like external libraries or APIs). In short, it’s perfect for what we need just now. Let’s walk through one possible way to approach drawing our mazes using only four different characters: space (“ ”) for cells and passages, pipe (“|”) for vertical walls, hyphen (“-”) for horizontal walls, and plus (“+”) to draw corners. Here’s an example of a small maze drawn using these characters:

 
+---+---+---+---+
 
| |
 
+ + +---+ +
 
| | | |
 
+---+---+ +---+
 
| |
 
+ +---+ + +
 
| | | |
 
+---+---+---+---+ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required