Skip to content

Instantly share code, notes, and snippets.

@petersvp
Created October 23, 2023 00:10
Show Gist options
  • Save petersvp/c987cbf52da1c76834f84f0615cb403d to your computer and use it in GitHub Desktop.
Save petersvp/c987cbf52da1c76834f84f0615cb403d to your computer and use it in GitHub Desktop.
C++ bot that plays Sequence Memory in HumanBenchmark
#include <Windows.h>
#include <iostream>
#include <vector>
#include "stdio.h"
using namespace std;
int main()
{
// for the memory game, colors
COLORREF background_color = 13731627;
COLORREF memory_background_color = 1267795;
// Following row/column offsets are based on Chrome with bookmarks bar shown,
// and the monitor resolution is 1920x1080
// You will most likely need to alter these and change them!
int COLS[] = { 825, 948, 1084 };
int ROWS[] = { 331, 458, 620 };
// Those coordinates are checked for the background color before any attempt to analyze the board
POINT check = { 735, 246 };
// Get Desktop's context forr reading colors from it as fast as computationally possible
auto dc = GetDC(NULL);
// Here we will record the sequence
vector<POINT> sequence;
time_t lastTime = time(0);
int turn = 1;
while (true)
{
auto pixel = GetPixel(dc, check.x, check.y);
if (pixel != background_color) {
std::cout << "Did you hid the game or navigated away? Please focus the game and restart?\n";
lastTime = time(0);
sequence.clear();
Sleep(1000);
continue;
}
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
{
auto p = GetPixel(dc, COLS[col], ROWS[row]);
if (p == 16777215)
{
POINT p = { COLS[col], ROWS[row] };
POINT q = { COLS[col], ROWS[row] };
if (sequence.size() > 0 && memcmp(&sequence.back(), &p, sizeof(POINT)) != 0)
{
sequence.push_back(p);
//std::cout << "\nChanged, new sequence: ";
for (auto p : sequence) std::cout << "(" << p.x << "," << p.y << ")";
lastTime = time(0);
}
else if(sequence.size() == 0)
{
sequence.push_back(p);
std::cout << "\nStarting sequence: ";
//for (auto p : sequence) std::cout << "(" << p.x << "," << p.y << ")";
lastTime = time(0);
}
}
}
if (time(0) > lastTime + 1)
{
std::cout << "\nLooks like playback finished! Playing what I've recorded";
for (const auto& p : sequence)
{
SetCursorPos(p.x, p.y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(32);
}
Sleep(500);
sequence.clear();
lastTime = time(0);
}
}
/**/
// Learning the board in Simon Says
//while (true)
//{
// POINT pos;
// GetCursorPos(&pos);
// c = GetPixel(dc, pos.x, pos.y);
// std::cout << pos.x << "," << pos.y << " => " << c << "\n";
//}
// REACTION TIME
/*
// for the reaction game, the expected color
COLORREF reaction_expected_color = 7002955;
while (true) {
POINT pos;
GetCursorPos(&pos);
c = GetPixel(dc, pos.x, pos.y);
if (c == reaction_expected_color)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(32);
}
}
/**/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment