Fix bug in terrainFunction.
Implement neighbors and legalNeighbors. Add tests.