Lesson 32. Capstone: Life on Mars

 

32.1. A grid to rove on

Make a grid that the rover can drive around on by implementing a MarsGrid type. You’ll need to use a mutex to make it safe for use by multiple goroutines at once. It should look something like the following:

// MarsGrid represents a grid of some of the surface
// of Mars. It may be used concurrently by different
// goroutines.
type MarsGrid struct {
    // To be done.
}

// Occupy occupies a cell at the given point in the grid. It
// returns nil if the point is already occupied or the point is
// outside the grid. Otherwise it returns a value that can be
// used to move to different places on the grid.
func (g *MarsGrid) Occupy(p image.Point) *Occupier

// Occupier represents an occupied cell in the grid.
// It may be used concurrently by different goroutines.
type Occupier struct {
    // To be done.
}

// Move moves the occupier to a different cell in the grid.
// It reports whether the move was successful
// It might fail because it was trying to move outside
// the grid or because the cell it's trying to move into
// is occupied. If it fails, the occupier remains in the same place.
func (g *Occupier) Move(p image.Point) bool

Now change the rover example from lesson 31 so that instead of only updating its coordinates locally, the rover uses a MarsGrid object passed into the NewRoverDriver function. If it hits the edge of the grid or an obstacle, it should turn and go in another random direction.

32.2. Reporting discoveries