# Clearing all empty cells for minesweeper (game) code

조회 수: 29 (최근 30일)
Rollo 2014년 2월 24일
편집: C Hart 2016년 10월 12일
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).

댓글을 달려면 로그인하십시오.

### 답변 (1개)

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
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
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -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! :)

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Video games에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by