Created
March 18, 2013 04:25
-
-
Save Stephenitis/5185093 to your computer and use it in GitHub Desktop.
Recursion from chris pine's learning to program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These are just to make the map # easier for me 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 | |
# So first we count this tile... | |
size = 1 | |
world[y][x] = ' counted land' | |
# ...then we count all of the | |
# neighboring eight tiles (and, | |
# of course, their neighbors by | |
# way of the 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 ) | |
 | |
Report erratum | |
RECURSION 83 | |
 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