How to Win at Minesweeper

Jan 10, 2010 22:59

Again, it's been a while since I've posted.

I've been playing Minesweeper for a while, and I've recently gotten quite good at it. I share my secrets with you in the following document. It looks long and complicated, but it's simpler than it looks (if not, ask me for clarification)!


An Everyday Use of Matrix Algebra

Introduction

What frustrates me about Minesweeper is that so much is left to chance. Even the first move is a gamble: you click on a random square and hope you don't get blown up (though some versions offer the option of an easy start). But that's not nearly as bad as almost clearing the entire board, then running into this at the last minute:



It isn't obvious where the mines are. And if you guess wrong, you get blown up. All that painstaking effort to find the mines, all for naught! (If you don't get blown up, you're still wasting your time playing Minesweeper. But at least you'd be less frustrated!)

What I like to see is stuff that's easy to reason about:



The 1 in the upper left says there's only one nearby mine. And since there's only one unexplored square nearby, we know that square must be a mine. I only have to look at one number at a time to make progress in the game. Piece of cake.



This one is not so clear. There's a 2, but it's next to three squares: two of them must contain mines, but we don't know which two. Below is a 1, but it's next to three squares, and any one of them could be a mine. At the bottom of the board is another 1, and it's next to two squares. No single square tells you anything certain.

Yet it is possible to make progress here:



The top 1 indicates that there is one mine out of squares A, B, and C. The bottom 1 indicates that there is one mine out of B and C. Note that the second condition is more specific: we've narrowed down the location of the mine. Since the mine must be B or C, it cannot be A, and thus A is not a mine! This allows us to clear that square, and that makes it obvious where two of the mines are...

Algebra

So the moral is that you can do better in Minesweeper if you consider multiple squares at once. The only problem is that this can be brain-hurt confusing, especially when more than a couple of squares are involved! The good news is that you can use the power of algebra to systematize such problems:

A + B + C = 1
B + C = 1

A square can contain either 1 mine or 0 mines. So the first equation says that if you look at A, B, and C, there's only going to be one mine. You already know this! It's just a different way of writing it.

Now, here's a cool trick. When solving a set of equations, you can subtract entire equations from each other:

(A + B + C = 1)
- ( B + C = 1)
-----------------
A + 0 + 0 = 0

Don't panic; it's easier than it looks! B - B is 0, so that drops out. Same with C. And 1 - 1 = 0. So we end up with A + 0 + 0 = 0. In other words, A = 0, and A is not a mine.

This might seem like an even more difficult method of reasoning about Minesweeper squares, but it's actually easier when you have a lot of squares to reason about. For example:



In this example, we have four squares (variables) for which to solve, and four tidbits of information (equations). Note that in equation (1), the total number of mines is one: that's because two of the 3 mines have already been discovered.

(1) A + B = 1
(2) A + B + C = 1
(3) B + C + D = 2
(4) C + D = 1

What we want to do is subtract these equations from each other in order to get A = something, B = something, etc... Let's start by making sure only one equation has A in it: subtract (1) from (2):

(1) A + B = 1
(2) C = 0
(3) B + C + D = 2
(4) C + D = 1

Now let's do the same for B. Let's get rid of the B in (1) by subtracting (3) from (1):

(1) A - C - D = -1
(2) C = 0
(3) B + C + D = 2
(4) C + D = 1

Now let's isolate C. Take (2), add it to (1), and subtract it from (3) and (4):

(1) A - D = -1
(2) C = 0
(3) B + D = 2
(4) D = 1

Now, let's take (4) and... well, you get the idea by now.

(1) A = 0
(2) C = 0
(3) B = 1
(4) D = 1

So B and D are the mines. Again, you could have figured that out just by sitting and thinking about it, but that can be difficult. Equations may involve more busywork, but it's more straightforward once you get used to it.

Speaking of busywork, here's a way to cut down on it:

Matrix Algebra

A B C D
1 1 0 0 | 1
1 1 1 0 | 1
0 1 1 1 | 2
0 0 1 1 | 1

This is just a shorthand for writing the equations. Instead of writing A + B + C = 1 (the second equation), we indicate which variables are included (1 1 1 0) and put the sum on the other side of the vertical line. And instead of adding and subtracting equations, we add and subtract rows. Here's the previous example in matrices:

1 1 0 0 | 1
1 1 1 0 | 1
0 1 1 1 | 2
0 0 1 1 | 1

1 1 0 0 | 1
0 0 1 0 | 0 (row 1 subtracted)
0 1 1 1 | 2
0 0 1 1 | 1

