1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
//////////
//main.cpp
//////////
#include <vector>
#include <iostream>
#include "AStarNode.h"
bool IsOnList(int xPos, int yPos, std::vector<AStarNode>& list);
std::vector<AStarNode>::iterator FindLowestFCost(std::vector<AStarNode>& open);
void PathFind(int xPos, int yPos, int xDest, int yDest, char* map);
char GetCharOnMap(int xPos, int yPos, char* map);
int main()
{
char map[10 * 10 + 1];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
map[j * 10 + i] = ' ';
}
}
PathFind(9, 9s, 0, 0, map);
map[yPos * 10 + xPos] = 'Z';
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
std::cout << map[j * 10 + i];
}
std::cout << std::endl;
}
std::cin.get();
return 0;
}
char GetCharOnMap(int xPos, int yPos, char* map)
{
if (xPos >= 10 || yPos >= 10 || xPos < 0 || yPos < 0)
return NULL;
else
return map[yPos * 10 + xPos];
}
bool IsOnList(int xPos, int yPos, std::vector<AStarNode>& list)
{
for (std::vector<AStarNode>::iterator i = list.begin(); i != list.end(); i++)
{
if (xPos == i->mXPos && yPos == i->mYPos)
return true;
}
return false;
}
std::vector<AStarNode>::iterator FindLowestFCost(std::vector<AStarNode>& open)
{
std::vector<AStarNode>::iterator lowest = open.begin();
for (std::vector<AStarNode>::iterator i = open.begin(); i != open.end(); i++)
{
if (i->mFCost < lowest->mFCost)
lowest = i;
}
return lowest;
}
void PathFind(int xPos, int yPos, int xDest, int yDest, char* map)
{
std::vector<AStarNode> open;
std::vector<AStarNode> closed;
std::vector<AStarNode> path;
std::vector<AStarNode>::iterator current;
int xDirections[4] = { 0, 0, 1, -1 };
int yDirections[4] = { 1, -1, 0, 0 };
open.push_back(AStarNode(xPos, yPos));
open.begin()->CalculateH(xDest, yDest);
open.begin()->CalculateFCost();
while (true)
{
current = FindLowestFCost(open);
closed.push_back(*current);
open.erase(current);
current = closed.end() - 1;
//std::cout << &(*current) << std::endl; //Prove that it is not being stored properly
//Check adjacent squares
for (int i = 0; i < (sizeof xDirections / sizeof xDirections[0]); i++)
{
char node = GetCharOnMap(current->mXPos + xDirections[i], current->mYPos + yDirections[i], map);
if (node == '|' || node == NULL || IsOnList(current->mXPos + xDirections[i], current->mYPos + yDirections[i], closed) ||
IsOnList(current->mXPos + xDirections[i], current->mYPos + yDirections[i], open)) //If it is on the closed or open list
{
}
else if (!IsOnList(current->mXPos + xDirections[i], current->mYPos + yDirections[i], open))
{
open.push_back(AStarNode(current->mXPos + xDirections[i], current->mYPos + yDirections[i], &(*current)));
open.back().CalculateH(xDest, yDest);
open.back().CalculateFCost();
}
}
if (IsOnList(xDest, yDest, closed) || open.size() == 0) //Check to see if we should stop pathfinding
break;
}
if (IsOnList(xDest, yDest, closed)) //If the destination is on the closed list
{
path.push_back(closed.back()); //Start from our destination and work backwards
while (true) //THIS IS WHERE THE PROBLEM IS
{
if (path.back().mXPos != xPos && path.back().mYPos != yPos && std::find(closed.begin(), closed.end(), *(path.back().mParent)) != closed.end())
{
path.push_back(*(std::find(closed.begin(), closed.end(), *(path.back().mParent)))); //For some reason it isn't tracing all the way back to us
}
else
break;
} //THIS IS WHERE THE PROBLEM ENDS
/*for (std::vector<AStarNode>::const_iterator i = closed.begin(); i != closed.end(); i++) //Displays the closed list
map[i->mYPos * 10 + i->mXPos] = i->mParentDir;*/
for (std::vector<AStarNode>::const_iterator i = path.begin(); i != path.end(); i++) //Display the path
map[i->mYPos * 10 + i->mXPos] = i->mParentDir;
}
}
//////////////
//END main.cpp
//////////////
/////////////
//AStarNode.h
/////////////
#pragma once
#include <iostream>
class AStarNode
{
public:
AStarNode(int xPos, int yPos, AStarNode * parent = nullptr);
void CalculateH(int xDest, int yDest);
void CalculateFCost();
bool operator==(const AStarNode& rhs) const;
bool operator!=(const AStarNode& rhs) const;
private:
void CalculateG();
public:
AStarNode * mParent;
char mParentDir; //Displays a symbol pointing to the parent
int mXPos;
int mYPos;
int mH;
int mG;
int mFCost;
};
AStarNode::AStarNode(int xPos, int yPos, AStarNode * parent) :
mXPos(xPos),
mYPos(yPos),
mParent(parent),
mH(0)
{
CalculateG();
if (mParent != nullptr)
{
if (mParent->mXPos > mXPos)
mParentDir = '<';
else if (mParent->mXPos < mXPos)
mParentDir = '>';
else if (mParent->mYPos > mYPos)
mParentDir = '^';
else if (mParent->mYPos < mYPos)
mParentDir = 'V';
}
else
mParentDir = '|';
}
void AStarNode::CalculateH(int xDest, int yDest)
{
int horizontal = abs(mXPos - xDest);
int vertical = abs(mYPos - yDest);
mH = horizontal + vertical;
}
void AStarNode::CalculateG()
{
if (mParent != nullptr)
mG = mParent->mG + 1;
else
mG = 0;
}
void AStarNode::CalculateFCost()
{
mFCost = mG + mH;
}
bool AStarNode::operator==(const AStarNode& rhs) const
{
if (this->mXPos == rhs.mXPos && this->mYPos == rhs.mYPos)
return true;
else
return false;
}
bool AStarNode::operator!=(const AStarNode& rhs) const
{
if (this->mXPos == rhs.mXPos && this->mYPos == rhs.mYPos)
return false;
else
return true;
}
/////////////////
//END AStarNode.h
/////////////////
|