I’m playing around with a random map generator. I have a load of tiles in a table, each named tile[i]. They’re all either blue or green. For each green tile, I’m trying to check if any of the surrounding tiles are blue. If they are, I turn that green tile yellow (for sand).
The code for this is already quite long (I have to check the 8 tiles surrounding). The problem is, for tiles on the edge (first or last row, first or last column), some of the tiles around them that I’m trying to check don’t exist.
Before I check the tiles surrounding, I could check whether the grass tile is one of these, and then change the check accordingly. But this would give me about 10x more lines of code (separate checks for row=0, column=0; row=max, column=max; row=0, column=max; row=max, column=0; just row=0; just row=max; just column=0; just column=max). And wouldn’t be very fun. There could be ways around it, like just making sure none of the border tiles are green, but I don’t want to resort to those as this kinda thing might come up in the future.
tl;dr: for all tiles, I’m checking if the tile to the left of it (or right, etc) is water. but for some of those tiles, that tile doesn’t exist so I get an error.
Any ideas or “sophisticated” workarounds? Would be great if the == just returned false instead of an error. thanks!
(now that I think of it, I might have to check for border tiles anyway, as otherwise a tile to the far left would just end up checking a tile to the far right, one row above, when I ask it to check tile[i-1]. still, an answer would be helpful)