static void Main(string[] args) { // Get process information for minesweeper game var proc = Process.GetProcessesByName("Winmine__XP")[0]; var rect = new User32.Rect(); User32.GetWindowRect(proc.MainWindowHandle, ref rect); MouseOperations.WindowRect = rect; var os = System.Environment.OSVersion; // Run minesweeper solver logic Solver solver = new Solver(os); Task.Run(() => solver.BeginSolving(rect)); Console.ReadLine(); }
private Board CreateGameBoard(User32.Rect windowRect, int windowWidth, int windowHeight) { Thread.Sleep(30); var bmp = new Bitmap(windowWidth, windowHeight, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(bmp); graphics.CopyFromScreen(windowRect.left, windowRect.top, 0, 0, new Size(windowWidth, windowHeight), CopyPixelOperation.SourceCopy); int boardWidth = (bmp.Width - 10 - GRID_OFFSET_X) / Square.PIXEL_WIDTH; int boardHeight = (bmp.Height - 10 - GRID_OFFSET_Y) / Square.PIXEL_HEIGHT; // Get grid of colors representing the values of each square Color[,] colorGrid = GetColorGrid(bmp, boardWidth, boardHeight); bool isSmiling = true; if (OS.Platform == PlatformID.Win32NT && OS.Version.Major == 6) { isSmiling = bmp.GetPixel((windowWidth / 2) - 2, 75).Equals(Color.FromArgb(255, 0, 0, 0)); } if (OS.Platform == PlatformID.Win32NT && OS.Version.Major == 6 && OS.Version.Minor == 2) { isSmiling = bmp.GetPixel((windowWidth / 2), 78).Equals(Color.FromArgb(255, 0, 0, 0)); } if (OS.Platform == PlatformID.Win32NT && OS.Version.Major == 10) { isSmiling = bmp.GetPixel((windowWidth / 2), 78).Equals(Color.FromArgb(255, 0, 0, 0)); } Board gameBoard = new Board(windowRect, isSmiling); gameBoard.Grid = new Square[boardHeight, boardWidth]; try { gameBoard.CreateGrid(boardWidth, boardHeight, colorGrid); } catch (Exception) { return(null); } return(gameBoard); }
/// <summary> /// Prepares game board and executes calculation logic /// </summary> /// <param name="windowRect"></param> /// <param name="os"></param> public void BeginSolving(User32.Rect windowRect) { // Get height and width properties of process int windowWidth = windowRect.right - windowRect.left; int windowHeight = windowRect.bottom - windowRect.top; while (true) { { Board gameBoard = CreateGameBoard(windowRect, windowWidth, windowHeight); if (gameBoard == null) { continue; } bool gameRestarted = false; if (!gameBoard.IsSmiling) { Thread.Sleep(5000); // Click restart button MouseOperations.PerformLeftClick(windowRect.left + (windowWidth / 2) + 2, windowRect.top + 77); gameRestarted = true; } // If the game has been restarted, make initial click in the corner (statistically safest move) if (gameRestarted) { MouseOperations.PerformLeftClick( gameBoard.WindowRect.left + Solver.GRID_OFFSET_X + (gameBoard.Grid[gameBoard.Grid.GetLength(0) - 1, 0].PosX * Square.PIXEL_WIDTH) + 8, gameBoard.WindowRect.top + Solver.GRID_OFFSET_Y + (gameBoard.Grid[gameBoard.Grid.GetLength(0) - 1, 0].PosY * Square.PIXEL_HEIGHT) + 8); Thread.Sleep(100); } // Calculate optimal move and apply click MoveCalculator moveCalculator = new MoveCalculator(gameBoard); moveCalculator.CalculateMove(); } } }
public Board(User32.Rect windowrect, bool isSmiling) { WindowRect = windowrect; IsSmiling = isSmiling; }