#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
enum direction{
north = 0,
east,
south,
west,
center,
error
};
typedef struct pointInfo
{
int x;
int y;
} Point;
typedef vector<Point> pVector;
typedef vector<Point>::iterator pIter;
typedef struct routeInfo
{
//enum direction pre_direction;
pVector point_vector;
} Route;
int map[6][6] = {
{0,0,0,0,0,0},
{0,1,0,0,0,0},
{0,1,0,1,0,0},
{0,1,0,1,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0}
};
enum direction getDirection2(Point* p1, Point* p2)
{
if (p1->x -p2->x == 1 && p1->y - p2->y == 0)
return west;
else if (p1->x - p2->x == -1 && p1->y - p2->y == 0)
return east;
else if (p1->x - p2->x == 0 && p1->y - p2->y == 1)
return north;
else if (p1->x - p2->x == 0 && p1->y - p2->y == -1)
return south;
else {
//error
return error;
}
};
enum direction getDirection(Route* r)
{
if (r->point_vector.size() <2)
{
return center;
}else
{
return getDirection2(&r->point_vector[r->point_vector.size()-2], &r->point_vector[r->point_vector.size()-1]);
}
}
int getCurTurns(Route* t)
{
int turns = 0;
if (t->point_vector.size() >2)
{
enum direction preDirection = center;
enum direction curDirection;
for (int i = 0; i < t->point_vector.size() - 1; i++)
{
curDirection = getDirection2(&t->point_vector[i], &t->point_vector[i+1]);
if (preDirection != center && preDirection != curDirection)
{
turns ++;
}
preDirection = curDirection;
}
}
return turns;
};
//only turn num < 2 can be the neighbors
void setNeighbors(Route* r, Point* p, pVector* neighbors)
{
r->point_vector.push_back(*p);
if (getCurTurns(r) < 3)
neighbors->push_back(*p);
r->point_vector.pop_back();
}
pVector getMovableNeighbors(Point* p, Point* end, Route* r)
{
pVector neighbors;
enum direction direct;
direct = getDirection(r);
//east
if (p->x + 1 < 6 //border
&& map[p->y][p->x + 1] != 1//movable
&& direct != west // can not go back
)
{
Point n;
n.x = p->x + 1; n.y = p->y;
setNeighbors(r, &n, &neighbors);
}
//west
if (p->x - 1 >= 0 //border
&& map[p->y][p->x - 1] != 1 //movable
&& direct != east // can not go back
)
{
Point n;
n.x = p->x - 1; n.y = p->y;
setNeighbors(r,&n, &neighbors);
}
//south
if (p->y + 1 < 6 //border
&& map[p->y + 1][p->x] != 1 //movable
&& direct != north // can not go back
)
{
Point n;
n.x = p->x; n.y = p->y + 1;
setNeighbors(r,&n, &neighbors);
}
//north
if (p->y - 1 >= 0 //border
&& map[p->y - 1][p->x] != 1 //movable
&& direct != south // can not go back
)
{
Point n;
n.x = p->x ; n.y = p->y - 1;
setNeighbors(r,&n, &neighbors);
}
return neighbors;
}
void printTheRoute(Route* aRoute)
{
ostringstream ost;
ost << "The Route is : turn " << getCurTurns(aRoute)<< "\n";
for(int i = 0 ; i < aRoute->point_vector.size(); i++)
{
ost << "[" << aRoute->point_vector[i].x << "," << aRoute->point_vector[i].y << "]";
if (i != aRoute->point_vector.size() - 1)
{
ost << " -> ";
}
}
cout << ost.str() << "\n";
}
void move_step(Point* start, Point* end, Route* r)
{
//get point's neighbor
//if the neighbor is the target, mission end
//else move_step(neighbor)
pVector neighbors = getMovableNeighbors(start, end, r);
pIter it;
for (it = neighbors.begin(); it != neighbors.end(); it++)
{
if (it->x == end->x && it->y == end->y)
{
//Found!!
r->point_vector.push_back(*end);
printTheRoute(r);
r->point_vector.pop_back();
}else
{
r->point_vector.push_back(*it);
//printTheRoute(r);
move_step(it, end, r);
r->point_vector.pop_back();
//printTheRoute(r);
}
}
}
void test();
void main()
{
//cout << "hello world!\n";
//test();
Route aRoute;
Point start;
Point des;
start.x = 1; start.y = 1;
des.x = 3; des.y = 3;
aRoute.point_vector.push_back(start);
map[des.y][des.x] = 0;
move_step(&start, &des, &aRoute);
}
void test()
{
Point p1;
p1.x = 10;
p1.y = 5;
Point p2;
p2.x = 10; p2.y = 6;
pVector points;
points.push_back(p1);
points.push_back(p2);
Route aRoute;
aRoute.point_vector = points;
for(int i = 0 ; i < aRoute.point_vector.size(); i++)
{
ostringstream ost;
ost << "point x: " << aRoute.point_vector[i].x << " point y: " << aRoute.point_vector[i].y << "\n";
cout << ost.str();
}
}
分享到:
相关推荐
微信小游戏源码 奇葩连连看游戏源码(仅用于学习参考)微信小游戏源码 奇葩连连看游戏源码(仅用于学习参考)微信小游戏源码 奇葩连连看游戏源码(仅用于学习参考)微信小游戏源码 奇葩连连看游戏源码(仅用于学习...
期末大作业基于VUE的连连看小游戏源码,内附安装教程。 期末大作业基于VUE的连连看小游戏源码,内附安装教程。期末大作业基于VUE的连连看小游戏源码,内附安装教程。期末大作业基于VUE的连连看小游戏源码,内附安装...
MFC连连看 MFC连连看 MFC连连看 MFC连连看 MFC连连看
基于Android开发的连连看小游戏源码。 基于Android开发的连连看小游戏源码.基于Android开发的连连看小游戏源码.基于Android开发的连连看小游戏源码.基于Android开发的连连看小游戏源码.基于Android开发的连连看小...
【Java毕业设计】基于Java的连连看游戏设计与实现(论文+答辩PPT+源代码+讲解视频).zip 【Java毕业设计】基于Java的连连看游戏设计与实现(论文+答辩PPT+源代码+讲解视频).zip 【Java毕业设计】基于Java的连连看...
连连看20合一的超级连连看20合一的超级连连看20合一的超级连连看
这是我最初实现的连连看(版本1.0)。和之前上传的后期实现(版本2.0)的不同,这个版本非常简单,没有“关卡”的概念,或者说只有一个关卡,并且没有任何的特效,不包含引力模式,只有基本的提示、洗牌的功能。 在...
基于Java的连连看实现代码
在按键精灵中,用vbscript实现的一个模拟点击QQ连连看的测试代码
基于java的连连看游戏设计和实现,基本功能包括:消除模块,重新开始模块,刷新模块,选择难度模块,计时模块。本系统结构如下: (1)消除模块: 完成连连看游戏消除动作的模块。 (2)重新开始模块: 完成...
连连看是以VC6.0为平台,用标准C++语言、基于微软MFC类库完成的一个简单的软件。整套软件由客户端程序和服务器端程序构成,客户端运行在玩家的个人电脑上,所有操作均通过网络通讯向服务器发送请求,并接受服务器端...
某日,闲来无事,无聊的翻看着电脑中的APP图标,忽然萌生一个想法:用Axure原型制作一个连连看类型的游戏原型,于是说做就做,打开AxureRP7,开始搞。一天,两天。。。(由于只有业余时间搞)。。。五天,原型终于...
连连看-连连看.rar
甘蔗连连看是甘蔗小游戏推出的一款连连看单机小游戏,这是非常风靡的办公室小游戏,甘蔗连连看游戏具有下面的特点: 1、由低到高的难度,让你循序渐进的成为连连看高手; 2、可以保存游戏进度,每日只需轻松的玩上一...
漂亮的办公小姐面对着手机聚精会神的时候,十之八九,她在玩连连看游戏!为了方便大家在工作之余能够获得更多快乐,偶特意制作了一款连连看小游戏——“京剧人物连连看”。本游戏的画面非常漂亮,游戏中的每个图标、...
基于Java的连连看游戏设计与实现毕业设计论文文档+软件源码+视频说明,基于java的连连看游戏设计和实现,基本功能包括:消除模块,重新开始模块,刷新模块,选择难度模块,计时模块。本系统结构如下: (1)消除...
连连看是一款经典的益智游戏,主要寻路算法为穷举算法,网上随处可见。 本游戏也是连连看游戏,作者给其取名为开心连连看,主要寻路算法由作者完全自主创新设计,跟传统的穷举寻路算法有很巨大的区别,因为这个算法...
涉及双缓冲和连连看算法....vc6.0实现
用c++语言写的一个连连看,有源码,学习娱乐都可以。
安卓开发--Android连连看游戏源码.zip 1. yinhe.board.AbstractBoard类: 为了初始化游戏状态,程序需要创建一个Piece[][]数组,为此程序定义一个AbstractBoard抽象类, 2. yinhe.board.GameService类 负责游戏的...