2012-02-06 35 views
-2

这是它,我不知道为什么它不起作用。有时它的确定,有时死机,有时它会返回错误的方式...明星C++,出了什么问题

//apath.h

#ifndef APATH_H 
#define APATH_H 
#include <vector> 
#include <cmath> 
#include <memory> 
#include <allegro.h> 

using namespace std; 

double len(int x1, int y1, int x2, int y2); 

class GameMap; 

class point 
{ 
public: 
     int x,y; 
     double f,g; 
     point *parent; 
     point(int _x=0, int _y=0, point *par=NULL) {x=_x; y=_y; parent=par; f=0; g=0;}; 
     void countF(int sx, int sy, int tx, int ty) 
     { 
     g=len(x,y,tx,ty); 
     f=g+len(sx,sy,x,y); 
     }; 
}; 

struct point2d 
{ 
     int x,y; 
}; 

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
const point2d directions[]= 
{ 
     {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}, 
}; 


class path 
{ 
public: 
     vector<point2d> way; 
     int step; 
     bool findPath(int x, int y, int fx, int fy, GameMap& map, BITMAP* out); 
private: 
     vector<point> open; 
     vector<point> closed; 
}; 


#endif 

和apath.cpp:

//apath.cpp

#include "apath.h" 
#include <vector> 
#include <cmath> 



double len(int x1, int y1, int x2, int y2) 
{ 
     return (sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))); 
     //return max(abs(x1-x2), abs(y1-y2)); 

     /*int mx=max(x1,x2)-min(x1,x2); 
     int my=max(y1,y2)-min(y1,y2); 
     return (min(mx,my)*14+max(mx,my)-min(mx,my)*10);*/ 
}; 

bool findPoint(vector<point> arr, point pt, point &ret) 
{ 
    for (int i=0; i<arr.size(); i++) if (arr[i].x==pt.x && arr[i].y==pt.y) { ret=arr[i]; return true;} 
    return '\0'; 
}; 

// GameMap持有地图...

bool path::findPath(int x, int y, int fx, int fy, GameMap& map, BITMAP* out) 
{ 
    while(!open.empty()) open.pop_back(); 
    while(!closed.empty()) closed.pop_back(); 
    while(!way.empty()) way.pop_back(); 

    point start(x,y); 
    start.countF(x,y,fx,fy); 
    open.push_back(start); 

    point finish(fx,fy); 

    double min=999999999; 
    int index; 
    point tmp; 
    point *tmp2; 
    point comparer; 


    while (!findPoint(closed,finish,comparer)) 
    { 
      min=999999999; 
      for (int i=0; i<open.size(); i++) 
      { 
       if(open[i].f<min) 
        { 
         min=open[i].f; 
         index=i; 
        } 
      } 

      tmp=open[index]; 
      closed.push_back(open[index]); 
      open.erase(open.begin()+index); 

      for (int i=0; i<8; i++) 
       { 
        tmp2=new point(tmp.x+directions[i].x,tmp.y+directions[i].y); 
        if (map.getCollisionXY(tmp2->x,tmp2->y)==1 // map.getCollision returns 1 when you cant pass through the tile and 0 otherwise... 
        || findPoint(closed,*tmp2,comparer)) continue; 
        if (!findPoint(open,*tmp2,comparer)) 
        { 
         point newP(tmp.x+directions[i].x,tmp.y+directions[i].y,&closed[closed.size()-1]); 
         newP.countF(x,y,fx,fy); 
         open.push_back(newP); 
        } 
        else 
        { 
         if (comparer.g>tmp.g) 
         { 
          comparer.parent=&closed[closed.size()-1]; 
          comparer.countF(x,y,fx,fy); 
         } 
        } 
        delete tmp2; 
       } 
/*  for (int i=0; i<open.size(); i++) 
     if (open[i].parent!=0)circlefill(out,open[i].x*16+4,open[i].y*16+4,3,0xff0000); 
      if (open.empty()) return false; 
    for (int i=0; i<closed.size(); i++) 
     if (closed[i].parent!=0)circlefill(out,closed[i].x*16+12,closed[i].y*16+12,3,0xffff00);*/ //debug draw 

    } 
    point2d pt; 
    pt.x=finish.x; 
    pt.y=finish.y; 
    way.push_back(pt); 
    point wayer; 
    wayer=closed[closed.size()-1]; 
    while(wayer.parent!=0) //CRASH from here 
    { 
     wayer=*wayer.parent; 
     pt.x=wayer.x; 
     pt.y=wayer.y; 
     way.push_back(pt); 
//   circlefill(out,pt.x*16+12,pt.y*16+12,3,0xffffff); //debug draw 
    } //CRASH to here, i dont exacly know where it is, but when trying to recreate path. 
    return true; 
}; 

一些说明: GameMap与地图 类和它的getCollisionXY返回1,你不能通过瓦和0否则。

Thanx的任何帮助。

+0

我们需要MOOORE! (崩溃细节,调试输出,回溯,应该发生的事情的详细描述,它不是......我们不会研究你的代码) – Vyktor 2012-02-06 13:43:11

+0

你是否设法确定哪部分代码会导致崩溃,例如:通过添加printlns或使用调试器? – DNA 2012-02-06 13:47:44

+0

尝试重新创建路径时崩溃,我将该行评论为//崩溃... – 2012-02-06 13:51:47

回答

1

我看到可能导致访问冲突的一个主要来源是您的观点包含家长作为指针,他们是一个向量的成员。如果向量被重新分配,这些指针可能会失效。

除此之外,很难立即看到造成这次事故的原因。

你真的应该通过调试器来运行你的代码。我刚刚指出了我立即看到的一件事。

+0

太棒了,我会努力的。如果你可以看看在改变父母的部分,当某一点的道路比以前更近时,那会很好。另外我不知道如果是欧洲最好的利尿功能...... – 2012-02-06 16:58:09