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\$
2
  • \$\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, 2014 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, 2014 at 18:48

1 Answer 1

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\$
1
  • \$\begingroup\$ I've read up on Flood Fill and I think it will work. Thank you. \$\endgroup\$
    – Megabytte
    Nov 7, 2014 at 12:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .