连连看外挂1.0

        一时兴起,决定写个连连看的外挂玩玩...于是断断续续经过几个晚上的努力基本呈现雏形...之前也看过一些外挂技术的文章,确实这里面的技术深不可测,第一次写就搞个简单的吧,以后再慢慢改进。

        最简单的外挂莫过于器械式的,也就是通过界面分析,然后去模拟一些鼠标或者键盘动作。连连看外挂就可以通过这种方式去写。整个外挂基本分为以下几步完成:

1. 获取界面信息,当然最主要的是方格信息,有了这个其实足够写一个连连看外挂了,但是为了功能更强大,我另外获取了其他一些界面元素,比如其他玩家的速度(剩下的方格速),开始按钮的位置等等。

2. 连连看算法的实现,其实这个是比较简单的。基本可以分为两种:两条X连线+一条Y连线、两条Y连线+一条X连线。当然也包括了各种特殊情况。比如一条X一条Y等等。

3. 鼠标事件的模拟。

1. 获取界面信息

        这部分工作其实是最繁琐的,只能依靠不断的调式去完成,我在这个过程使用到了系统钩子,在一定程度上简化了这个过程。首先要获得的是每个方格的信息,QQ连连看中一共有11×19个方格数,一共有44(好像45)种不同的图案,这个基本可以靠肉眼获得。

        接下来的事情就需要代码去完成了,主要包括起始点,每个方格的长和宽,可以唯一区分每种图案的N个像素点。首先需要获得一个大概的数值,我写了一个系统的鼠标钩子,主要完成的功能是通过鼠标在QQ连连看窗口的点击,获得该点的client端坐标和颜色值。基本代码如下:

  1. // MouseHook.cpp : Defines the exported functions for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #pragma data_seg ("shareddata")
  5. HHOOK         g_MouseHook = NULL;
  6. HINSTANCE     g_Instance  = NULL;
  7. HWND          g_GameWnd   = NULL;
  8. #pragma data_seg()
  9. #pragma comment(linker, "/SECTION:shareddata,RWS")
  10. int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  11. {
  12.     UNREFERENCED_PARAMETER(lpReserved);
  13.     switch (dwReason)
  14.     {
  15.     case DLL_PROCESS_ATTACH:
  16.     case DLL_THREAD_ATTACH:
  17.         g_Instance = hInstance;
  18.         break;
  19.     case DLL_THREAD_DETACH:
  20.     case DLL_PROCESS_DETACH:
  21.         break;
  22.     }
  23.     return TRUE;
  24. }
  25. LRESULT WINAPI MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
  26. {
  27.     LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
  28.     if(0 < nCode) {
  29.         if(WM_RBUTTONDOWN == wParam) {
  30.             if (NULL == g_GameWnd)
  31.             {
  32.                 MessageBoxA(NULL, "Game window not found!""Error", MB_OK);
  33.                 return CallNextHookEx( g_MouseHook, nCode, wParam, lParam );
  34.             }
  35.             int xPos = pMouseHook->pt.x;
  36.             int yPos = pMouseHook->pt.y;
  37.             CPoint point(xPos, yPos);
  38.             CWindowDC dc(NULL);
  39.             COLORREF cl = dc.GetPixel(point.x, point.y);
  40.             ::ScreenToClient(g_GameWnd, &point);
  41.             CString strMsg;
  42.             strMsg.Format("x = %d, y = %d, color = %d", point.x, point.y, cl);
  43.             MessageBoxA(NULL, strMsg, "Point Position & Color", MB_OK);
  44.         }
  45.     }
  46.     LRESULT RetVal = CallNextHookEx( g_MouseHook, nCode, wParam, lParam ); 
  47.     return RetVal;
  48. }
  49. BOOL InstallHook(HWND hWnd)
  50. {
  51.     if (NULL == hWnd) {
  52.         MessageBoxA(NULL, "Game window not found!""Error", MB_OK);
  53.         return FALSE;
  54.     }
  55.     g_GameWnd = hWnd;
  56.     if(NULL != g_MouseHook) {
  57.         MessageBoxA(NULL, "Hook already set!""Error", MB_OK);
  58.         return FALSE;
  59.     }
  60.     g_MouseHook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc, g_Instance, 0);
  61.     if(NULL == g_MouseHook) {
  62.         MessageBoxA(NULL, "Hook can not be set!""Error", MB_OK);
  63.         return FALSE;
  64.     }
  65.     return TRUE;
  66. }
  67. BOOL UnInstallHook()
  68. {
  69.     if(NULL == g_MouseHook) {
  70.         MessageBoxA(NULL, "Hook not installed correct!""Error", MB_OK);
  71.         return FALSE;
  72.     } else {
  73.         if(UnhookWindowsHookEx(g_MouseHook))
  74.             return TRUE;
  75.         else {
  76.             MessageBoxA(NULL, "Hook can not be removed!""Error", MB_OK);
  77.             return FALSE;
  78.         }
  79.     }
  80. }

        另外在def文件中需要将InstallHook跟UnInstallHook导出。另外注意到在InstallHook中需要一个参数HWND,这个参数需要在调用这个DLL的客户端代码中获得。方法自然是用EnumWindows函数去枚举桌面上的所有窗口然后找到连连看窗口。

        通过这个方式,然后加上一些简单的调式,或者如下四个数据:

  1. const int  gc_CellStartX     =  12;   // 方块窗口起始点-X
  2. const int  gc_CellStartY     =  180;  // 方块窗口起始点-Y
  3. const int  gc_CellWidth      =  31;   // 每个方块的宽度
  4. const int  gc_CellLength     =  35;   // 每个方块的长度

        接下的需要做的也是最重要的就是在每个方格上取N个点来唯一的表示一种图案。那么N应该是多少呢?自然是越少越好,程序才能跑的快。之前我也是随机的在方块上取4-5个点,但是仔细一算一共209个小方块,那需要调用1000+次GetPixel,这大大影响了程序的运行速度。如果能用一个唯一的点的颜色会表示一个图案自然最好,但是这个似乎找起来有难度。不管三七二十一,我先取了中心点再说,看看究竟能区分多少方块。为了这个我再次使用到了钩子,这次的钩子我需要完成这件事情:在QQ连连看窗口上用鼠标右击某个图案,然后可以获得这个图案上特定点的位置,跟前一个钩子不同的主要是MouseProc函数:

  1. LRESULT WINAPI MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
  2. {
  3.     LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
  4.     if(0 < nCode) {
  5.         if(WM_RBUTTONDOWN == wParam) {
  6.             if (NULL == g_GameWnd)
  7.             {
  8.                 MessageBoxA(NULL, "Game window not found!""Error", MB_OK);
  9.                 return CallNextHookEx( g_MouseHook, nCode, wParam, lParam );
  10.             }
  11.             int xPos = pMouseHook->pt.x;
  12.             int yPos = pMouseHook->pt.y;
  13.             CPoint point(xPos, yPos);
  14.             ::ScreenToClient(g_GameWnd, &point);
  15.             int colIndex = (point.x - gc_CellStartX)/gc_CellWidth;
  16.             int rowIndex = (point.y - gc_CellStartY)/gc_CellLength;
  17.             ASSERT(rowIndex >= 0 && rowIndex < 11);
  18.             ASSERT(colIndex >= 0 && colIndex < 19);
  19.             
  20.             int startx = gc_CellStartX + colIndex * gc_CellWidth;
  21.             int starty = gc_CellStartY + rowIndex * gc_CellLength;
  22.             CPoint pointCT(startx + gc_CellWidth / 2, starty + gc_CellLength / 2);   // center point of the cell
  23.             ::ClientToScreen(g_GameWnd, &pointCT);
  24.             CPoint pointUP(pointCT.x, pointCT.y - 10);
  25.             CWindowDC dc(NULL);
  26.             COLORREF colorUP = dc.GetPixel(pointUP);
  27.             COLORREF colorCT = dc.GetPixel(pointCT);
  28.             CString strMsg;
  29.             strMsg.Format("Row: %d, Column: %d/ncolorUP: %d/nncolorCT: %d/n", rowIndex, colIndex, colorUP, colorCT);
  30.             MessageBoxA(NULL, strMsg, "Cell Color", MB_OK);
  31.         }
  32.     }
  33.     LRESULT RetVal = CallNextHookEx( g_MouseHook, nCode, wParam, lParam ); 
  34.     return RetVal;
  35. }

        当然还要加上之前获得的4个常量才能通过编译。不难看出在这个MouseProc中我们使用了两个点唯一的区分一个图案,一个中心点一个中心点偏上10个像素。但是其实这并不是一开始就获得的结果,起初我就取了一个中心点,然后通过不断的测试,找到QQ连连看中每一种图案中心点的颜色值,结果发现中心点可以区分出44种不同图案中的39种,另外5种图案中共有两种颜色值。如下图所示,红颜色的两个图案中心点像素值相同,蓝颜色的三个图案中心点像素也相同。

        

        有了这个结果基本可以断定两个点可以唯一的区分一个图案,我随意的取了中心点上方的10个像素的点作为第二个点,经过上述钩子程序的测试该点可以区分该5个点。为了尽量的加快程序速度,我并不是简单的每次选取所有图案的两个点,而是当必要的时候,也就是碰到中心点不能区分的时候再次去读取第二个点的像素值。为此,我使用了一个CellColor类唯一代表一个图案,提供了一点基本的操作:

  1. // class CellColor
  2. class CellColor{
  3. public:
  4.     // Following static variables are used to make our application quicker...only for QQGame...
  5.     static const COLORREF cBackGroundColor = 7359536;
  6.     static const COLORREF cDuplicateColor1   = 16317688;
  7.     static const COLORREF cDuplicateColor2   = 40184;
  8. public:
  9.     CellColor(){ ;}
  10.     CellColor(COLORREF color1, COLORREF color2)
  11.         : m_color1(color1), m_color2(color2)
  12.     {
  13.         // 
  14.     }
  15.     CellColor::CellColor(const CellColor &color)
  16.     {
  17.         m_color1 = color.m_color1;
  18.         m_color2 = color.m_color2;
  19.     }
  20.     bool isBackGround()
  21.     {
  22.         return cBackGroundColor == m_color1;
  23.     }
  24.     bool equal(CellColor& color)
  25.     {
  26.         return m_color1 == color.m_color1 && m_color2 == color.m_color2;
  27.     }
  28. private:
  29.     COLORREF m_color1;
  30.     COLORREF m_color2;
  31. };

        下面的函数用来从QQ连连看窗口中获取一个图案的CellColor:

  1. //
  2. // Get each cell color. We choose two points for each 
  3. // cell, because two points is enough to distinguish 
  4. // all these patterns
  5. // 
  6. CellColor GetEachCellColor(int startx, int starty)
  7. {
  8.     // Two points is enough to distinguish all these patterns.
  9.     CPoint point1(startx + gc_CellWidth / 2, starty + gc_CellLength / 2);
  10.     CPoint point2(point1.x, point1.y - 10);
  11.     COLORREF color1 = 0, color2 = 0;
  12.     color1 =::GetPixel(g_GameDC, point1.x, point1.y);
  13.     // Use following if statement to make it faster. Because GetPixel takes time.
  14.     if(CellColor::cDuplicateColor1 == color1 || CellColor::cDuplicateColor2 == color1)
  15.          color2 = ::GetPixel(g_GameDC, point2.x, point2.y);
  16.     return CellColor(color1, color2);
  17. }

        到目前为止,最需要的东西我们已经得到。已经足够写出一个最基本功能的连连看外挂。但是在完成最基本功能的同时我又突发奇想希望能完成这么一个功能:我的外挂可以根据其他对手的速率动态的调整自己的速度。有了这个想法,我需要获得更多的界面信息,当然主要是其他玩家分数(剩余方格数量),但是这可不是一个简单的活,虽然游戏窗口上明明白白写着一个100,但是我怎么才能获得这个数值呢?首先,我还是通过系统钩子的方法再加上严格的测试得到了每个玩家窗口中的包含分数的小矩形,通过截屏的方法我获得了玩家分数中从0到9的所有数值的特征点,放大后如下所示:

       

