Clearing all empty cells for minesweeper (game) code
조회 수: 25(최근 30일)
표시 이전 댓글
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:
- A square is clicked, it contains no mine and no neighbouring mines
- Uncover this square and its surrounding 8 squares
- Find which of these 8 squares also have no neighbouring mines
- Uncover the 8 neighbours of these squares
- 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).
Huge thanks in advance!
댓글 수: 0
Doug Hull 2014년 2월 24일
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
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.
댓글 수: 1
C Hart 2016년 10월 12일
편집: C Hart 2016년 10월 12일
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! :)
Find more on Video games 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!