liveBook · Manning

MazeLocation

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

MazeLocation is a book-specific value type representing a row-and-column position in a maze.

A maze search needs a value for each position it visits. `MazeLocation` stores that position as an integer row and column. It is a custom struct rather than a tuple because search data structures need hashable states. section 2.2

struct MazeLocation: Hashable {
    let row: Int
    let col: Int
    var hashValue: Int { return row.hashValue ^ col.hashValue }
}

func == (lhs: MazeLocation, rhs: MazeLocation) -> Bool {
    return lhs.row == rhs.row && lhs.col == rhs.col
}

The struct defines `row`, `col`, a hash value, and equality based on both coordinates. section 2.2

A goal test compares a location with the goal at `(9, 9)`. Successor generation checks the four adjacent positions, excluding maze edges and blocked cells. The resulting locations can be searched and marked as a path through the maze. section 2.2

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines coordinates, hashing, and equality, then uses the type in bounded four-direction movement.
Examples
Uses `(0, 0)` as a start, `(9, 9)` as a goal, and marked maze paths.
What it assumes
Assumes familiarity with Swift structs and functions while defining the maze-specific position type.
What you can do after
Build it from scratch as a Hashable Swift position type and use it in maze search.
How it connects
Connects Hashable states to depth-first search, breadth-first search, A* search, and distance heuristics.

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.