13,197,342 members (56,348 online)
alternative version

Stats

80.3K views
38 bookmarked
Posted 15 Jun 2007

Minesweeper Solver is My Solution to Solving the Expert Level in the Minesweeper Game

, 15 Jun 2007
 Rate this:
I had trouble solving the expert Minesweeper level, so I wrote a program to do it for me.

Introduction

Have you ever played the Minesweeper game? I didn't start playing until a few weeks ago. I found the beginner and intermediate levels not too difficult, but the expert level was a real pain. So I decided to write an app that would solve the Minesweeper game for me.

Background

While playing the Minesweeper game, I found on the expert level I was often making silly mistakes. Usually after finding 40 or 50 mines I would get mentally tired and mark something as a bomb or click somewhere that was a bomb. Anyway, I found it really difficult to get down to the last few bombs. Then when I finally would be able to get there, I would always have to guess. So I figured I would write a program that could solve the Minesweeper game for me and at least it wouldn't make the same mistakes I was making.

The Game

If you have never played the Minesweeper game, here is a quick intro to it. Depending on the level you are on, you have a certain number of squares. The goal is to find all the mines and mark them as mines without clicking on one. When you start the game you have no idea where the mines are, so you randomly left click a few squares. As you click you start to see numbers. The numbers relate to the number of mines that surround the number on the eight touching sides. When you think you know where a mine is you right click to flag it as a mine. You try to find all the mines as quickly as possible. The game is timed so you try to get the best time you can to get on the best times list.

The Problem

Being a human, I make silly mistakes when I get tired or just don't pay close enough attention. I just could not beat the expert level. So instead of playing more and more of the Minesweeper game, I wrote a program that could solve it for me.

The Solution

First I would like to note that I used these two articles to help out with the Windows messaging and screen print part of the program.

First I make sure the Minesweeper program is running. Then I get the handle to the program and force it to the forefront and put it in the upper left hand corner of the screen. This is important so I have an idea of where I need to be sending mouse clicks. Base off the screen width of the Minesweeper game I make an assumption on which level the user is playing at. Next I use the screen print of the Minesweeper game to figure out which boxes I have found. I use the colors found in the boxes to help me figure out which number I have clicked, etc. Next I wrote some code to help me figure out what all the surrounding boxes are to the current box I am looking at. Then I pass all this info into `TheBrain `method. I was surprised to find how simple the brain method ended up being:

```private Boolean TheBrain(SquareType currPos, SquareType top,
SquareType topRight, SquareType right, SquareType bottomRight,
SquareType bottom, SquareType bottomLeft, SquareType left,
SquareType topLeft, int x, int y)
{
Boolean ret = true;
int bombCnt = GetCounts(SquareType.Bomb,top,topRight,right,
bottomRight,bottom,bottomLeft,left,topLeft);
int unKnownsCnt = GetCounts(SquareType.Unknown, top, topRight,
right, bottomRight, bottom, bottomLeft, left, topLeft);
if (unKnownsCnt == 0)
{
TheSquaresPos[x, y].Complete = true;
return ret;
}
int maxBombs = 0;
String errorNumber = String.Empty;
switch (currPos)
{
case SquareType.One :
maxBombs = 1;
errorNumber = "One";
break;
case SquareType.Two :
maxBombs = 2;
errorNumber = "Two";
break;
case SquareType.Three :
maxBombs = 3;
errorNumber = "Three";
break;
case SquareType.Four :
maxBombs = 4;
errorNumber = "Four";
break;
case SquareType.Five :
maxBombs = 5;
errorNumber = "Five";
break;
case SquareType.Six :
maxBombs = 6;
errorNumber = "Six";
break;
case SquareType.Seven :
maxBombs = 7;
errorNumber = "Seven";
break;
} //switch

if (bombCnt == maxBombs)
{
//Any Unknowns should be checked
ProcessUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);
}
else if (maxBombs >= 7 && bombCnt == 7 && unKnownsCnt == (maxBombs - 7))
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);

}
else if (maxBombs >= 6 && bombCnt == 6 && unKnownsCnt == (maxBombs - 6))
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);

}
else if (maxBombs >= 5 && bombCnt == 5 && unKnownsCnt == (maxBombs - 5))
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);

}
else if (maxBombs >= 4 && bombCnt == 4 && unKnownsCnt == (maxBombs - 4))
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);

}
else if (maxBombs >= 3 && bombCnt == 3 && unKnownsCnt == (maxBombs - 3))
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);
}
else if (maxBombs >= 2 && bombCnt == 2 && unKnownsCnt == (maxBombs -2))
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);
}
else if (bombCnt == 1 && unKnownsCnt == (maxBombs - 1))
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);
}
else if (bombCnt == 0 && unKnownsCnt == maxBombs)
{
RightClickUnknowns(top, topRight, right, bottomRight, bottom,
bottomLeft, left, topLeft, x, y);
}
else if (bombCnt > maxBombs)
{
toolStripStatusLabel1.Text = "Error on "+errorNumber+" processing too many bombs!";
ret = false;
}
return ret;
}```

