2012-02-06 37 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