0
\$\begingroup\$

This question already has an answer here:

I'm creating a tile based game similar to Minesweeper. When a user clicks on a tile and its not a mine or a number space I need to search and expose the adjacent tiles that are empty and stop when it hits a number or the edge of the board. I have it working right now so that it will clear the row of the tile clicked and stop at a numbered tile or the edge of the board. What I have is going to get ugly quickly and I am sure there is a easier way of doing this. Is there a searching algorithm I can use that will find all adjacent tiles to the one the player click only stopping once it hits the edge of the board or hit a numbered tile?

\$\endgroup\$

marked as duplicate by DMGregory, Jesse Dorsey Oct 27 '17 at 20:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ This sounds like a basic flood fill, which can be implemented with a bog standard depth-first or breadth-first search or scanline algorithms. If your implementation works but isn't as clean or as fast as you'd like, you can try sharing your code on the Code Review exchange for improvement notes. \$\endgroup\$ – DMGregory Oct 27 '17 at 19:17
  • \$\begingroup\$ Also, don't neglect to search for your keywords first to see if someone's already answered a similar question. It looks like this covers the same issue: C# Minesweeper - When cells with no mines nearby gets clicked \$\endgroup\$ – DMGregory Oct 27 '17 at 20:10
  • \$\begingroup\$ I did a little searching to try and find some code examples but didn't find anything like this. But it appears that flood fill is exactly what I need, thanks. \$\endgroup\$ – Jason Crosby Oct 27 '17 at 20:18
2
\$\begingroup\$

The search algorithm you're looking for is called Flood Fill

Every empty tile is colour A. Every non-empty tile is colour B. Then you apply the flood fill with the modification that you also stop filling in that direction when there's an adjacent non-empty tile.

Alternatively: every empty-with-no-adjacent-non-empty tile is colour A, all the others colour B, and you apply a flood fill then enlarge the filled area mask by 1 tile in all directions.

\$\endgroup\$
  • \$\begingroup\$ I just finished implementing this and it works like a charm. Thanks for the help. \$\endgroup\$ – Jason Crosby Nov 2 '17 at 16:43

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