SoFunction
Updated on 2025-03-07

C# realizes automatic mine sweeping

I accidentally saw a small desktop program written in Python that can automatically play minesweeping games. I thought it was quite interesting and decided to use C# to make one. [The real situation is: I know that Python is quite popular recently and is very suitable for crawling, big data, machine learning, etc., but now even desktop programs are made in Python? Will you still give it to .NET programmers or not? It's simply unbearable! 】

During the Spring Festival, I happened to have some time to do it. I first downloaded a third-party minesweeping game. After I realized the function, I felt that the resolution of the minesweeping game I downloaded was too low and not good-looking, so I made a minesweeping game myself and made a set of them.

Source code download address:/seabluescn/AutoMineSweeper

It should be noted in advance that these two programs are independent and have no interface or contact. The automatic mine-sweeping program obtains the game status by reading screen information and simulating mouse operations to play. Let me share with you a few related technical points below.

1. Get the application window

[DllImport("")]
    private static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);

    private Rect GetWindowRect()
    {
      Process[] processes = ();
      Process process = null;
      for (int i = 0; i <  - 1; i++)
      {
        process = processes[i];
        if ( == "MineSweeper")
        {
          break;
        }        
      }     

      Rect rect = new Rect();
      GetWindowRect(, out rect);

      return rect;
    }

2. Screenshot

Rect rect = GetWindowRect();

      int left = ;
      int top = ; 

      int centerleft = 21;  //Offset      int centertop = 93;
      int centerwidth = 300;
      int centerheight = 300;
      
      Bitmap bitmapCenter = new Bitmap(centerwidth, centerheight);
      using (Graphics graphics = (bitmapCenter))
      {
        (left + centerleft, top + centertop, 0, 0, new Size(centerwidth, centerheight));
        this.?.Dispose();
        this. = bitmapCenter;
      }

After taking a screenshot, the state of the position is judged based on the color information of the fixed position on the picture, and finally an array is formed.

3. Simulate mouse click

[DllImport("user32")]
    private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    const int MOUSEEVENTF_MOVE = 0x0001; //Move the mouse    const int MOUSEEVENTF_LEFTDOWN = 0x0002; //Simulate the left mouse button to press    const int MOUSEEVENTF_LEFTUP = 0x0004; //Simulate the left mouse button to lift up    const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //Press the simulated right mouse button    const int MOUSEEVENTF_RIGHTUP = 0x0010; //Simulate the right mouse button to lift    const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //Simulate the middle mouse button    const int MOUSEEVENTF_MIDDLEUP = 0x0040; //Simulate the middle mouse button lift    const int MOUSEEVENTF_ABSOLUTE = 0x8000; //Display whether the absolute coordinates are used
        int clickPointX = X * 65535 / ;
        int clickPointY = Y * 65535 / ;

        //Move the mouse        mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, clickPointX, clickPointY, 0, 0);

        //Left click        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        
        //Right click        mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);

4. Game Algorithm

After obtaining the game state, you need to determine the next operation, whether to click on a certain position or right-click to mark a certain position. The algorithm loops through all blocks, in a total of three steps:

1) Basic algorithm

Basic algorithm 1: For the opened block, the center number is the same as the marked number of thunders around it. All unknown positions around it are not thunders. Click on the left button.

Basic algorithm 2: For blocks that have been opened, the center number = number of unknown positions + number of thunders marked around: All unknown positions around it are lightning, right-click to mark

2) Higher-level algorithm

First calculate the sum of the number of thunders that have been opened and the surrounding unknown blocks contain.

Algorithm 1: For the already opened block, if there are more than 2 surrounding unknown blocks, one of which is unknown block: Center number - Lei == Other location block combination Lei count sum: This unknown block must not be Lei

Algorithm 2: For the block that has been opened, if there are more than 2 unknown blocks around, one of which is unknown block: Number-thunder-other position block combination number=1: The unknown block must be thunder

3) I really haven't found a suitable point, so I can only click it at random

For all unknown points, calculate the probability of surrounding lightning and select the point with the least probability to open.

After testing, the program's recognition rate of the target state is 100%, and its intelligence is not bad, better than that of ordinary people. You can watch it for a day when you are bored.

The above is the detailed content of C#’s automatic mine clearance. For more information about C#’s mine clearance, please pay attention to my other related articles!