I would maintain a list of blocks that still need to be cleared. On each iteration, if the current block has no mines surrounding it, add all surrounding blocks to the list that have not been added yet. Something like this:
| void ClearBlock(Point currentBlock) |
| { |
| var blocksToClear = new List<Point>(); |
| blocksToClear.Add(currentBlock); |
| |
| SetBlockAsVisited(currentBlock); |
| |
| for (int i = 0; i < blocksToClear.Count; ++i) |
| { |
| if (IsMine(currentBlock)) |
| { |
| // BOOM! |
| } |
| else if (!IsAdjacentToMine(currentBlock)) |
| { |
| for (int y = currentBlock.Y - 1; y <= currentBlock.Y + 1; ++y) |
| { |
| for (int x = currentBlock.X - 1; x <= currentBlock.X + 1; ++x) |
| { |
| Point nextBlock = new Point(x, y); |
| if (nextBlock != currentBlock && |
| !IsBlockVisited(nextBlock)) |
| { |
| blocksToClear.Add(nextBlock); |
| } |
| } |
| } |
| } |
| } |
| } |
| |
Edit: You could also use recursion, if you wanted. The implementation is a bit easier, but there is the potential for performance issues, depending on the size of your maps. Also, the iterative approach I've shown you here is adaptable to occur over multiple frames, in case you want to use animations when clearing a block.
Previously known as "Rainault".
Twitter -
me,
Jade Vault GamesAnnouncing
ASCII Quest, a Roguelike under development for Xbox LIVE Indie Games