Skip to content

Instantly share code, notes, and snippets.

@inukshuk
Created October 31, 2011 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inukshuk/1329348 to your computer and use it in GitHub Desktop.
Save inukshuk/1329348 to your computer and use it in GitHub Desktop.
M = 'M'
o = 'o'
X = 'X'
world = [
[o,o,o,o,o,M,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,M,o,o,M,M,o],
[o,o,o,M,o,M,o,o,o,M,o],
[o,o,o,o,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[M,M,M,M,M,M,M,M,M,M,M],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,M,o,o,o,o],
[o,o,o,o,o,M,o,o,o,o,o]]
def map(world)
world.map(&:join).join("\n")
end
def stack_trace(world, x, y)
puts "\n\nLooking at tile (#{x},#{y})"
puts map(world)
end
def continent_size world, x ,y
stack_trace world, x, y
if x < 0 or x > 10 or y < 0 or y > 10
return 0
end
if world[y][x] != M
return 0
end
size = 1
world [y][x] = X
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