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.

Now you can start several rovers by calling NewRoverDriver and see them drive around together on the grid.

32.2. Reporting discoveries

We want to find life on Mars, so we’ll send several rovers down to search for it, but we need to know when life is found. In every cell in the grid, assign some likelihood of life, a random number between 0 and 1000. If a rover finds a cell with a life value above 900, it may have found life and it must send a radio message back to Earth.

Unfortunately, it’s not always possible to send a message immediately because the relay satellite is not always above the horizon. Implement a buffer goroutine that receives messages sent from the rover and buffers them into a slice until they can be sent back to Earth.

Implement Earth as a goroutine that receives messages only occasionally (in reality for a couple of hours every day, but you might want to make the interval a little shorter than that). Each message should contain the coordinates of the cell where the life might have been found, and the life value itself.

You may also want to give a name to each of your rovers and include that in the message so you can see which rover sent it. It’s also helpful to include the name in the log messages printed by the rovers so you can track the progress of each one.

Set your rovers free to search and see what they come up with!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset