Borrar filtros
Borrar filtros

Clearing all empty cells for minesweeper (game) code

13 visualizaciones (últimos 30 días)
Rollo el 24 de Feb. de 2014
Editada: C Hart el 12 de Oct. de 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).
Huge thanks in advance!

Respuestas (1)

Doug Hull
Doug Hull el 24 de Feb. de 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
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 comentario
C Hart
C Hart el 12 de Oct. de 2016
Editada: C Hart el 12 de Oct. de 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! :)

Iniciar sesión para comentar.


Más información sobre Video games en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by