필터 지우기
필터 지우기

clearing all empty cells for minesweeper (game) code

조회 수: 2 (최근 30일)
C Hart
C Hart 2016년 10월 13일
편집: Geoff Hayes 2020년 4월 14일
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
Mollymomo
Mollymomo 2018년 10월 13일
Hi, this is two years later, but did you figure it out?

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

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 4월 14일
편집: Geoff Hayes 2020년 4월 14일
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.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by