ASCII Masks

Imagine if we could “draw” our masks in a separate file and feed that file to our program. We could play with all kinds of designs, without having to change our program at all! Let’s make that happen, using simple text files as input. The text files will be formatted something like the following, which shows one possible definition of a 10×10 grid.

mask.txt
 
X........X
 
....XX....
 
...XXXX...
 
....XX....
 
X........X
 
X........X
 
....XX....
 
...XXXX...
 
....XX....
 
X........X

Every “X” indicates an “off” cell, and anything else (the “.” characters, in this case) will be an “on” cell.

We’ll implement this by introducing a helper method on our Mask class. Open mask.rb and add the following just after the start of that class, before ...

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.