[C++] Source: Minesweeper Bot

01/08/2009 11:06 BlackFog#1
Moin,
Hier mal einen Minesweeper Bot den ich geschrieben habe. Wer sich ein bisschen mit ReadProcessMemory beschäftigen möchte, kann, denke ich, etwas aus dem Source lernen.

Quote:
#include <iostream>
#include <windows.h>

using namespace std;

class cMineMain
{
public:
int x_size;
int y_size;
int mines;


public:
int ReadXSize(HWND hWnd);
int ReadYSize(HWND hWnd);
int ReadMines(HWND hWnd);
int MineCheck(HWND hWnd,int x_coord,int y_coord);
void FirstClick(HWND hWnd,int win_x_pos,int win_y_pos,int screen_x,int screen_y);
void ClickTehShit(HWND hWnd,int x,int y,int screen_x,int screen_y,int win_x_pos,int win_y_pos);



};




int main ( void )
{

cMineMain MineBot;
HWND hWnd;
RECT *rect; // Rect Pointer für GetWindowRect
hWnd = FindWindow(0,"MineSweeper");
int screen_x = GetSystemMetrics(SM_CXSCREEN);
int screen_y = GetSystemMetrics(SM_CYSCREEN);

if (!hWnd)
{
cout << "MineSweeper nicht gefunden!" << endl;

}

else
{
GetWindowRect(hWnd,rect);
MineBot.x_size = MineBot.ReadXSize(hWnd);
MineBot.y_size = MineBot.ReadYSize(hWnd);
MineBot.mines = MineBot.ReadMines(hWnd);
cout << screen_x << endl;
cout << screen_y << endl;
cout << "X-Felder: " << MineBot.x_size << endl;
cout << "Y-Felder: " << MineBot.y_size << endl;
cout << "Anzahl der Minen: " << MineBot.mines << endl;
cout << "X-Position: " << rect->left << endl;
cout << "Y-Position: " << rect->top << endl;

MineBot.FirstClick(hWnd,rect->left,rect->top,screen_x,screen_y);


// Ausgabe !!!
int x = 0;
int y = 0;

do
{
for(x;x < MineBot.x_size;x++)
{
if (MineBot.MineCheck(hWnd,x,y) == 0)
{
cout << "x " ;
MineBot.ClickTehShit(hWnd,x,y,screen_x,screen_y,re ct->left,rect->top);
Sleep(0);
continue;

}
else
{
cout << "B " ;


}

}

y++;
x = 0;
cout << endl;
}while (y < MineBot.y_size);

cout << "Got teh Shit !!!" << endl;
// Ausgabe!!!






}


cin.get();

}







// Read X Size //
// Parameter : hWnd //
// Rückgabewert : Größe des Feldes in X Richtung //
int cMineMain::ReadXSize(HWND hWnd)
{
// Variablen
DWORD procid;
HANDLE mine_handle;
unsigned const address = 0x1005334;
int buf = 0;

GetWindowThreadProcessId(hWnd,&procid);
mine_handle = OpenProcess(PROCESS_ALL_ACCESS,false,procid);
ReadProcessMemory(mine_handle,LPCVOID(address),&bu f,sizeof(buf),NULL);
CloseHandle(mine_handle);
return buf;
}



// Read Y Size //
// Parameter : hWnd //
// Rückgabewert : Größe des Feldes in Y Richtung //
int cMineMain::ReadYSize(HWND hWnd)
{
// Variablen
DWORD procid;
HANDLE mine_handle;

unsigned const address =0x10056A8 ;
int buf = 0;

GetWindowThreadProcessId(hWnd,&procid);
mine_handle = OpenProcess(PROCESS_ALL_ACCESS,false,procid);
ReadProcessMemory(mine_handle,LPCVOID(address),&bu f,sizeof(buf),NULL);
CloseHandle(mine_handle);
return buf;
}

// Read Mines Number //
// Parameter : hWnd //
// Rückgabewert : Anzahl der Minen im Feld //

int cMineMain::ReadMines(HWND hWnd)
{
DWORD procid;
HANDLE mine_handle;
unsigned const address = 0x1005194;
int buf = 0;

GetWindowThreadProcessId(hWnd,&procid);
mine_handle = OpenProcess(PROCESS_ALL_ACCESS,false,procid);
ReadProcessMemory(mine_handle,LPCVOID(address),&bu f,sizeof(buf),NULL);
CloseHandle(mine_handle);
return buf;
}

// Mine Check //
// Parameter: hWnd , x-Koordinate , y- Koordinate //
// Rückgabewert : 1 = Mine ; 0 = Keine Mine //

int cMineMain::MineCheck(HWND hWnd,int x_coord,int y_coord)
{
DWORD procid;
HANDLE mine_handle;
unsigned address =(0x1005340) + (32 * (y_coord+1)) + (x_coord+1);
int buf = 0;

GetWindowThreadProcessId(hWnd,&procid);
mine_handle = OpenProcess(PROCESS_ALL_ACCESS,false,procid);
ReadProcessMemory(mine_handle,LPCVOID(address),&bu f,1,NULL);

if (buf == 0x8f)
{
return 1;
}
else
{
return 0;
}
CloseHandle(mine_handle);
}



void cMineMain::FirstClick(HWND hWnd,int win_x_pos,int win_y_pos,int screen_x, int screen_y)
{
float x_pixel = 65535/screen_x;
float y_pixel = 65535/screen_y;
float complete_winx = x_pixel * static_cast<float>(win_x_pos)+ x_pixel*23;
float complete_winy = y_pixel * static_cast<float>(win_y_pos)+ y_pixel*110;

SetForegroundWindow(hWnd);
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, complete_winx,complete_winy,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);


}

void cMineMain::ClickTehShit(HWND hWnd,int x,int y,int screen_x,int screen_y,int win_x_pos,int win_y_pos)
{

const float pixel_per_field = 16.0;
float x_pixel = 65535/screen_x;
float y_pixel = 65535/screen_y;
float complete_winx = x_pixel * static_cast<float>(win_x_pos)+ x_pixel*23;
float complete_winy = y_pixel * static_cast<float>(win_y_pos)+ y_pixel*110;
SetForegroundWindow(hWnd);
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, complete_winx+(x*pixel_per_field*x_pixel) ,complete_winy+(y*pixel_per_field*y_pixel),0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);




}







100% von mir geschrieben ;). Bei Fragen -> PM oder hier im Thread.