Skip to content

Instantly share code, notes, and snippets.

@RainMonster
Created May 22, 2013 01:21
Show Gist options
  • Select an option

  • Save RainMonster/5624592 to your computer and use it in GitHub Desktop.

Select an option

Save RainMonster/5624592 to your computer and use it in GitHub Desktop.
Modified continent exercise from Chris Pine's "How to Program"
M = 'land'
o = 'water'
world = [[o,o,o,o,M,o,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,M,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,M],
[o,o,o,o,M,M,M,M,o,o,M],
[M,M,M,M,M,M,M,M,M,M,M],
[o,o,o,M,M,M,M,M,M,o,o],
[o,o,o,M,M,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,M,M,o,o,o,o]]
def continent_size world, x, y
if world[y][x] == nil
return 0
end
if world[x][y] == nil
return 0
end
if world[y][x] != 'land'
return 0
end
size = 1
world[y][x] = 'counted land'
size = size + continent_size(world, x-1, y-1)
size = size + continent_size(world, x , y-1)
size = size + continent_size(world, x+1, y-1)
size = size + continent_size(world, x-1, y )
size = size + continent_size(world, x+1, y )
size = size + continent_size(world, x-1, y+1)
size = size + continent_size(world, x , y+1)
size = size + continent_size(world, x+1, y+1)
size
end
puts continent_size(world, 5, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment