## Clearing all empty cells for minesweeper (game) code

### Rollo (view profile)

on 24 Feb 2014
Latest activity Edited by C Hart

### C Hart (view profile)

on 12 Oct 2016
I have been trying to solve this problem all day and I think i'm going insane.
I'm trying to write a code to simulate minesweeper - I dont want to overcomplicate it, but I want to get the basic functionality down. I know there are a few bugs in the code here but the thing I am most struggling on is how to clear the area of "0s" when you click on a grid space that has no mine and no mine neighbours. (Google minesweeper if you don't know what I am referring to).
This is what I want to achieve:
1. A square is clicked, it contains no mine and no neighbouring mines
2. Uncover this square and its surrounding 8 squares
3. Find which of these 8 squares also have no neighbouring mines
4. Uncover the 8 neighbours of these squares
5. Repeat steps 3 and 4 until all squares are uncovered and the perimeter of the "mine free" area is shown
So far I am just sweeping across in one direction until I hit an area where the mines begin to appear, but I don't know how best to keep repeating this until all the area is cleared.
I have attached my code so hopefully it should be easier to visualise my problem. I think I can't think straight after thinking about this for so many hours (I am v new to MatLab).

### Doug Hull (view profile)

on 24 Feb 2014

I like to ask myself "What is the simplest thing that could possibly work?" This is always going to be fast, so how about this routine after you have clicked on an empty square:
"I Changed Something Flag" = True
While "I Changed Something Flag" == True
"I Changed Something Flag" = FALSE
Start in upper left corner, sweep from left to right, top to bottom until you hit bottom Right:
If the current square is empty, and any adjacent square is unknown
reveal unknown square
make the "I changed something flag" true
end
end
end
This will have you sweeping through several times, but it is going to be fast enough most likely. The logic is simple, but inefficient. Inefficient does not matter here, so go for simple.
Doug

C Hart

### C Hart (view profile)

on 12 Oct 2016
I know this is like 2 years later, but could you further explain 'sweeping' through the matrix? (so from upper left corner to lower right corner) And maybe further explain your answer? In words I understand what you are saying, however when trying to use this in code I don't understand what you mean by 'false' and 'true' and 'If the current square is empty, and any adjacent square is unknown, reveal unknown square' Thank you so much! :)