Maps with Ruby

Here is a simple example of how to find continents for Civilization III, using Ruby. Although this is a slimed down version, the same principals apply.

# These are just to make the map
# easier to read. "M" is
# visually more dense than "O".

M = 'land'
o = 'water'

world = [[o,o,o,o,o,o,o,o,o,o,o],
				 [o,o,o,o,M,M,o,o,o,o,o],
				 [o,o,o,o,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,o,o,o,o,M,M,o,o,o],
				 [o,M,o,o,o,M,o,o,o,o,o],
				 [o,o,o,o,o,o,o,o,o,o,o]]

def continent_size world, x, y
	if world[y][x] != 'land'
		# Either it's water or we already
		# counted it, but either way, we don't
		# want to count it now.
		return 0
	end

	# First count the tile
	size = 1
	world[y][x] = 'counted land'

	# Next we count all of the
	# neighboring eight tiles(and
	# of course, their neighbors by
	# way of 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

puts continent_size(world, 5, 5)

#=> prints '23' to STDOUT.