先贴代码
主要是一个寻路算法。
程序运行于VS2017 + OPENCV (项目要加入opencv的配置)+ ANDROID(用电脑连安卓手机玩,adb)
程序可以运行于460关之后7*6网格的关卡,低等级关卡要修改网格长宽,和每个格子的位置。
/*
vs2017
时间:2018-11-1
功能:wechat最强连一连C++代码的实现,有待改进,仅供学习之用!欢迎大家提出新算法
*/
#include "pch.h"
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include<stdlib.h>//rand()随机数头文件
#include<windows.h>//Speep()随便延迟
#include <vector>
using namespace cv;
using namespace std;
//宏定义
#define ROW 6
#define COLUMN 7
//结构体定义
struct point
{
int x;
int y;
};
//节点
struct node
{
int len;
struct point p;
struct node *father;
struct node *t;
struct node *d;
struct node *l;
struct node *r;
bool b;
bool m[COLUMN][ROW];
node()
{
len = 0;
p.x = 0;
p.y = 0;
t = NULL;
d = NULL;
t = NULL;
l = NULL;
b = true;
memset(m, false, sizeof(m));
}
};
//全局变量定义区
struct node *g;//全局可用节点
int g_iCount;//节点长度
bool m[COLUMN][ROW];
std::vector<point> myPoint;
//获取格子个数
int GetLen(bool m[][ROW])
{
int len = 0;
for (int i = 0; i < COLUMN; i++)
{
for (int j = 0; j < ROW; j++)
{
if (m[i][j])
{
len++;
}
}
}
return len;
}
//获取路径
bool FindPath(node *now)
{
if (g)
{
std::cout << "找到G" << std::endl;
return false;
}
//左
if (now->p.x - 1 >= 0 && now->m[now->p.x - 1][now->p.y])
{
struct node *l = new node();
l->p.x = now->p.x - 1;
l->p.y = now->p.y;
l->len = now->len + 1;
memcpy(l->m, now->m, sizeof(l->m));
l->m[l->p.x][l->p.y] = false;
now->l = l;
l->father = now;
if (l->len >= g_iCount)
{
g = l;
return false;
}
FindPath(l);
}
//上
if (now->p.y - 1 >= 0 && now->m[now->p.x][now->p.y - 1])
{
struct node *t = new node();
t->p.x = now->p.x;
t->p.y = now->p.y - 1;
t->len = now->len + 1;
memcpy(t->m, now->m, sizeof(t->m));
t->m[t->p.x][t->p.y] = false;
now->t = t;
t->father = now;
if (t->len >= g_iCount)
{
g = t;
return false;
}
FindPath(t);
}
//下
if (now->p.y + 1 < 6 && now->m[now->p.x][now->p.y + 1])
{
struct node *d = new node();
d->p.x = now->p.x;
d->p.y = now->p.y + 1;
d->len = now->len + 1;
memcpy(d->m, now->m, sizeof(d->m));
d->m[d->p.x][d->p.y] = false;
now->d = d;
d->father = now;
if (d->len >= g_iCount)
{
g = d;
return false;
}
FindPath(d);
}
//右
if (now->p.x + 1 < 7 && now->m[now->p.x + 1][now->p.y])
{
struct node *r = new node();
r->p.x = now->p.x + 1;
r->p.y = now->p.y;
r->len = now->len + 1;
memcpy(r->m, now->m, sizeof(r->m));
r->m[r->p.x][r->p.y] = false;
now->r = r;
r->father = now;
if (r->len >= g_iCount)
{
g = r;
return false;
}
FindPath(r);
}
//删除节点
if (now->l == NULL && now->t == NULL && now->d == NULL && now->r == NULL)
{
if (now->father != NULL)
{
if (now->p.x == now->father->p.x)
{
if (now->p.y == now->father->p.y + 1)
{
now->father->d = NULL;
}
else
{
now->father->t = NULL;
}
}
else
{
if (now->p.x == now->father->p.x + 1)
{
now->father->r = NULL;
}
else
{
now->father->l = NULL;
}
}
delete now;
}
}
return true;
}
int myPrint()
{
if (g)
{
int position[ROW][COLUMN] = { 0 };
node *p = g;
for (int i = 0; i < g_iCount - 1; i++)
{
position[p->p.x][p->p.y] = g_iCount - 1 - i;
p = p->father;
}
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COLUMN; j++)
{
printf("[%2d] ", position[i][j]);
}
printf("\n");
}
}
return 0;
}
int GetPoint()
{
if (g)
{
node *p = g;
for (int i = 0; i < g_iCount - 1; i++)
{
point po;
po.x = p->p.x;
po.y = p->p.y;
myPoint.push_back(po);
p = p->father;
}
}
return 0;
}
//截图
void get_screenshot()
{
system("adb shell screencap -p /sdcard/autojump.png");
system("adb pull /sdcard/autojump.png");
}
int main(int argc, char** argv)
{
while (true)
{
g = NULL;
//截图
get_screenshot();
//读图
Mat srcImage = imread("autojump.png");
//****************************************
//识图大礼包900-950 630-680
Mat xImage = imread("x.png");
//模板本身也是有一定体积的,所以result的宽高只能是这样
int width = srcImage.cols - xImage.cols + 1;
int height = srcImage.rows - xImage.rows + 1;
//将各像素的匹配值也输出为图像
Mat result(width, height, CV_32FC1);
matchTemplate(srcImage, xImage, result, CV_TM_CCOEFF_NORMED);
//查询匹配系数
cv::Point minLoc;
cv::Point maxLoc;
double minValue, maxValue;
minMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc, Mat());
if (maxValue > 0.95)
{
Sleep(1000 + rand() % 2000);
int xx1 = 920 + rand() % 20;
int xx2 = 920 + rand() % 20;
int yy1 = 650 + rand() % 20;
int yy2 = 650 + rand() % 20;
int tt1 = 40 + rand() % 20;
char AA[50];
sprintf_s(AA, "adb shell input swipe %d %d %d %d %d", xx1, yy1, xx2, yy2, tt1);
system(AA);
Sleep(5000 + rand() % 2000);
continue;
}
//下一关
Mat xygImage = imread("xyg.png");
int xyg_width = srcImage.cols - xygImage.cols + 1;
int xyg_height = srcImage.rows - xygImage.rows + 1;
Mat xyg_result(xyg_width, xyg_height, CV_32FC1);
matchTemplate(srcImage, xygImage, xyg_result, CV_TM_CCOEFF_NORMED);
cv::Point xyg_minLoc;
cv::Point xyg_maxLoc;
double xyg_minValue, xyg_maxValue;
minMaxLoc(xyg_result, &xyg_minValue, &xyg_maxValue, &xyg_minLoc, &xyg_maxLoc, Mat());
if (xyg_maxValue > 0.95)
{
Sleep(1000 + rand() % 2000);
int xxx1 = 540 + rand() % 20;
int xxx2 = 540 + rand() % 20;
int yyy1 = 1600 + rand() % 20;
int yyy2 = 1600 + rand() % 20;
int ttt1 = 40 + rand() % 20;
char AAA[50];
sprintf_s(AAA, "adb shell input swipe %d %d %d %d %d", xxx1, yyy1, xxx2, yyy2, ttt1);
system(AAA);
Sleep(5000 + rand() % 2000);
continue;
}
//第一个节点
node *start = new node();
start->len = 1;
start->p.x = 0;
start->p.y = 0;
//识图
for (int i = 0; i < COLUMN; i++)
{
for (int j = 0; j < ROW; j++)
{
Scalar color = srcImage.at<Vec3b>( 420 + i * 125 , 225 + j * 125);//纵,横
if (color.val[0] == 204 && color.val[1] == 204 && color.val[2] == 204)
{
m[i][j] = true;
}
else if (color.val[0] == 59 && color.val[1] == 43 && color.val[2] == 35)//障碍物
{
m[i][j] = false;
}
else//起始点
{
m[i][j] = true;
start->p.x = i;
start->p.y = j;
}
}
}
g_iCount = GetLen(m);
memcpy(start->m, m, sizeof(start->m));
start->m[start->p.x][start->p.y] = false;
std::cout << "start x=" << start->p.x << "y=" << start->p.y << std::endl;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COLUMN; j++)
{
printf("%d", m[i][j]?1:0);
}
printf("\n");
}
std::cout << "iCount = " << g_iCount << std::endl;
//找路径
FindPath(start);
GetPoint();
//点击
int x = 0, y = 0, t = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for (int i = 0; i < g_iCount; i++)
{
if (myPoint.size()>0)
{
point po = myPoint.back();
y1 = 420 + po.x * 125 - 10 + rand() % 20;
x1 = 225 + po.y * 125 - 10 + rand() % 20;
y2 = 420 + po.x * 125 - 10 + rand() % 20;
x2 = 225 + po.y * 125 - 10 + rand() % 20;
t = 40 + rand() % 20;
std::cout << "x=" << po.x << "y=" << po.y << std::endl;
myPoint.pop_back();
char AA[50];
sprintf_s(AA, "adb shell input swipe %d %d %d %d %d", x1, y1, x2, y2, t);
system(AA);
}
Sleep(500+rand()%200);
}
Sleep(5000);
}
return 0;
}
这个是900关的图片。
先这样。