We all know it. The idea is simple, we reveal a grid whilst avoiding the mines that lay hidden beneath.
The reason why I decided to blog about this is because it's not every day that I find a scenario to use recursion, but it is however well suited for this problem.
Recursion is when a method calls itself and is used for solving many computational problems, especially in games. In this article, I am technically not having a method call itself, but a form of recursion is happening.
Please note: When I mention a square, cell or coordinate, just consider them as a square on the grid. Knowing the exact difference here is not required to appreciate what I am doing with recursion :)
Problem
A common problem which may cause a sweat or two (or none) for a developer is the revealing of the mine-free squares.This occurs when you select a square which reveals a region of mine-free squares until it reaches a square that is connected to a mine. This is what the green squares are in the image below:
(Actual in-game footage!!)
I built this game using C#, WPF with Net Framework 4.
Solution
I decided to use the magic that is recursion for solving this problem.The method below is what is invoked when a square has been clicked. The implementation then goes through a process of invoking 'RevealNextCell' for each surrounding square.
1 2 3 4 5 6 7 8 9 10 11 12 | internal void Reveal(int x, int y) { RevealNextCell(x - 1, y - 1); RevealNextCell(x - 1, y); RevealNextCell(x - 1, y + 1); RevealNextCell(x + 1, y); RevealNextCell(x, y - 1); RevealNextCell(x, y + 1); RevealNextCell(x + 1, y - 1); RevealNextCell(x + 1, y + 1); } |
The 'RevealNextCell' begins by fetching the coordinate (square) and checking that it is a valid cell to be processed and if it isn't we return on the method - When using recursion it is important to not forget your return conditions or you may get into trouble.
'IsBlank' checks the mine counter on the coordinate (The counter is set when the mine field is first created) to determine whether it is a mine-free square or not. If so, it sets the colour of the cell (Yes, this shouldn't be hard coded!) and finally invokes 'Reveal' which will got through the process of checking all the surrounding squares of this cell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | private void RevealNextCell(int x, int y) { var coordinate = GetCoordinate(x, y, mappedCoordinates); if (coordinate == null) return; if (coordinate.State == State.Revealed) return; if (IsBlank(coordinate)) { coordinate.State = State.Revealed; coordinates[x, y].Cell.Background = Brushes.LightGreen; Reveal(x, y); } else { coordinate.State = State.Revealed; coordinates[x, y].Cell.Child = new TextBlock() { Text = coordinate.Counter.ToString() }; coordinates[x, y].Cell.Background = Brushes.Goldenrod; } } |
To conclude, recursion is a great technique for solving many game problems and can save you from writing nasty nested loops which are unreadable. Recursion can however be dangerous and you may end up writing infinite recursion - just be sure to write that return condition!
Also, you may be at risk of stack overflow if you use deep recursion so be cautious when using it but in the case of a simple 20 x 20 minesweeper grid; it works like a charm and it keeps your code clean and easy to read.
Happy mining!

No comments:
Post a Comment