Typing out SetWall calls to build a test map is tedious. Today you parse a grid straight from an ASCII drawing, where a dot is open and a hash is a wall, so every later test can describe its map as a picture.
Parse a multi-line ASCII string into a grid, reading '.' as open and '#' as a wall.
Building a test map cell by cell does not scale past a few walls. Every pathfinding
example in books and articles is drawn as a picture, so we teach the grid to
read that picture directly. The convention is the standard one: a dot (.) is
an open cell and a hash (#) is a wall. Rows are separated by newlines.
The parse is mechanical: the number of lines is the height, the length of a line is
the width, and any # marks a wall at that column and row. Trimming a trailing
newline first keeps a final blank line from inventing an empty row. From now on a
test can hand you a whole maze as a few lines of text, which is exactly how the spec
for every search in this project will describe its map.
// split on newlines; height is line count, width is line lengthfunc ParseGrid(s string) *Grid {lines := strings.Split(strings.TrimRight(s, "\n"), "\n")g := NewGrid(len(lines[0]), len(lines))for y, line := range lines {for x := 0; x < len(line); x++ {if line[x] == '#' { g.SetWall(Coord{x, y}) }}}return g}