大概的思路是这样的:首先1.找到游戏窗口->
2.解析方块的位置、图案->
3.遍历方块,找到可以消去的方块->
4.计算方块在窗口中的位置,模拟鼠标点击。
大家都知道连连看的规则是:如果两个方块的连线小于等于两个直角时,这对方块就可以消去。所以除了两个方块之间用直线连接外,还有另外这两种情况。
1.一个直角
2.两个直角
根据上述情况,就可以归纳出一种统一的算法。即:首先找出图案相同的两个方块,分别向他们的四个方向引出四条线段,线段的另一端终止于边缘,或者其它的方块处。分别判断水平与水平、竖直与竖直方向的线段的横坐标、纵坐标是否有交集。如果没有两个方向都没有交集,则这两个方块不能消去。如果有,再在任一条线段有交集的位置向另一条线段连线,如果连线成功(中间无无方块阻碍或者两线段相连),则两方块可以消去。
至于方块图案的判断,是因为背景的图案颜色非常靠近(现在“角色版”索性为单色背景),分别取最亮处,与最暗处的RGB值。找出R,G,B值的最大、最小值。判断的时候是在方块上取出任意几个点的(程序中为4个点)RGB值,如果这四个点的R,G,B值都在背景色R,G,B的最大、最小值的范围内,则断定这个方格上没有方块。
其中比较重要的两个技巧就是这样。我把主要的代码贴出来:
class
CLLKer
{
public:
CLLKer();
virtual ~CLLKer();
// 设置窗口标题
void SetWndCaption(CString strCaption){ m_strWndCaption = strCaption; }
// 设置方块个数
void SetCellCount(UINT nHorCount, UINT nVerCount) {
m_nHorCellCount = nHorCount;
m_nVerCellCount = nVerCount;
}
// 设置单元格大小
void SetCellSize(UINT nWidth, UINT nHeight) {
m_nCellWidth = nWidth;
m_nCellHeight = nHeight;
}
// 设置边界
void SetMagin(UINT nLeft, UINT nTop) {
m_nMargin_X = nLeft;
m_nMargin_Y = nTop;
}
BOOL LockWnd(); // 锁定窗口
UINT Encode(); // 扫描窗口记录布局信息
BOOL EliminateOnePair();
void EliminateAll();
BOOL CheckWnd();
void Reset();
private:
//
// 功能函数
int CompareTowColor(COLORREF cColor1, COLORREF cColor2); // 返回两颜色的RGB的差值和
int SearchColor(COLORS color); // 取得颜色相同的Cell索引值
void FindXLine(const POS& pos, int& iLeft, int& iRight); // 取得水平方向的路径
void FindYLine(const POS& pos, int& iTop, int& iLow); // 取得竖直方向的路径
BOOL IsXPath(const POS& pos1, const POS& pos2); // 水平方向的路径是否相通
BOOL IsYPath(const POS& pos1, const POS& pos2); // 竖直方向的路径是否相通
BOOL IsPath(const POS& pos1, const POS& pos2); // 判断两单元格是否存在连通路径
BOOL FindMatchingCell(const POS& pos, POS& posMathed); // 找到指定方块的匹配方块
BOOL FindPair(POS& posOri, POS& posMathed); // 找到一对可消除的方块
void ReleaseData();
//
// 中间变量
int **m_pMap; // 单元格布局
CWnd *m_pWnd; // 游戏窗口
vector<CELL> m_vCells; // 单元格信息
//
// 参数
CString m_strWndCaption;
UINT m_nHorCellCount;
UINT m_nVerCellCount;
UINT m_nCellWidth;
UINT m_nCellHeight;
UINT m_nMargin_X;
UINT m_nMargin_Y;
UINT m_nMax_R;
UINT m_nMax_G;
UINT m_nMax_B;
UINT m_nMin_R;
UINT m_nMin_G;
UINT m_nMin_B;
} ;
{
public:
CLLKer();
virtual ~CLLKer();
// 设置窗口标题
void SetWndCaption(CString strCaption){ m_strWndCaption = strCaption; }
// 设置方块个数
void SetCellCount(UINT nHorCount, UINT nVerCount) {
m_nHorCellCount = nHorCount;
m_nVerCellCount = nVerCount;
}
// 设置单元格大小
void SetCellSize(UINT nWidth, UINT nHeight) {
m_nCellWidth = nWidth;
m_nCellHeight = nHeight;
}
// 设置边界
void SetMagin(UINT nLeft, UINT nTop) {
m_nMargin_X = nLeft;
m_nMargin_Y = nTop;
}
BOOL LockWnd(); // 锁定窗口
UINT Encode(); // 扫描窗口记录布局信息
BOOL EliminateOnePair();
void EliminateAll();
BOOL CheckWnd();
void Reset();
private:
//
// 功能函数
int CompareTowColor(COLORREF cColor1, COLORREF cColor2); // 返回两颜色的RGB的差值和
int SearchColor(COLORS color); // 取得颜色相同的Cell索引值
void FindXLine(const POS& pos, int& iLeft, int& iRight); // 取得水平方向的路径
void FindYLine(const POS& pos, int& iTop, int& iLow); // 取得竖直方向的路径
BOOL IsXPath(const POS& pos1, const POS& pos2); // 水平方向的路径是否相通
BOOL IsYPath(const POS& pos1, const POS& pos2); // 竖直方向的路径是否相通
BOOL IsPath(const POS& pos1, const POS& pos2); // 判断两单元格是否存在连通路径
BOOL FindMatchingCell(const POS& pos, POS& posMathed); // 找到指定方块的匹配方块
BOOL FindPair(POS& posOri, POS& posMathed); // 找到一对可消除的方块
void ReleaseData();
//
// 中间变量
int **m_pMap; // 单元格布局
CWnd *m_pWnd; // 游戏窗口
vector<CELL> m_vCells; // 单元格信息
//
// 参数
CString m_strWndCaption;
UINT m_nHorCellCount;
UINT m_nVerCellCount;
UINT m_nCellWidth;
UINT m_nCellHeight;
UINT m_nMargin_X;
UINT m_nMargin_Y;
UINT m_nMax_R;
UINT m_nMax_G;
UINT m_nMax_B;
UINT m_nMin_R;
UINT m_nMin_G;
UINT m_nMin_B;
} ;
class
CAboutDlg :
public
CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
} ;
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange * pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
// {{AFX_MSG_MAP(CAboutDlg)
// No message handlers
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/////
// CLLKDlg dialog
CLLKDlg::CLLKDlg(CWnd * pParent /*=NULL*/ )
: CDialog(CLLKDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLLKDlg)
m_bAutoElmit = FALSE;
m_bIsTopMost = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bStarted = FALSE;
}
void CLLKDlg::DoDataExchange(CDataExchange * pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLLKDlg)
DDX_Check(pDX, IDC_CHK_AUTO, m_bAutoElmit);
DDX_Check(pDX, IDC_CHK_TOPMOST, m_bIsTopMost);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLLKDlg, CDialog)
// {{AFX_MSG_MAP(CLLKDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_LOCK, OnLock)
ON_BN_CLICKED(IDC_ENCODE, OnEncode)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CHK_TOPMOST, OnChkTopmost)
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/////
// CLLKDlg message handlers
BOOL CLLKDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
////
// 取得配置文件路径
char szModulPath[_MAX_PATH] = {0};
GetModuleFileName(NULL, szModulPath, _MAX_PATH);
CString strPath = szModulPath;
int iPos = strPath.ReverseFind('/');
m_strInitPath = strPath.Left(iPos+1);
m_strInitPath += "CONFIG.INI";
////
CRect rcWnd;
GetWindowRect(rcWnd);
int iPos_X = GetPrivateProfileInt("OPTION", "POS_X", 500, m_strInitPath);
int iPos_Y = GetPrivateProfileInt("OPTION", "POS_Y", 700, m_strInitPath);
rcWnd.OffsetRect(iPos_X, iPos_Y);
MoveWindow(rcWnd, FALSE);
BOOL m_bIsTopMost = GetPrivateProfileInt("OPTION", "TOPMOST", 0, m_strInitPath);
CheckDlgButton(IDC_CHK_TOPMOST, m_bIsTopMost);
HWND hWnd = this->GetSafeHwnd();
if ( m_bIsTopMost )
::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
else
::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
SetTimer(1, 100, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLLKDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLLKDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLLKDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CLLKDlg::OnLock()
{
// TODO: Add your control notification handler code here
if ( m_LLKer.LockWnd() )
{
AfxMessageBox("锁定成功", MB_ICONINFORMATION);
m_bStarted = TRUE;
return;
}
AfxMessageBox("未找到游戏窗口,请确认窗口名称是否正确", MB_ICONERROR);
}
void CLLKDlg::OnEncode()
{
// TODO: Add your control notification handler code here
UINT nBlock = m_LLKer.Encode();
if ( nBlock > 0 )
{
AfxMessageBox("解析成功!", MB_ICONINFORMATION);
}
}
void CLLKDlg::OnDelete()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
POINT ptCursor;
int iRes = ::GetCursorPos(&ptCursor);
if ( m_bAutoElmit )
{
m_LLKer.EliminateAll();
::SetCursorPos(ptCursor.x, ptCursor.y);
}
else
{
if ( m_LLKer.EliminateOnePair() )
::SetCursorPos(ptCursor.x, ptCursor.y);
}
}
void CLLKDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if ( !m_LLKer.CheckWnd() && m_bStarted )
{
m_LLKer.Reset();
}
CDialog::OnTimer(nIDEvent);
}
void CLLKDlg::OnChkTopmost()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
HWND hWnd = this->GetSafeHwnd();
if ( m_bIsTopMost )
::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
else
::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
CString strBuf;
strBuf.Format("%d", m_bIsTopMost);
WritePrivateProfileString("OPTION", "TOPMOST", strBuf, m_strInitPath);
}
BOOL CLLKDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
CRect rcWnd;
GetWindowRect(rcWnd);
CString strBuf;
strBuf.Format("%d", rcWnd.left);
WritePrivateProfileString("OPTION", "POS_X", strBuf, m_strInitPath);
strBuf.Format("%d", rcWnd.top);
WritePrivateProfileString("OPTION", "POS_Y", strBuf, m_strInitPath);
return CDialog::DestroyWindow();
}
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
} ;
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange * pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
// {{AFX_MSG_MAP(CAboutDlg)
// No message handlers
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/////
// CLLKDlg dialog
CLLKDlg::CLLKDlg(CWnd * pParent /*=NULL*/ )
: CDialog(CLLKDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLLKDlg)
m_bAutoElmit = FALSE;
m_bIsTopMost = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bStarted = FALSE;
}
void CLLKDlg::DoDataExchange(CDataExchange * pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLLKDlg)
DDX_Check(pDX, IDC_CHK_AUTO, m_bAutoElmit);
DDX_Check(pDX, IDC_CHK_TOPMOST, m_bIsTopMost);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLLKDlg, CDialog)
// {{AFX_MSG_MAP(CLLKDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_LOCK, OnLock)
ON_BN_CLICKED(IDC_ENCODE, OnEncode)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CHK_TOPMOST, OnChkTopmost)
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/////
// CLLKDlg message handlers
BOOL CLLKDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
////
// 取得配置文件路径
char szModulPath[_MAX_PATH] = {0};
GetModuleFileName(NULL, szModulPath, _MAX_PATH);
CString strPath = szModulPath;
int iPos = strPath.ReverseFind('/');
m_strInitPath = strPath.Left(iPos+1);
m_strInitPath += "CONFIG.INI";
////
CRect rcWnd;
GetWindowRect(rcWnd);
int iPos_X = GetPrivateProfileInt("OPTION", "POS_X", 500, m_strInitPath);
int iPos_Y = GetPrivateProfileInt("OPTION", "POS_Y", 700, m_strInitPath);
rcWnd.OffsetRect(iPos_X, iPos_Y);
MoveWindow(rcWnd, FALSE);
BOOL m_bIsTopMost = GetPrivateProfileInt("OPTION", "TOPMOST", 0, m_strInitPath);
CheckDlgButton(IDC_CHK_TOPMOST, m_bIsTopMost);
HWND hWnd = this->GetSafeHwnd();
if ( m_bIsTopMost )
::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
else
::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
SetTimer(1, 100, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLLKDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLLKDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLLKDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CLLKDlg::OnLock()
{
// TODO: Add your control notification handler code here
if ( m_LLKer.LockWnd() )
{
AfxMessageBox("锁定成功", MB_ICONINFORMATION);
m_bStarted = TRUE;
return;
}
AfxMessageBox("未找到游戏窗口,请确认窗口名称是否正确", MB_ICONERROR);
}
void CLLKDlg::OnEncode()
{
// TODO: Add your control notification handler code here
UINT nBlock = m_LLKer.Encode();
if ( nBlock > 0 )
{
AfxMessageBox("解析成功!", MB_ICONINFORMATION);
}
}
void CLLKDlg::OnDelete()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
POINT ptCursor;
int iRes = ::GetCursorPos(&ptCursor);
if ( m_bAutoElmit )
{
m_LLKer.EliminateAll();
::SetCursorPos(ptCursor.x, ptCursor.y);
}
else
{
if ( m_LLKer.EliminateOnePair() )
::SetCursorPos(ptCursor.x, ptCursor.y);
}
}
void CLLKDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if ( !m_LLKer.CheckWnd() && m_bStarted )
{
m_LLKer.Reset();
}
CDialog::OnTimer(nIDEvent);
}
void CLLKDlg::OnChkTopmost()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
HWND hWnd = this->GetSafeHwnd();
if ( m_bIsTopMost )
::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
else
::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
CString strBuf;
strBuf.Format("%d", m_bIsTopMost);
WritePrivateProfileString("OPTION", "TOPMOST", strBuf, m_strInitPath);
}
BOOL CLLKDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
CRect rcWnd;
GetWindowRect(rcWnd);
CString strBuf;
strBuf.Format("%d", rcWnd.left);
WritePrivateProfileString("OPTION", "POS_X", strBuf, m_strInitPath);
strBuf.Format("%d", rcWnd.top);
WritePrivateProfileString("OPTION", "POS_Y", strBuf, m_strInitPath);
return CDialog::DestroyWindow();
}