As part of a project I'm working on, I want to divide tiles containing water into 'seas' and 'lakes'. This is done by first finding all water tiles on the edge of the map, and designating them as 'sea' tiles. All water tiles adjacent (no diagonals) to a sea tile are also sea tiles. All other water tiles are 'lake' tiles.
My method for finding the first sea tiles (on the edges of of board) works without flaw. However, my method for finding other sea tiles often leaves out large rectangular areas that should be sea. The code is as follows: (seed is a boolean array where true is land and false is water, seas is another boolean array where true is a sea tile and false is not)
for (int x = 0; x < length; x++)
{
for (int y = 0; y < height; y++)
{
if (!seed[x, y])
{
if (x + 1 < length && seas[x + 1, y])
seas[x, y] = true;
else if (x - 1 >= 0 && seas[x - 1, y])
seas[x, y] = true;
else if (y + 1 < height && seas[x, y + 1])
seas[x, y] = true;
else if (y - 1 >= 0 && seas[x, y - 1])
seas[x, y] = true;
}
}
}
I cannot for the life of me find a typo in this code, maybe a fresh set of eyes will help?
The next step of my code is to find all water tiles that are not sea tiles and designate them as lake tiles. This is done with the following snippet: (seas and seed are same as above, and lakes is a boolean array where true is a lake tiles and false is not)
for (int x = 0; x < length; x++)
{
for (int y = 0; y < height; y++)
{
if (!seed[x, y] && !seas[x, y])
{
lakes[x, y] = true;
}
}
}
This is another possible source of error, but once again I am at a loss for how it is broken.
Attached is an image that is generated based on the three boolean arrays generated. Green is a land tile, blue is a sea tile, and aqua (or lighter blue) is a lake tile. As you can see, there are large areas of water that are adjacent to sea tiles, that should also be sea tiles, but are instead lake tiles.

Thank you in advance.
As a messy mathematical solution, You can repeat first block of code N time (where N is equal to your length variable perhaps N-1 times because you do a +-1 check at first time). The reason is that you may jump of some tiles which its neighbors are not marked yet but will be marked later. So you have to come back again later and check if its neighbors are marked. Obviously there are more optimum methods which need less process.
for (int z=0;z < length; z++){
for (int x = 0; x < length; x++)
{
for (int y = 0; y < height; y++)
{
if (!seed[x, y])
{
if (x + 1 < length && seas[x + 1, y])
seas[x, y] = true;
else if (x - 1 >= 0 && seas[x - 1, y])
seas[x, y] = true;
else if (y + 1 < height && seas[x, y + 1])
seas[x, y] = true;
else if (y - 1 >= 0 && seas[x, y - 1])
seas[x, y] = true;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With