1
\$\begingroup\$

I have this tile-based game I've been messing around with and I want to have a way to check if a set of wall tiles make an enclosed space.

Here's some more details: I have a tile map. Each tile has an ID. If you have a set of IDs like so:

0,0,0,0,0
0,1,1,1,0
0,1,2,1,0
0,1,1,1,0
0,0,0,0,0

0 is grass, 1 is wall, and 2 is floor. Assuming these have a spot in a 2d array, how can I find and confirm if whether or not the floor tile is surrounded by walls. This method has to be scalable as well. So if the floor area is 2x2 it would still work.

I've already tried implementing a way to check it, but I failed on my first couple of tries. Any recommendations?

\$\endgroup\$
  • \$\begingroup\$ your question is a bit unclear. Given what you write, I'd check every boundary tile to see if it has a wall, as that's how I understand what a "closed space" is, but you might mean something different \$\endgroup\$ – Babis Nov 6 '14 at 17:53
  • \$\begingroup\$ Sounds to me like the classic problem encountered in Maze Solving or Game Of Life algorithms \$\endgroup\$ – dot_Sp0T Nov 6 '14 at 18:48
2
\$\begingroup\$

You could use a modified Flood Fill starting from a floor tile to check for the presence of grass tiles. In this case, the wall would count as a boundary and floor tiles would count as open space. If at any point during the flood fill you reach a grass tile then you know that the floor is not surrounded by walls.

\$\endgroup\$
  • \$\begingroup\$ I've read up on Flood Fill and I think it will work. Thank you. \$\endgroup\$ – Megabytte Nov 7 '14 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.