diff options
author | zedarider <ymherklotz@gmail.com> | 2016-12-08 11:56:03 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-12-08 11:56:03 +0000 |
commit | 285c050be224a6dcdab5ee36e6ef758913453b02 (patch) | |
tree | 3f07515ad4477eb7852e1f7b28291dae336c5353 | |
parent | dce13d7a63ecf8e2c0ceb4df5a13eee4f0849350 (diff) | |
download | A-star-algorithm-285c050be224a6dcdab5ee36e6ef758913453b02.tar.gz A-star-algorithm-285c050be224a6dcdab5ee36e6ef758913453b02.zip |
updated astar class
-rw-r--r-- | .clang_complete | 1 | ||||
-rwxr-xr-x | bin/main | bin | 272776 -> 272776 bytes | |||
-rw-r--r-- | include/astar.hpp | 48 | ||||
-rw-r--r-- | include/tilemap.hpp (renamed from include/TileMap.hpp) | 0 | ||||
-rw-r--r-- | src/astar.cpp | 37 | ||||
-rw-r--r-- | src/main.cpp | 3 | ||||
-rw-r--r-- | src/tilemap.cpp (renamed from src/TileMap.cpp) | 2 |
7 files changed, 89 insertions, 2 deletions
diff --git a/.clang_complete b/.clang_complete index 30679be..fc62fbd 100644 --- a/.clang_complete +++ b/.clang_complete @@ -1 +1,2 @@ -Iinclude +-I/usr/include Binary files differdiff --git a/include/astar.hpp b/include/astar.hpp new file mode 100644 index 0000000..5e0331b --- /dev/null +++ b/include/astar.hpp @@ -0,0 +1,48 @@ +#ifndef ASTAR_HPP +#define ASTAR_HPP + +#include <ostream> + +#define NEIGHBOUR_NUM 4 + +class AStar { +public: +private: +}; + +class Node { +public: + Node(); + ~Node(); + + // overloading operators for ease of use. + friend bool operator<(const Node& n1, const Node& n2); + friend bool operator==(const Node& n1, const Node& n2); + friend std::ostream& operator<<(std::ostream& out, const Node& n2); + + inline friend bool operator>(const Node& n1, const Node& n2); + inline friend bool operator<=(const Node& n1, const Node& n2); + inline friend bool operator>=(const Node& n1, const Node& n2); + inline friend bool operator!=(const Node& n1, const Node& n2); +private: + // pointer to previous node so that I can backtrack without using recursion. + Node *previous_node; + // pointers to the next nodes of which there are 4 in a grid. + Node *next_nodes[NEIGHBOUR_NUM]; + + // score that is used to compare to other nodes to see if the other path is + // more efficient. + int f_score; + // path length to get to that node. + int g_score; + // heulistic length to destination. + int h_score; + + // see if node has been visited. + bool visited; + + // updates the f_score accordingly. + void update_f_score(); +}; + +#endif diff --git a/include/TileMap.hpp b/include/tilemap.hpp index e47bb22..e47bb22 100644 --- a/include/TileMap.hpp +++ b/include/tilemap.hpp diff --git a/src/astar.cpp b/src/astar.cpp new file mode 100644 index 0000000..39af5fc --- /dev/null +++ b/src/astar.cpp @@ -0,0 +1,37 @@ +#include "astar.hpp" + +Node::Node() : previous_node(NULL), f_score(-1), g_score(0), h_score(-1), visited(false) { + for(unsigned int i = 0; i < NEIGHBOUR_NUM; ++i) + next_nodes[i] = NULL; +} + +Node::~Node() { +} + +bool operator<(const Node& n1, const Node& n2) { + return n1.f_score < n2.f_score; +} + +bool operator==(const Node& n1, const Node& n2) { + return n1.f_score == n2.f_score; +} + +bool operator>(const Node& n1, const Node& n2) { + return !(n1 < n2) && !(n1 == n2); +} + +bool operator<=(const Node& n1, const Node& n2) { + return (n1 < n2) || (n1 == n2); +} + +bool operator>=(const Node& n1, const Node& n2) { + return !(n1 < n2); +} + +bool operator!=(const Node& n1, const Node& n2) { + return !(n1 == n2); +} + +void Node::update_f_score() { + f_score = g_score + h_score; +} diff --git a/src/main.cpp b/src/main.cpp index 89f9f10..e9bb016 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,8 @@ * */ -#include "../include/TileMap.hpp" +#include "tilemap.hpp" +#include "astar.hpp" #include <SFML/Graphics.hpp> diff --git a/src/TileMap.cpp b/src/tilemap.cpp index 21222bf..eaf1657 100644 --- a/src/TileMap.cpp +++ b/src/tilemap.cpp @@ -1,4 +1,4 @@ -#include "../include/TileMap.hpp" +#include "tilemap.hpp" TileMap::TileMap() { } |