Once the brain method made a decision on what should be done, I would either send right mouse clicks or left mouse clicks. When starting a game or when the program would get stuck, I would randomly pick the next box to click.

The Best Times from Minesweeper Solver

Note these times are legitimate. That is how long it took the program to solve the game. I found some interesting things that really sped up the time that it took the program to solve the game. First it appears the more RAM memory and CPU the Minesweeper solver has, the faster it will run. Second, I stopped reading all the pixels in the squares. I started to read only half of them. Third, I started to scan the board left to right on the first pass and then right to left on the second pass. This switching of the way I was scanning the board also helped with the speed in which the game could be solved.

Some Odds and Ends

I found that some computers and monitors would pick slightly different colors for the same colors. So I ended up putting those into the app.config file so they can be easily changed. I also found that screen resolution could affect the starting position of boxes. So when trying to get this to run on your PC, you might need to tweak the app.config file values a little before it will work. I have a check 1 button that will check on box in the grid. You can use this to figure out if you need to modify the app.config values.

Known Issues

For some reason, the Minesweeper solver program will mark a bomb twice which switches that square to a question mark. When the app randomly picks a square to click it is random, some intelligence could be programmed into it.

Conclusion

I had some time on my hands, although it only took me two days to write the core of this code. Still it was a fun exercise and helped me improve my programming skills. I found that the Minesweeper solver would solve the beginner level almost every time. It would solve the intermediate level some of the time. You guessed it. It had trouble with the expert level as well. It might solve the expert level once in a hundred tries.

History

• 15th June, 2007: Initial post

Share

 Software Developer (Senior) United States
I started my programmer career over 21 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with vb.net when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out asp.net in 2004. I achieved my MCSD.net in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between vb.net and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2015.

On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.

You may also be interested in...

 Pro Pro

 First PrevNext
 Solution not shown Richard Jones21-Sep-07 3:17 Richard Jones 21-Sep-07 3:17
 Re: Solution not shown kubben21-Sep-07 3:20 kubben 21-Sep-07 3:20
 Re: Solution not shown Richard Jones21-Sep-07 3:47 Richard Jones 21-Sep-07 3:47
 Re: Solution not shown kubben21-Sep-07 3:52 kubben 21-Sep-07 3:52
 Re: Solution not shown Richard Jones21-Sep-07 4:32 Richard Jones 21-Sep-07 4:32
 Re: Solution not shown kubben21-Sep-07 11:14 kubben 21-Sep-07 11:14
 No ball with Vista Jared James Sullivan23-Jun-07 14:25 Jared James Sullivan 23-Jun-07 14:25
 Re: No ball with Vista kubben23-Jun-07 16:30 kubben 23-Jun-07 16:30
 Re: No ball with Vista The Dogcow Farmer1-Sep-08 23:48 The Dogcow Farmer 1-Sep-08 23:48
 :-D Urs Enzler18-Jun-07 20:08 Urs Enzler 18-Jun-07 20:08
 Re: :-D kubben19-Jun-07 1:31 kubben 19-Jun-07 1:31
 Check out this one: Reading Mine Location from Memory BugByter16-Jun-07 6:51 BugByter 16-Jun-07 6:51
 Re: Check out this one: Reading Mine Location from Memory kubben16-Jun-07 13:53 kubben 16-Jun-07 13:53
 Two things PIEBALDconsult15-Jun-07 11:07 PIEBALDconsult 15-Jun-07 11:07
 Re: Two things kubben15-Jun-07 11:14 kubben 15-Jun-07 11:14
 You get my 5 - it's Kirk and the Kobiyashi Maru scenario! Seriously, though, letting the program capture the screen of Minesweeper and analyze it as a starting point is very clever.
 WWJD? astanton197815-Jun-07 10:50 astanton1978 15-Jun-07 10:50
 Re: WWJD? kubben15-Jun-07 11:18 kubben 15-Jun-07 11:18
 Re: WWJD? Johnny J.18-Jun-07 21:03 Johnny J. 18-Jun-07 21:03
 Re: WWJD? kubben19-Jun-07 1:34 kubben 19-Jun-07 1:34
 Re: WWJD? Jasmine250121-Jun-07 6:26 Jasmine2501 21-Jun-07 6:26
 Last Visit: 31-Dec-99 18:00     Last Update: 20-Oct-17 17:32 Refresh 12 Next »