Wednesday, August 21, 2019

algorithm - C# Minesweeper - When cells with no mines nearby gets clicked


I'm currently making a Minesweeper in C#. I've already done the basics (generating minefield, rules and ui) but I don't know how I should implement when the user clicks a cell and the cell has no mines nearby and clicks nearby cells with also no mines nearby, like this:


Minefield before click


And after click


Minefield after click


This is my current code on this:


if (MineField(x,y) == false) // If the clicked cell isn't a mine

{
if (GetNeighbours(x,y) == 0) // GetNeighbours returns the amount of mines in the closest 8 cells
{
// To be honest, no idea...
}
}

Answer



One way to do this, is to use recursive Flood Fill algorithm. Something like:


If cell(x,y) has no bomb: 1. If coordinates (x,y) didn't went beyond game board, and cell (x,y) wasn't marked as visited:


a) give cell (x,y) property visited to true (or mark it in any other way) b) if getNeighbours(x,y) > 0: print number of cell (x,y) c) if getNeighbours(x,y) == 0: reveal cell (x,y) and call whole function responsible for revealing field for coordinates (x-1,y-1),(x-1, y), (x-1, y+1), (x, y+1), (x+1, y+1), (x+1, y), (x+1, y-1), (x, y-1).



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...