1 0 -1 -1 | -1 (row 3 subtracted)
0 0 1 0 | 0
0 1 1 1 | 2
0 0 1 1 | 1

1 0 0 -1 | -1 (row 2 added)
0 0 1 0 | 0
0 1 0 1 | 2 (row 2 subtracted)
0 0 0 1 | 1 (row 2 subtracted)

1 0 0 0 | 0 (row 4 added)
0 0 1 0 | 0
0 1 0 0 | 1 (row 4 subtracted)
0 0 0 1 | 1

You can also rearrange rows, in order to keep things organized. Mathematicians like to see a nice line of 1s down the diagonal.

1 0 0 0 | 0 (A = 0)
0 1 0 0 | 1 (B = 1)
0 0 1 0 | 0 (C = 0)
0 0 0 1 | 1 (D = 1)

When solving a matrix, there are many different paths to a solution. You can solve for the variables in any order you like, and you should be able to get the same result.

One more tip: You can multiply an entire row by a number. This is useful if you have too many negative signs:

1 0 | 0
0 -1 | -1 (multiply by -1)

1 0 | 0
0 1 | 1

Replacing rows

So now you know some very basic matrix algebra, and you know how to use it to solve Minesweeper problems. The hairiest of problems fails to strike fear into your heart!



Easy as pie. We'll just make a matrix out of that!

A B C D E F G H I J K L
1 1 1 0 0 0 0 0 0 0 0 0 | 1
0 1 1 1 0 0 0 0 0 0 0 0 | 2
0 0 1 1 1 0 0 0 0 0 0 0 | 1
0 0 0 1 1 1 0 0 0 0 0 0 | 2
0 0 0 0 1 1 1 0 0 0 0 0 | 1
0 0 0 0 0 1 1 1 1 1 0 0 | 2
0 0 0 0 0 0 0 0 1 1 1 0 | 1
0 0 0 0 0 0 0 0 0 1 1 1 | 2
0 0 0 0 0 0 0 0 0 0 1 1 | 1

Now all we have to do is fiddle with the rows until we get a diagonal of 1s! This is going to be easy. Right. Okay. Um. Uh... this is not supposed to happen:

A B C D E F G H I J K L
1 0 0 0 0 0 -1 0 0 0 0 0 | 0
0 1 0 0 0 0 0 -1 0 0 0 -1 | 0
0 0 1 0 0 0 1 1 0 0 0 1 | 1
0 0 0 1 0 0 -1 0 0 0 0 0 | 1
0 0 0 0 1 0 0 -1 0 0 0 -1 | -1
0 0 0 0 0 1 1 1 0 0 0 1 | 2
0 0 0 0 0 0 0 0 1 0 0 -1 | -1
0 0 0 0 0 0 0 0 0 1 0 0 | 1
0 0 0 0 0 0 0 0 0 0 1 1 | 1

What went wrong? We were able to solve J = 1 (which actually helps a bit), but we'd really like to solve for most or all of them. There's still a bunch of junk in columns G, H, and L that we can't get rid of.

