clearing all empty cells for minesweeper (game) code
7 views (last 30 days)
Show older comments
How can I create something that when I click on one of the zero's all the other zeros appear aswell?
I found some advice from Dough Hull on 24 Feb 2014:
"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
However, I don't completely understand this, is there anyone that might be able to explain it a little further? For example, how do I sweep accross the matrix (from left to right)? And how do I actually make it 'reveal' the unknown square?
1 Comment
Answers (1)
Geoff Hayes
on 14 Apr 2020
Edited: Geoff Hayes
on 14 Apr 2020
The original answer can be found at https://www.mathworks.com/matlabcentral/answers/118873-clearing-all-empty-cells-for-minesweeper-game-code but I think that the idea was to keep sweeping over every cell in the matrix until no new cell has been revealed....because every time that you reveal a 0, then you would need to check all neighbouring zeros to ensure that they have been revealed too. And since that new reveal might affect a cell (in a previous column or row) that should be revealed, then a new sweep must be started. If we assume we have two identically sized matrices with one representing the zeros and ones (gamingMatrix), and the other representing whether an cell has been revealed (revealedMatrix) then the sample code might look like
hasRevealedElement = true;
while hasRevealedElement
hasRevealedElement = false;
for col=1:size(gamingArea,2)
for row=1:size(gamingArea,1)
if gamingArea(row,col) == 0 && revealedMatrix(row,col) == true
if revealEmptyAdjacentNeighbours(row,col)
hasRevealedElement = true;
end
end
end
end
end
The function revealEmptyAdjacentNeighbours reveals any empty (0) adjacent neighbours to the current cell. It returns true if at least one adjacent neighbour has been revealed. If a sweep has been made where no new element has been revealed, then we know that all 0's adjacent to the one that was revealed by the user, have been revealed too.
Other approaches could be considered as well: a recursive function could be used to reveal all empty adjacent cells to the current revealed one. The function would then be called for all cells that were just revealed, continuing in this fashion until all appropriate cells have been revealed.
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!