//
// Algorithm_A_Self.cpp
// StudyTest
//
// Created by metrics001 on 16/11/9.
//
//
#include "Algorithm_A_Self.hpp"
#define DIS 50
#define boardDIS 140
#define boardDIS2 260
static POSTION allPOSTION[25];
static const POINT* point_head;
static vector*> pointList;
void Algorithm_A_Self::init_path(Layer* layer)
{
int data[5][5] = {
{1,1,1,1,1},
{1,1,1,0,1},
{1,1,1,0,1},
{1,0,0,1,1},
{1,1,1,1,1}
};
int index = 0;
int n =5;int m =5;
for (int i =0;i
for (int j =0;j
allPOSTION[index].x = j;
allPOSTION[index].y = i;
allPOSTION[index].value = data[i][j];
index++;
}
}
for (int i = 0; i<</span>25; i++) {
Sprite* spr = Sprite::create("CloseNormal.png");
layer->addChild(spr);
spr->setPosition(Vec2(boardDIS+allPOSTION[i].x*DIS,boardDIS2-allPOSTION[i].y*DIS));
if (allPOSTION[i].value==0) {
spr->setColor(Color3B(255,0,0));
}
}
}
int Algorithm_A_Self::get_length(POINT* cur)
{
int length = 1;
POINT* point = cur;
while (point->m_last!=nullptr) {
point = point->m_last;
length++;
}
return length;
}
bool Algorithm_A_Self::check_pos_valid(POSTION pos)
{
for (int i = 0; i<</span>25; i++) {
if (pos.x == allPOSTION[i].x&&pos.y == allPOSTION[i].y) {
if (allPOSTION[i].value==0) {
return false;
}
return true;
}
}
return false;
}
void Algorithm_A_Self::handle_point(POINT* cur,POSTION pos,vector* nextList)
{
POINT* point = check_bool_at_path(pos,point_head);
if (point==nullptr) {
POINT* pData = new POINT();
pData->m_pos = pos;
cur->m_next->push_back(pData);
pData->m_last = cur;
pData->m_length = get_length(pData);
//新加的放入下次搜索列表中
nextList->push_back(pData);
}else
{
point->m_length = get_length(point);
cur->m_length = get_length(cur);
if (point->m_length >cur->m_length+1) {
POINT* last = point->m_last;
point->m_last = cur;
cur->m_next->push_back(point);
if (last==nullptr) {
return;
}
vector<_POINT*>* next = last->m_next;
int index = 0;
for (auto iter = next->begin(); iter != next->end(); iter++) {
if (*iter==point) {
next->erase(next->begin()+index);
break;
}
index++;
}
}
}
}
vector* Algorithm_A_Self::updata_data_for_queue(vector* points)
{
if(NULL == points )
return nullptr;
vector* nextList = new vector();
for(int index = 0; index < points->size(); index ++){
POINT* data = points->at(index);
if ( data->m_open==false) { //检测过了,有点远
continue;
}
vector* pointList = new vector();
data->m_next = pointList;
if(check_pos_valid(POSTION(data->m_pos.x, data->m_pos.y - 1))){
handle_point(data,POSTION(data->m_pos.x, data->m_pos.y - 1),nextList);
}
if(check_pos_valid(POSTION(data->m_pos.x -1, data->m_pos.y))){
handle_point(data,POSTION(data->m_pos.x -1, data->m_pos.y),nextList);
}
if(check_pos_valid(POSTION(data->m_pos.x, data->m_pos.y + 1))){
handle_point(data,POSTION(data->m_pos.x, data->m_pos.y + 1),nextList);
}
if(check_pos_valid(POSTION(data->m_pos.x + 1, data->m_pos.y))){
handle_point(data,POSTION(data->m_pos.x + 1, data->m_pos.y),nextList);
}
}
// points->clear();
// delete points;
return nextList;
}
POINT* Algorithm_A_Self::check_bool_at_path(POSTION pos,const POINT* head)
{
const POINT* cur = head;
if (cur->m_pos.x==pos.x&&cur->m_pos.y==pos.y) {
// printf("已经存在了\n");
return (POINT*)cur;
}
if (cur->m_next == nullptr || cur->m_next->size()==0) {
return nullptr;
}
for (auto iter = cur->m_next->begin(); iter != cur->m_next->end(); iter++) {
POINT* point = check_bool_at_path(pos,*iter);
if (point!=nullptr) {
return point;
}
}
return nullptr;
}
// 得到估算距离最近
POINT* Algorithm_A_Self::sort_most_nearest_neigh(vector* pointList,POSTION end)
{
//排序优先查找最小的
sort(pointList->begin(),pointList->end(),[=](POINT* p1,POINT* p2){
int dis1 = abs(p1->m_pos.x - end.x)+ abs(p1->m_pos.y - end.y) + get_length(p1);
int dis2 = abs(p2->m_pos.x - end.x)+ abs(p2->m_pos.y - end.y)+ get_length(p2);;
// point->m_length = get_length(point);
// cur->m_length = get_length(cur);
// float dis1 = (float) sqrt((p1->m_pos.x - end.x) * (p1->m_pos.x - end.x)+ (p1->m_pos.y - end.y) * (p1->m_pos.y - end.y));
// float dis2 = (float) sqrt((p2->m_pos.x - end.x) * (p2->m_pos.x - end.x)+ (p2->m_pos.y - end.y) * (p2->m_pos.y - end.y));
if (dis1
return true;
}
return false;
});
return pointList->at(0);
}
POINT* Algorithm_A_Self::check_path(POINT* point,POSTION end)
{
vector* points = new vector();
points->push_back(point);
while (true) {
if (points->size()==0) {
printf("此路不通\n");
return nullptr;
}
pointList.push_back(points);
// //排序下一级的点
POINT* min = sort_most_nearest_neigh(points,end);
// //关闭远的点
int dis1 = abs(min->m_pos.x - end.x)+ abs(min->m_pos.y - end.y) + get_length(min);
for (int i = 0;isize(); i++) {
POINT *point = points->at(i);
int dis = abs(point->m_pos.x - end.x)+ abs(point->m_pos.y - end.y) + get_length(point);
if (dis>dis1) {
point->m_open=false;
break;
}
}
for (auto iter = points->begin(); iter != points->end(); iter++) {
POINT* cur = *iter;
if (cur->m_pos.x==end.x&&cur->m_pos.y==end.y) {
printf("找到目标了\n");
return cur;
}
}
points = updata_data_for_queue(points);
printf("count:%d\n",(int)points->size());
}
return nullptr;
// POINT* cur = point;
// if (cur->m_pos.x==end.x&&cur->m_pos.y==end.y) {
// printf("找到目标了\n");
// return cur;
// }
// vector* points = updata_data_for_queue(cur);
// if (points->size()==0) {
//
// printf("此路不通\n");
// printf("%d,%d,\n",cur->m_pos.x,cur->m_pos.y);
// return nullptr;
// }
//
//
// //排序下一级的点
// POINT* min = sort_most_nearest_neigh(points,end);
//
// for (auto iter = points->begin(); iter != points->end(); iter++) {
// POINT* cur = *iter;
// POINT* point =check_path(cur, end);
// if (point!=nullptr) {
// printf("找到目标了\n");
// return point;
// }
// }
//
return check_path(min,end);
//
// POINT* reult = nullptr;
// for (auto iter = cur->m_next->begin(); iter != cur->m_next->end(); iter++) {
// POINT* _reult = check_path(*iter,end);
// if (_reult!=nullptr) {
// return _reult;
reult = _reult;
// }
// }
// return reult;
}
void Algorithm_A_Self::find_path(POSTION begin,POSTION end,Layer* layer)
{
POINT*head = new POINT();
point_head = head;
POINT*cur = head;
cur->m_pos = begin;
POINT* end_pint = check_path(cur,end);
if (end_pint == nullptr) {
printf("没有找到可行路径\n");
return ;
}
// routePOSTION 即为路径
POINT* point = end_pint;
vector routePOSTION;
routePOSTION.push_back(point->m_pos);
while (point->m_last!=nullptr) {
point = point->m_last;
routePOSTION.push_back(point->m_pos);
}
int count = pointList.size();
for (int i = 0; i
vector* points = pointList.at(i);
for (int j = 0; jsize();j++) {
POINT* point = points->at(j);
Sprite* spr = Sprite::create("ui_b_next.png");
layer->addChild(spr,2);
spr->setPosition(Vec2(boardDIS+point->m_pos.x*DIS,boardDIS2-point->m_pos.y*DIS));
spr->setVisible(false);
spr->runAction(Sequence::create(DelayTime::create(i*1),CallFunc::create([=](){spr->setVisible(true);}),nullptr));
spr->setColor(Color3B(60,60,60));
Size size = spr->getContentSize();
auto label = Label::createWithTTF(__String::createWithFormat("%d",i)->getCString(),"fonts/Marker Felt.ttf", 12);
label->setPosition(Vec2( size.width/2, size.height/2));
spr->addChild(label);
}
}
// int count = (int)routePOSTION.size();
// for (int i = 0; i
// Sprite* spr = Sprite::create("ui_b_next.png");
// layer->addChild(spr,2);
// spr->setPosition(Vec2(boardDIS+routePOSTION.at(count-1-i).x*DIS,boardDIS2-routePOSTION.at(count-1-i).y*DIS));
// spr->setVisible(false);
// spr->runAction(Sequence::create(DelayTime::create(i*1),CallFunc::create([=](){spr->setVisible(true);}),nullptr));
// }
//
Sprite* spr = Sprite::create("ui_record.png");
layer->addChild(spr,1);
spr->setPosition(Vec2(boardDIS+end.x*DIS,boardDIS2-end.y*DIS));
}