One way to look at it is that our matrix is the wrong shape: it's shorter than it is wide, and so a perfect diagonal of 1s is impossible. In math speak, we have fewer equations than variables, and that means we don't have enough information to solve all the variables. (Side note: it's okay to have too many equations -- some of them will be redundant and can be thrown out as we solve.)

But maybe we aren't stuck yet. Look at the fourth equation: D - G = 1. If this were a regular math problem, there would be infinitely many solutions. However, we know something extra: a square can only contain either 0 or 1 mines. With this extra knowledge, we can see that there is only one solution: D = 1 and G = 0.

We can represent this by replacing that existing row with two rows:

0 0 0 1 0 0 -1 0 0 0 0 0 | 1 (D - G = 1)

becomes

0 0 0 1 0 0 0 0 0 0 0 0 | 1 (D = 1)
0 0 0 0 0 0 1 0 0 0 0 0 | 0 (G = 0)

There are a few general rules for replacing rows, but they're easily figured out. Just look at the following equations, and convince yourself that each has only one solution (assuming only 0s and 1s are allowed):

  • X + Y = 0
  • X + Y + Z = 3
  • -X - Y = -2
  • X - Y = -1
  • X - Y - Z = 1

Anyway, if we replace rows while solving the matrix, we get the following result:

A B C D E F G H I J K L
1 0 0 0 0 0 0 0 0 0 0 0 | 0
0 1 0 0 0 0 0 0 0 0 0 0 | 1
0 0 1 0 0 0 0 0 0 0 0 0 | 0
0 0 0 1 0 0 0 0 0 0 0 0 | 1
0 0 0 0 1 0 0 0 0 0 0 0 | 0
0 0 0 0 0 1 0 0 0 0 0 0 | 1
0 0 0 0 0 0 1 0 0 0 0 0 | 0
0 0 0 0 0 0 0 1 0 0 0 0 | 0
0 0 0 0 0 0 0 0 1 0 0 0 | 0
0 0 0 0 0 0 0 0 0 1 0 0 | 1
0 0 0 0 0 0 0 0 0 0 1 0 | 0
0 0 0 0 0 0 0 0 0 0 0 1 | 1

Of course, replacing rows applies only to our special matrix algebra, where only 1 and 0 are allowed in variables. Don't do any of that in your matrix algebra homework, because you'll likely get the problem wrong, and your TA will wonder what the heck you did to the matrix from problem 23. He did the same thing on problem 21. Is he boldly making up arbitrary rules? Or is he just smoking something?

A Few Last Notes

You're playing a game of Minesweeper. You solve the simple stuff in your head, and you make matrices for the more difficult portions of the board. You're almost done, when suddenly you get to this:



You make a matrix, but already it looks bad:

A B C D E
1 0 1 0 0 | 1
0 0 1 1 1 | 1

On the Minesweeper board, the 3 and the 2 immediately below it both give the same equation: A + C = 1. Thus, we've effectively only got two equations to work with. We can try to solve it...

A B C D E
1 0 0 -1 -1 | 0
0 0 1 1 1 | 1

...but we simply don't have enough to work with. Normally, this means we have to make a guess and hope we don't die. However, we do have one last piece of information: how many mines are left.

Suppose squares A-E are the only unknown squares left on the board, and that one mine is left. Since the matrix represents all that remains of the board, we can say that A + B + C + D + E = 1. Hey, we can add that to our matrix!

A B C D E
1 0 1 0 0 | 1
0 0 1 1 1 | 1
1 1 1 1 1 | 1

It doesn't look terribly impressive yet, but let's solve it and see what happens:

1 0 1 0 0 | 1
0 0 1 1 1 | 1
0 1 0 1 1 | 0 (row 1 subtracted)

Now, that third row looks awfully interesting. Three variables sum to 0! That must mean none of them are mines:

1 0 1 0 0 | 1
0 0 1 1 1 | 1
0 1 0 0 0 | 0 (expanded from old row 3)
0 0 0 1 0 | 0 (expanded from old row 3)
0 0 0 0 1 | 0 (expanded from old row 3)

1 0 0 -1 -1 | 0 (row 2 subtracted)
0 0 1 1 1 | 1
0 1 0 0 0 | 0
0 0 0 1 0 | 0
0 0 0 0 1 | 0

1 0 0 0 -1 | 0 (row 4 added)
0 0 1 0 1 | 1 (row 4 subtracted)
0 1 0 0 0 | 0
0 0 0 1 0 | 0
0 0 0 0 1 | 0

1 0 0 0 0 | 0 (row 5 added)
0 0 1 0 0 | 1 (row 5 subtracted)
0 1 0 0 0 | 0
0 0 0 1 0 | 0
0 0 0 0 1 | 0

A B C D E
1 0 0 0 0 | 0
0 1 0 0 0 | 0
0 0 1 0 0 | 1
0 0 0 1 0 | 0
0 0 0 0 1 | 0

So C is the last mine, and we win the game! We got lucky, though. If there had been two mines remaining instead of one, there would have been multiple possible solutions, and we would have had to guess. Some guesses are less risky than others, but they all have some risk, and figuring probabilities is a tedious nightmare. In short, I hate guessing.

It doesn't even have to depend on the number of mines:



There is one unmarked mine in the above puzzle. There is no way to deduce its location. There's a 50% chance it's in the top square, and a 50% chance it's in the bottom one. Minesweeper is cruel sometimes.

So math and logic have their limits: Minesweeper is still a game of chance. However, with some practice, you can now win at Minesweeper much more of the time. Impress your friends with your newfound skill! Or, optionally, sit around and pretend you're Spock!

One more thing: I actually don't make very many Minesweeper matrices anymore. After about a week of practice, I began to recognize some basic patterns; I would see the answer before I finished writing the matrix. I do most of the reasoning in my head, and I make a matrix only when I get stuck. I make a few mistakes, but I win a lot of games. So if this looks like a lot of work, just give it a try, and it might become easier for you too!

Previous post Next post
Up