I'm making a Minesweeper app.

I read that if you click mine on the first turn, Windows Minesweeper moves that mine to the top left corner.

It's simple, but I can't find an easy way to always reveal a '0' cell on the first click.


Don't generate your level until the player clicks.

When they do click, mark the 9 tiles at/around their cursor as "reserved". Then place all your mines, re-rolling your mine placement if it tries to place in a reserved cell.

Now you're guaranteed that the site the player clicked contains 0 mines and touches 0 adjacent mines, and for any reasonable map size the generation will be so fast, the player won't be able to tell you did it just-in-time.

You can extend this further, taking a random walk out from the clicked cell to reserve more cells, if you'd like to ensure the player reveals not just a single 0 cell, but a swath of them. Or you could vary the mine spawn probability with a gradient that decreases near the clicked site.

Here's an alternative that's a bit cheeky:

Generate your map in advance, and flood-fill the 0 cells, making islands of connected zeros (this is a connected component search). Keep track of the largest such island(s) you find.

When the player clicks, barrel-shift your map data (eg. take a column off the left edge and move it to the right edge, take a row off the top edge and add it to the bottom edge), so that one of the large islands you found moves to include the clicked point. Then re-run your adjacency counter to update the cell numbers.

You'll need to double-check that the move successfully connected the click site to a large island, since it's possible that in the process of the shift, you changed the connectivity of the island (eg. an important connecting tile got pushed off the edge and wrapped around to the other side, breaking the island into smaller pieces) - but you should be able to try a few candidate moves and keep the best one to avoid this pathological case.

Now you ensure the player gets not just one 0, but a nice opening cascade to work with. And it happens in a reasonably organic way. Rather than reserving a "bald spot", you just take advantage of whatever bald spots occurred naturally in the course of the unbiased generation process. This might lead to fewer visible/statistical artifacts that players can exploit beyond the first click.

Note that if your generator routine includes fix-ups for problem cases (like de-clustering mines that would be impossible to deduce), you should run those fix-ups after the move, since the relevant adjacency information can change with the wrap-around.

A risk to consider here: part of what makes a big opening cascade feel so satisfying is that it doesn't always happen. Sometimes you get only a tiny peek into the map, and you have to work carefully to gradually expand it a few cells at a time. So when you do get the big reveal, it's exciting, and you feel like you got lucky and are about to have a great round.

So, weigh whether always giving the player a big reveal from the first click is desirable. It might take some of the emotional lows out of the game start, but it can also come at the cost of some of the highs, and that contrast between runs can be the seed for memorable player anecdotes.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.