Jason Thomas

I like to make stuff

April 20, 2023 @ 00:15

Using propositional logic to play Minesweeper

Recently I realised that me studying maths has meant I am no longer terrible at the logic game, Minesweeper. This post talks about basic use of propositional logic. This is useful when playing Minesweeper. Mostly, this is a loose application of Proof by Contradiction and Proof by Exhaustion.

When I was a kid playing Minesweeper on a Windows 2000, I would have liked to have known about propositional logic.

Examples of logic in a recent Minesweeper game

Consider this start in a game of Minesweeper below. I went ahead and marked all of the trivial mines with flags and I hope you can see I required no guesswork here. If a 3 has three empty squares that it touches then according to the rules of Minesweeper all of those empty squares must have mines. But then the opportunity to use this simple logic is no more.

first state

What we can do next is propose something that contradicts itself.

There's a 3 in this picture that touches one mine already so there must be two other mines. The possible places are A, B, C.

first state

Suppose there are two mines in each of A, B. This is impossible because then there would be a 2 with three mines touching. So that means there can be at most one mine in either A or B. But then that means that C must have a mine in it. It also means that since 2 is already touching two mines, with one in A or B, then nothing else can touch 2.

So we get this:

first state

Take a look at the 5,3 and the top right

first state

Figuring this out will take a bit more work.

Consider the 5 by itself.

There are four cases, with mines in:

In all of the three possible cases, 1 touches its required mine in either D, E or F. That means any other square that touches 1 has no mine. So the natural thing is to click the square directly right of 1.

And so we end up with this

first state

It seems to be very rare that we actually need to make guesses in Minesweeper.

log in