简介:自动扫雷工具。
背景:实际上有许多人已经完成了这个东东,在下只是作为学习C#而用了,请勿见笑!
开发平台:VisualStudio.NET 2003
OS: Windows XP SP2
Product Version of WinMine.exe: 5.1.2600.0
自动扫雷工具程序运行效果如下图,可以看到,炸弹的位置及数字和WinMine.exe中完全一样:
实现
程序分两部分,核心部分请看以下文章:
http://www.codeproject.com/csharp/minememoryreader.asp
有了这个东东,我们可以从WinMine.exe(扫雷游戏程序)的内存中读取布雷的信息,然后显示位置。我只是在此基础上实现了自动游戏的功能,实际上“一点技术含量都没有”!以下是我添加的代码。
注意:注释中的 the original code 指的是CodeProject原文的代码
[DllImport("user32.dll", EntryPoint="SendMessageA")]
public static extern int SendMessage (IntPtr hwnd, int wMsg, UInt32 wParam, UInt32 lParam);
protected const int WM_LBUTTONDOWN = 0x201;
protected const int WM_LBUTTONUP = 0x202;
protected const int WM_RBUTTONDOWN = 0x204;
protected const int WM_RBUTTONUP = 0x205;
protected const UInt32 MK_LBUTTON = 1;
protected const UInt32 MK_RBUTTON = 2;
protected UInt32 MAKELONG(UInt32 low, UInt32 high)
{
return (low & 0xFFFF) | ((high & 0xFFFF) << 16);
}
private void buttonAutoPlay_Click(object sender, System.EventArgs e)
{
// NOTE:
// Please modify the original code: processWinMine = myProcesses[0];
if (processWinMine == null)
{
MessageBox.Show("No MineSweeper process found!");
return;
}
const int CELL_WIDTH = 16;
const int CELL_HEIGHT = 16;
const int GRID_LEFT = 12;
const int GRID_TOP = 58;
// NOTE:
// MinesArray is a 2-dimension array which specify whether there is a bomb in the cell
// Please modify the original code: if (iIsMine == 0x8f) then set MinesArray[x,y] as 1; else set MinesArray[x,y] as 0
int iWidth = MinesArray.GetLength(0);
int iHeight = MinesArray.GetLength(1);
int x, y;
for (y=0 ; y < iHeight; y++)
{
for (x=0 ; x<iWidth ; x++)
{
if (MinesArray[x,y] == 1)
{
// there is bomb! right click the mouse to mark the bomb
SendMessage(processWinMine.MainWindowHandle, WM_RBUTTONDOWN, MK_RBUTTON,
MAKELONG(Convert.ToUInt32((GRID_LEFT + CELL_WIDTH * x)), Convert.ToUInt32(GRID_TOP + CELL_HEIGHT * y)));
SendMessage(processWinMine.MainWindowHandle, WM_RBUTTONUP, MK_RBUTTON,
MAKELONG(Convert.ToUInt32((GRID_LEFT + CELL_WIDTH * x)), Convert.ToUInt32(GRID_TOP + CELL_HEIGHT * y)));
}
else
{
// there is no bomb: left click to clear this cell
SendMessage(processWinMine.MainWindowHandle, WM_LBUTTONDOWN, MK_LBUTTON,
MAKELONG(Convert.ToUInt32((GRID_LEFT + CELL_WIDTH * x)), Convert.ToUInt32(GRID_TOP + CELL_HEIGHT * y)));
SendMessage(processWinMine.MainWindowHandle, WM_LBUTTONUP, MK_LBUTTON,
MAKELONG(Convert.ToUInt32((GRID_LEFT + CELL_WIDTH * x)), Convert.ToUInt32(GRID_TOP + CELL_HEIGHT * y)));
}
}
}
}