When I was generating the worlds for the game Civilization III, I wanted worlds with two primary supercontinents; those tend to be a lot of fun, and just sort of feel “earthy” and...real. So after I generated the land masses (which was some pretty clever programming there, too), I wanted to test them to see what the sizes of the different continents were. If there were two of relatively equal size (say, differing by a factor of 2 or less) and no others close in size, I’d say that was a pretty good world. The process, then, was something like the following: 1. Build the world. 2. Find a “continent” (which could be a one-tile island... at this point I wouldn’t know). 3. Compute its size. 4. Find another continent (making sure not to count any of them twice but also making sure each gets counted), and repeat the process. 5. Then find the largest two, and see whether they look like like fun to play on. The fun part was in computing each continent’s size, because the best way to do that was recursively. So let’s look at a trimmed-down version. Let’s say we have an 11x11 world (represented as an array of arrays...basically just a grid) and that we want to find the size of the continent in the middle (that is, the continent of which tile (5,5) is a part). We don’t want to count any land tiles belonging to any of the other continents. Also, as in Civilization III, we’ll say that tiles touching only at the corners are still considered to be on the same continent (since units could walk along diagonals). But before we get to the code, let’s solve the problem in English first. My initial plan was to look at every tile on the map, and if that tile is a land tile on the continent I’m looking for, we add 1 to the running total. The problem, though, is how do we know whether a land tile is on the same continent as some other land tile? There are ways to solve this problem, but they all seemed to be too messy; either I was keeping track of lots of information I didn’t feel like I needed, or I seemed to be doing the same computation over and over again. But then I thought, hey, two tiles are on the same continent if you can walk from one to the other. (That was essentially the operating definition of continent in Civilization III.) So that’s how the code should work! First, you count the spot you are standing on (duh); in this case, that means tile (5,5). Then, you send out eight little guys, one in each direction, and tell them to count the rest of the continent in that direction. The only rule is that no one can count a tile that someone else has already counted. When those eight guys return, you add their answers to your already-running total (which is just 1, from the tile you started with), and that’s your answer. Brilliant! Except for one tiny little detail...how are those eight little guys supposed to determine the size of the continent? You just shrugged the problem onto them! The only tile you counted was the one you were standing on. This is pretty frickin’ lazy. Which is probably a good thing.... How are your eight little helpers supposed to compute the size of the continent? The same way you do! So somehow, by a bunch of little, lazy, imaginary helpers counting only the tile they are on, you get the size of the whole continent. (We still need to make sure no tile is counted twice, but we can just mark each tile as it is visited to keep track.) Without further ado, behold the magic of recursion.