From: Mike Agres Date: 2006-10-10T11:32:32+09:00 Subject: Re: Learn to Program, by Chris Pine here's the whole code... # These are just to make the map easier to read. "M" is # visually more dense than "o". M = 'land' o = 'water' world = [[M,o,o,o,o,o,o,o,o,o,M], [o,M,M,o,M,M,o,o,o,M,M], [o,o,M,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,o], [o,o,o,o,M,M,M,M,o,o,o], [o,o,o,M,M,M,M,M,M,M,o], [o,o,o,M,M,o,M,M,M,o,o], [o,o,M,M,o,o,M,M,M,o,o], [o,M,M,M,o,M,o,o,o,M,M], [M,o,o,o,o,o,o,o,o,o,M]] def continent_size world, x, y if world[y][x] != 'land' # either it's water or we've already counted it; we don't want to count it # again return 0 end if ((y <= 10 && y > 0) && (x <= 10 && x > 0)) # So, first we count this tile... size = 1 world[y][x] = 'counted land' # ...then we count all of the # neighboring eigth tiles (and, # of course, their neighbors via recursion) 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 end puts continent_size(world, 5, 5) #this should be fine; but what about (11,11)? -- Posted via http://www.ruby-forum.com/.