我所需要做的就是找到那么几个点唯一的确定这些数字,理论上可能4,5个就可以做到,然后一个人找啊找,结果发现这还真不是一件容易的活,反正vs在手,这还不是小菜一碟...

  1. #include <iostream>
  2. using namespace std;
  3. bool Numbers[11][8][5];
  4. void fill(int Num, int RowIndex, int ColIndex, int HorizontalNum = 1, int VerticalNum = 1)
  5. {
  6.     if(Num > 10 || RowIndex + VerticalNum > 8 || ColIndex + HorizontalNum > 5) {
  7.         cout<<"Wrong Number!"<<endl;
  8.         return;
  9.     }
  10.     for(int i = 0; i < HorizontalNum; i++) {
  11.         Numbers[Num][RowIndex][ColIndex + i] = true;
  12.     }
  13.     for(int i = 0; i < VerticalNum; i++) {
  14.         Numbers[Num][RowIndex + i][ColIndex] = true;
  15.     }
  16. }
  17. int main()
  18. {
  19.     memset(Numbers, 0, 11 * 5 * 8);
  20.     // 0
  21.     fill(0, 0, 1, 3);
  22.     fill(0, 1, 0, 1, 6);
  23.     fill(0, 1, 4, 1, 6);
  24.     fill(0, 7, 1, 3);
  25.     // 1
  26.     fill(1, 1, 1);
  27.     fill(1, 0, 2, 1, 8);
  28.     fill(1, 7, 1, 3);
  29.     // 2
  30.     fill(2, 0, 1, 3);
  31.     fill(2, 1, 0, 1, 2);
  32.     fill(2, 1, 4, 1, 2);
  33.     fill(2, 3, 3);
  34.     fill(2, 4, 2);
  35.     fill(2, 5, 1);
  36.     fill(2, 6, 0);
  37.     fill(2, 7, 0, 5);
  38.     // 3
  39.     fill(3, 0, 1, 3);
  40.     fill(3, 1, 0);
  41.     fill(3, 1, 4, 1, 2);
  42.     fill(3, 3, 2, 2);
  43.     fill(3, 4, 4, 1, 3);
  44.     fill(3, 6, 0);
  45.     fill(3, 7, 1, 3);
  46.     // 4
  47.     fill(4, 0, 3, 1, 8);
  48.     fill(4, 1, 2, 2);
  49.     fill(4, 2, 1, 1, 2);
  50.     fill(4, 4, 0);
  51.     fill(4, 5, 1, 4);
  52.     fill(4, 7, 3, 2);
  53.     // 5
  54.     fill(5, 0, 0, 5, 4);
  55.     fill(5, 3, 0, 4);
  56.     fill(5, 4, 4, 1, 3);
  57.     fill(5, 6, 0);
  58.     fill(5, 7, 1, 3);
  59.     // 6
  60.     fill(6, 0, 1,3);
  61.     fill(6, 1, 0, 1, 6);
  62.     fill(6, 1, 3);
  63.     fill(6, 3, 0, 4);
  64.     fill(6, 4, 4, 1, 3);
  65.     fill(6, 7, 1, 3);
  66.     // 7
  67.     fill(7, 0, 0, 5, 2);
  68.     fill(7, 0, 3, 1, 3);
  69.     fill(7, 3, 2, 1, 5);
  70.     // 8
  71.     fill(8, 0, 1, 3);
  72.     fill(8, 1, 0, 1, 2);
  73.     fill(8, 1, 4, 1, 2);
  74.     fill(8, 3, 1, 3);
  75.     fill(8, 4, 0, 1, 3);
  76.     fill(8, 4, 4, 1, 3);
  77.     fill(8, 7, 1, 3);
  78.     // 9
  79.     fill(9, 0, 1, 3);
  80.     fill(9, 1, 0, 1, 3);
  81.     fill(9, 1, 4, 1, 6);
  82.     fill(9, 4, 1, 4);
  83.     fill(9, 6, 1);
  84.     fill(9, 7, 1, 3);
  85.     for(int m = 0; m < 11; m++){
  86.         for(int i = 0; i < 8; i++) {
  87.             for(int j = 0; j < 5; j++)
  88.             {
  89.                 if(Numbers[m][i][j])
  90.                     cout<<"O";
  91.                 else
  92.                     cout<<" ";
  93.             }
  94.             cout<<endl;
  95.         }
  96.         cout<<endl<<endl;
  97.     }
  98.     for(int i1 = 0; i1 < 8; i1++)
  99.     for(int j1 = 0; j1 < 5; j1++) {
  100.         int i2 = (j1 == 4? i1 + 1 : i1);
  101.         int j2 = (j1 == 4? 0 : j1 + 1);
  102.         for(; i2 < 8; i2++)
  103.         for(; j2 < 5; j2++) {
  104.             int i3 = (j2 == 4? i2 + 1 : i2);
  105.             int j3 = (j2 == 4? 0 : j2 + 1);
  106.             for(; i3 < 8; i3++)
  107.             for(; j3 < 5; j3++) {
  108.                 int i4 = (j3 == 4? i3 + 1 : i3);
  109.                 int j4 = (j3 == 4? 0 : j3 + 1);
  110.                 for(; i4 < 8; i4++)
  111.                 for(; j4 < 5; j4++) {
  112.                     int i5 = (j4 == 4? i4 + 1 : i4);
  113.                     int j5 = (j4 == 4? 0 : j4 + 1);
  114.                     for(; i5 < 8; i5++)
  115.                     for(; j5 < 5; j5++) {
  116.                         int i6 = (j5 == 4? i5 + 1 : i5);
  117.                         int j6 = (j5 == 4? 0 : j5 + 1);
  118.                         for(; i6 < 8; i6++)
  119.                         for(; j6 < 5; j6++) {
  120.                             int value[11] = {0};
  121.                             for(int index = 0; index < 11; index++) {
  122.                                 int r1 = Numbers[index][i1][j1] << 5;
  123.                                 int r2 = Numbers[index][i2][j2] << 4;
  124.                                 int r3 = Numbers[index][i3][j3] << 3;
  125.                                 int r4 = Numbers[index][i4][j4] << 2;
  126.                                 int r5 = Numbers[index][i5][j5] << 1;
  127.                                 int r6 = Numbers[index][i6][j6] << 0;
  128.                                 value[index] = r1 + r2 + r3 + r4 + r5 + r6;
  129.                             }
  130.                             bool bSucess = true;
  131.                             for(int i = 0; i < 11; i++)
  132.                             for(int j = i + 1; j < 11; j++) {
  133.                                 if(value[i] == value[j]) {
  134.                                     bSucess = false;
  135.                                     goto next;
  136.                                 }
  137.                             }
  138. next: ;
  139.                             if(bSucess) {
  140.                                 cout << "x1 = " << j1 << " y1 = " << i1 <<" x2 = "<<j2 <<" y2 = "<<i2 <<" x3 = "<<j3 <<" y3 = "
  141.                                      << i3 << " x4 = "<< j4 << " y4 = "<< i4 <<" x5 = "<< j5<<" y5 = "<<i5<<" x6 = "<<j6<<" y6= "<<i6<<endl;
  142.                                 for(int i = 0; i < 11; i++) {
  143.                                     printf("%d: %d/n", i, value[i]);
  144.                                 }
  145.                                 return 0;
  146.                             }
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153.     cout<<"Can not find six points to distinguish these numbers!"<<endl;
  154.     return 0;
  155. }

        写了程序才知道至少需要六个点才能区分这些个数字,怪不得我找了半年没找到,还好即使调转方向。除了这些信息之外,我还获得了开始按钮坐标等信息(实现自动开始,保障程序的连续运行)。OK,万事俱备了,来看看我们的收获吧:

  1. /
  2. // These const globe variables will help us to get all players' information
  3. // during a game. Of cause the main thing we care about is the score.
  4. // Here score means the number of left cells.
  5. const COLORREF gc_OtherPlayersBKGround  = 11034624;  // 是否有玩家
  6. const COLORREF gc_ScoreColor                        = 63728;        // 分数颜色
  7. const int      gc_FirstScoreRectStartX                   = 101;            // 第一个玩家分数起始点 - X坐标
  8. const int      gc_FirstScoreRectStartY                   = 44;              // Y坐标,同上
  9. const int      gc_SpaceBetweenTwoScoreRects    = 119;             // 两个玩家的分数间距
  10. const int      gc_EachNumberRectWidth               = 7;                // 每个数字的宽度
  11. const int      gc_EachNumberRectHeight              = 10;              // 每个数字的高度
  12. const int      gc_MyScoreRectStartX                    = 505;            // 自己的分数起始点 - X坐标
  13. const int      gc_MyScoreRectStartY                    = 576;            // Y坐标,同上
  14. /
  15. /
  16. // These globe variables are useful during the game. gc_StartFlagPoint 
  17. // and gc_StartFlagColor are used to check wether the game has been started.
  18. // gc_StartButtonPoint is used to simulate the mouse click to start a game.
  19. // gc_SignificantPointsForSocre is used to get players' score.
  20. const CPoint              gc_StartFlagPoint(311, 576);          // 
  21. const COLORREF       gc_StartFlagColor = 10534136;      // 结合上一个表示游戏是否已开始
  22. const CPoint              gc_StartButtonPoint(670, 570);     // 开始按钮的坐标点
  23. const CPoint              gc_SignificantPointsForSocre[6] = {CPoint(0,1),CPoint(4,1),CPoint(4,2),CPoint(4,3),CPoint(0,4),CPoint(2,4)};                                                                                     // 区分0-9的六个特征点
  24. /

        到此为止第一步基本完成。

 

2. 连连看算法的实现

        这个部分分为两部分,第一部分是遍历整个数组找到两个相同的图案,第二部分检查这两个图案是否连通。在介绍这部分工作之前需要简单介绍一下表示每个方块的数据结构及这些数据结构的填充:

  1. /
  2. // All player information during each game.
  3. struct Player{
  4.     bool exist;     // 是否有玩家
  5.     int  leftcells; // 玩家剩余方格数
  6. } g_AllPlayers[5] = {(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)};
  7. /
  8. /
  9. // All cell information during each game.
  10. struct Cell{
  11.     Cell(){ empty = true; }
  12.     CellColor   color;    // 方块颜色,唯一确定一种图案
  13.     CPoint      postion;  // 方块位置, 模拟鼠标点击
  14.     bool        empty;    // 是否存在一个未消掉的方块
  15. } g_AllCells[gc_RowNum][gc_ColNum];
  16. /
  17. /
  18. UINT    g_CellNum    = 0;      // My lefted cell number - used inside
  19. .....
  20. .....
  21. .....
  22. // Get all the cell information for the game. This must
  23. // be done after the game starts. We get all necessary
  24. // information here. The most important is to fill the
  25. // g_AllCells array.
  26. // 
  27. void GetAllCellInfo()
  28. {
  29.     // set new values for them
  30.     for(int i = 0; i < gc_RowNum; i++)
  31.     for(int j = 0; j < gc_ColNum; j++)
  32.     {
  33.         int x = gc_CellStartX + j * gc_CellWidth;
  34.         int y = gc_CellStartY + i * gc_CellLength;
  35.         g_AllCells[i][j].color   = GetEachCellColor(x, y);
  36.         g_AllCells[i][j].postion = CPoint(x + gc_CellWidth / 2, y + gc_CellLength / 2);
  37.     }
  38.     for(int i = 0; i < gc_RowNum; i++)
  39.     for(int j = 0; j < gc_ColNum; j++) {
  40.         if(!g_AllCells[i][j].color.isBackGround()) {
  41.             g_AllCells[i][j].empty = false;
  42.             g_CellNum++;
  43.         }
  44.     }
  45. }

        不难看出,g_AllCells保存了所有方格信息,而g_CellNum保存了自己的剩余方格数。接下来就可以遍历g_AllCells这个数组找到图案相同的方块并检查是否连通。

第一部分代码如下:

  1.         while(g_CellNum) {
  2.             for(int i = 0; i < gc_RowNum; i++)
  3.             for(int j = 0; j < gc_ColNum; j++) {
  4.                 if (!g_AllCells[i][j].empty) {
  5.                     for(int m = 0; m < gc_RowNum; m++)
  6.                     for(int n = 0; n < gc_ColNum; n++) {
  7.                         if(!g_AllCells[m][n].empty && 
  8.                             g_AllCells[i][j].color.equal(g_AllCells[m][n].color)) {
  9.                             if(!(i == m && j == n) && CanBeDeleted(i, j, m, n)) {
  10.                                 SimulateMouseClick(g_AllCells[i][j].postion, g_AllCells[m][n].postion);
  11.                                 g_AllCells[i][j].empty = true;
  12.                                 g_AllCells[m][n].empty = true;
  13.                                 g_CellNum -= 2;
  14.                                 // 这里需要做很多事情...调整速度、检查输赢等...
  15.                                 goto next;
  16.                             }
  17.                         }
  18.                     }
  19.                     next: ;   // do nothing here, just start a new search.
  20.                 }
  21.             }
  22.         }

        第二部分其实也就一个函数的实现: CanbeDeleted

  1. /
  2. // This function will check if two cells have any connect path
  3. // if so, true will be returned and then we simulate the mouse
  4. // click, else we return false and do nothing.
  5. // 
  6. bool CanBeDeleted(int row1, int col1, int row2, int col2)
  7. {
  8.     // First we check this kind of path: two x-lines and one y-line.
  9.     // one x-line and one y-line or only one x-line are all special
  10.     // cases, will also be handled by following code.
  11.     int left1 = col1, right1 = col1;
  12.     int left2 = col2, right2 = col2;
  13.     if(col1 != 0)
  14.         while(left1 > 0 && g_AllCells[row1][left1 - 1].empty){ left1-- ;}
  15.     if(col1 != gc_ColNum - 1)
  16.         while(right1 < gc_ColNum - 1 && g_AllCells[row1][right1 + 1].empty){ right1++ ;}
  17.     if(col2 != 0)
  18.         while(left2 > 0 && g_AllCells[row2][left2 - 1].empty){ left2-- ;}
  19.     if(col2 != gc_ColNum - 1)
  20.         while(right2 < gc_ColNum - 1 && g_AllCells[row2][right2 + 1].empty){ right2++ ;}
  21.     ASSERT(left1 >= 0 && left2 >=0 && right1 < gc_ColNum && right2 < gc_ColNum);
  22.     int commonLeft  = max(left1, left2);
  23.     int commonRight = min(right1, right2);
  24.     if(commonLeft <= commonRight) {
  25.         int upCellRowIndex   = min(row1, row2);
  26.         int downCellRowIndex = max(row1, row2);
  27.         for(int i = commonLeft; i <= commonRight; i++) {
  28.             int upCellRowIndexTmp = upCellRowIndex;
  29.             int downCellRowIndexTmp = downCellRowIndex;
  30.             if(i == col1) {
  31.                 if(row1 == upCellRowIndexTmp)
  32.                     upCellRowIndexTmp++;
  33.                 else
  34.                     downCellRowIndexTmp--;
  35.             }
  36.             if(i == col2) {
  37.                 if(row2 == upCellRowIndexTmp)
  38.                     upCellRowIndexTmp++;
  39.                 else
  40.                     downCellRowIndexTmp--;
  41.             }
  42.             if(downCellRowIndexTmp < upCellRowIndexTmp)
  43.                 return true;
  44.             while(g_AllCells[upCellRowIndexTmp][i].empty) {
  45.                 if(upCellRowIndexTmp++ == downCellRowIndexTmp) {
  46.                     // We do find a path here, return true.
  47.                     return true;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     // First we check this kind of path: two y-lines and one x-line.
  53.     // one x-line and one y-line or only one y-line are all special
  54.     // cases, will also be handled by following code.
  55.     int up1 = row1, down1 = row1;
  56.     int up2 = row2, down2 = row2;
  57.     if(row1 != 0)
  58.         while(up1 > 0  && g_AllCells[up1 - 1][col1].empty){ up1-- ;}
  59.     if(row1 != gc_RowNum - 1)
  60.         while(down1 < gc_RowNum - 1 && g_AllCells[down1 + 1][col1].empty){ down1++ ;}
  61.     if(row2 != 0)
  62.         while(up2 > 0  && g_AllCells[up2 - 1][col2].empty){ up2-- ;}
  63.     if(row2 != gc_RowNum - 1)
  64.         while(down2 < gc_RowNum - 1 && g_AllCells[down2 + 1][col2].empty){ down2++ ;}
  65.     ASSERT(up1 >= 0 && up2 >=0 && down1 < gc_RowNum && down2 < gc_RowNum);
  66.     int commonUp   = max(up1, up2);
  67.     int commonDown = min(down1, down2);
  68.     if(commonUp <= commonDown) {
  69.         int leftCellColIndex  = min(col1, col2);
  70.         int rightCellColIndex = max(col1, col2);
  71.         for(int i = commonUp; i <= commonDown; i++) {
  72.             int leftCellColIndexTmp  = leftCellColIndex;
  73.             int rightCellColIndexTmp = rightCellColIndex;
  74.             if(i == row1) {
  75.                 if(col1 == leftCellColIndexTmp)
  76.                     leftCellColIndexTmp++;
  77.                 else
  78.                     rightCellColIndexTmp--;
  79.             }
  80.             if(i == row2) {
  81.                 if(col2 == leftCellColIndexTmp)
  82.                     leftCellColIndexTmp++;
  83.                 else
  84.                     rightCellColIndexTmp--;
  85.             }
  86.             if(rightCellColIndexTmp < leftCellColIndexTmp)
  87.                 return true;
  88.             while(g_AllCells[i][leftCellColIndexTmp].empty) {
  89.                 // We do find a path here, return true.
  90.                 if(leftCellColIndexTmp++ == rightCellColIndex) {
  91.                     return true;
  92.                 }
  93.             }
  94.         }
  95.     }
  96.     // No connect path find if we get here. Return false.
  97.     return false;
  98. }

        这个函数包含了两部分:检查X-Y-X连通及Y-X-Y连通。当然包括X-Y等其他特殊情况。两部分代码基本完全一致,不难看懂。

        到目前为止,大部分工作已经完成了。接下来我们需要模拟鼠标的点击事件。

3. 鼠标事件的模拟

        整个程序有两个地方需要模拟鼠标点击,第一当然是去消掉那些方块,第二我们用于模拟点击开始按钮,这样我们就可以保证程序连续运行,实现真正的挂机。分别采用了如下的方法:

  1. /
  2. // Simulate the mouse click every time we find two cells 
  3. // that can be deleted.
  4. // 
  5. void SimulateMouseClick(CPoint pt1, CPoint pt2)
  6. {
  7.     ::SendMessage(g_GameWnd, WM_LBUTTONDOWN, 0, (LPARAM)MAKELONG(pt1.x, pt1.y));
  8.     ::SendMessage(g_GameWnd, WM_LBUTTONUP, 0, (LPARAM)MAKELONG(pt1.x, pt1.y));
  9.     ::SendMessage(g_GameWnd, WM_LBUTTONDOWN, 0, (LPARAM)MAKELONG(pt2.x, pt2.y));
  10.     ::SendMessage(g_GameWnd, WM_LBUTTONUP, 0, (LPARAM)MAKELONG(pt2.x, pt2.y));
  11. }
  12. ......
  13. ......
  14. ......
  15. // 模拟鼠标点击开始按钮的代码
  16. // Click the start button, waiting for other players to start.
  17. if(gc_StartFlagColor != ::GetPixel(g_GameDC, gc_StartFlagPoint.x, gc_StartFlagPoint.y)) {
  18.     // Simulate to click the start button on the game window.
  19.     CPoint pt(gc_StartButtonPoint);
  20.     ::ClientToScreen(g_GameWnd, &pt);
  21.     SetCursorPos(pt.x, pt.y);
  22.     mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
  23.     mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
  24.     // Keep waiting if the game window remains unchanged.
  25.     while(::GetPixel(g_GameDC, gc_StartFlagPoint.x, gc_StartFlagPoint.y) != gc_StartFlagColor){;}
  26. }

        好了,到此为止程序已经可以跑起来了...并且可以实现秒杀了...但是在我的外挂中另外还实现了其他一些简单的功能。从下面的截图中我们可以看出来程序的其他一些功能。

       

        我们可以看到程序可以有手动和自动两种方式运行。我们可以在程序运行的时候动态地拖动滚动条来更改程序运行速度,当然我们需要采用多线程编写。手动调整速度的实现比较简单,我们只需根据滚动条的位置计算出一个较为合理的等待时间,然后调用sleep函数就可以了。自动方式较为复杂,我需要根据其他玩家的速度动态调整我的速度,虽然功能是实现了,基本可以保证赢得看不出来,但是还是有很多地方需要改进。

        另外需要说明的是,虽然这个外挂是基于无道具版本写的,但是具有一定的处理道具的能力。我的实现方式如下:每次找到两个可以消掉的方块后先进行鼠标模拟点击,然后从界面上获得自己的剩余方格数量与程序内部的方格数量g_CellNum进行比较,如果发现两个不符则说明遇到障碍、禁手、或者镜子等道具,等待一定时间后重新获取界面各种信息继续运行,这样在一定程度上保证了程序的持续运行性。但是改版本暂时不能处理重列功能,当遇到无解的时候需要手动重列。虽然要实现这个功能不难,但是考虑到该版本是使用了模拟鼠标的方法实现的,很难做到完美,就在下一个版本中增加这个功能了。

        另外再开发的时候还遇到了一个难点,就是如何判断输赢。每次消掉两个后程序会等待一个时间然后再继续消掉下两个,但是在这段时间中对手可能已经赢了,游戏结束,但是我的程序确还在"消掉"。这显然不是我们期望的,因为在整个程序中,我没有使用sleep函数去等待。而是在每次等待的时间内不断的去查看其它玩家的剩余方块数,如果检测到零则说明游戏结束,其他玩家已经胜利。如下函数帮我实现了这个目的:

  1. /
  2. // This function is used to wait for a provided wait time and
  3. // check the game state. If anyother player has finished this
  4. // game we return flase.
  5. // 
  6. bool WaitAndCheckGameState(int waitingTime)
  7. {
  8.     DWORD startTime = ::GetTickCount();
  9.     do {
  10.         // 这里的GetMostCompetitiveScore用来获得最快玩家的剩余方块数
  11.         if(0 == GetMostCompetitiveScore())
  12.             return false;
  13.     }
  14.     while(::GetTickCount() <= startTime + waitingTime);
  15.     return true;
  16. }

        通过这种方式,我可以基本地判断出游戏的胜负。但是还有一种情况暂时无法处理,就是当遇到其他玩家也使用外挂并且都是用秒杀的时候,这时胜负很难判断。程序会经常判断错误。在以后的版本中会进行改进。

遇到的问题:

1. 在HOOK中使用MessageBox似乎有问题,当用鼠标右击的时候有偶然性。不是每次都能弹出。网上查了一下好像也说在HOOK中不易使用MessageBox。具体原因有待研究...

2. 在多线程中使用InstallHook好像不能正常使用HOOK,没找到什么原因,因为在这个外挂中使用到了多线程来保证界面的可交互性,当我在开始按钮点击的相应函数中调用InstallHook可以正常使用,但是在AfxBeginThread函数启动的新线程中调用InstallHook好像不能正常使用HOOK。

3. 开始我的程序全部使用了CWindowDC作为获取像素值的DC,后来改为用::GetPixel(g_GameDC,...),但是发现用这两种方法获取的同一个点的像素值不同,不知道是不是正常现象。

4. 在模拟鼠标点击开始按钮的时候我开始也使用了::SendMessage()函数去给游戏窗口发送消息,但是没有成功。后来只能选择用mouse_event来实现这个功能。但是在这个程序中mouse_event函数比起::SendMessage()函数有不足之处。我们需要保持窗口处于最顶层然后进行点击,造成了很多不便。不知道是何原因,可能还需要深入了解外挂知识。

希望在今后的学习中能找到这些问题的原因及解决方法。

程序存在的问题:

目前来说程序还存在很多问题:

1. 没有强大的道具处理能力。在出现道具的情况下程序有时会失灵(我的错...有bug没解掉T_T)。

2. 在运行时需要将游戏窗口置于在前端,要不然也会有问题。因为要获取颜色、用mouse_event等都需要将游戏窗口置前。

3. 在遇到更强大的外挂的时候秒杀拼不过人家...郁闷...这个估计跟外挂的类型有关了...等待下一个版本的完成(通过伪造数据包的方法应该会更快更强大)。

4. 本来以为能解决前面三个问题程序就比较完美了,没想到在写这篇文章的时候花生米找我玩连连看,牛人啊,开口就让我调半秒。一玩果然不亏zju连连看老大啊,速度相当快。他提出了很多建设性意见,可以更好的伪装。原来大牛们看外挂有自己的一套啊。赶紧记下来,在以后的版本中改进改进:1.高手都用重列和指南针。本来以为用指南针会影响高手速度,原来不是的,高手都是重列完马上指南针然后可以连击下去。见识了见识了。2. 很多外挂不符合人眼规则,就是不消近的消远的。一直以为高手外连连看都狂点狂点,想不到还有心思研究这个。牛啊。另外他还跟我说了一个秘密哈哈,全中国的连连看高手都在无道具场3和10。果然是高手,唉,真的见识了。下次再写一个厉害一点的让他去鉴定哈哈。

5. 今天在测试的时候又遇到了一个新的问题...在vista下不能用...唉...问题多多,期待下一个版本的完成吧...

在整个程序中也遇到了一些问题,至今还未解决,暂且一一列出:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值