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:

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

Get Mazes for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.