diff options
author | ymherklotz <ymherklotz@gmail.com> | 2016-12-09 23:12:20 +0000 |
---|---|---|
committer | ymherklotz <ymherklotz@gmail.com> | 2016-12-09 23:12:20 +0000 |
commit | 879f058f89cd51e87c741025df1b13146e3aef1b (patch) | |
tree | 47e89b00638ec1164267c7914bf300c8de0fcdbc | |
parent | 0c5b64e10694bddf1f5eb904fcd64c4aac81a8a5 (diff) | |
download | A-star-algorithm-879f058f89cd51e87c741025df1b13146e3aef1b.tar.gz A-star-algorithm-879f058f89cd51e87c741025df1b13146e3aef1b.zip |
added separate file for the node class and commented more
-rwxr-xr-x | bin/main | bin | 208440 -> 221936 bytes | |||
-rw-r--r-- | include/astar.hpp | 47 | ||||
-rw-r--r-- | include/node.hpp | 45 | ||||
-rw-r--r-- | include/priority_queue.hpp | 10 | ||||
-rw-r--r-- | include/tilemap.hpp | 2 | ||||
-rw-r--r-- | src/astar.cpp | 41 | ||||
-rw-r--r-- | src/node.cpp | 37 |
7 files changed, 98 insertions, 84 deletions
Binary files differ diff --git a/include/astar.hpp b/include/astar.hpp index 9dde48f..bcff452 100644 --- a/include/astar.hpp +++ b/include/astar.hpp @@ -1,6 +1,9 @@ #ifndef ASTAR_HPP #define ASTAR_HPP +#include "priority_queue.hpp" +#include "node.hpp" + #include <ostream> // defines the number of nodes one can go to @@ -12,46 +15,8 @@ class AStar { public: AStar(); private: + PriorityQueue<Node> open_set; + Node current; }; -class Node { -public: - // TODO These constructors have to change - Node(); - - void set_f(int i) { - f_score = i; - } - - // 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 bool operator>(const Node& n1, const Node& n2); - friend bool operator<=(const Node& n1, const Node& n2); - 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& n); -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. - // We do not need this as we are implementing a priority queue; - //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. - // We don't need this as we will have an open and a closed set - //bool visited; - - // updates the f_score accordingly. - void update_f_score(); -}; - -#endif +#endif // ASTAR_HPP diff --git a/include/node.hpp b/include/node.hpp new file mode 100644 index 0000000..314fc7c --- /dev/null +++ b/include/node.hpp @@ -0,0 +1,45 @@ +#ifndef NODE_HPP +#define NODE_HPP + +#include <ostream> + +class Node { +public: + Node(); + + void set_f(int i) { + f_score = i; + } + + // 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 bool operator>(const Node & n1, const Node & n2); + friend bool operator<=(const Node& n1, const Node& n2); + 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& n); +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. +// We do not need this as we are implementing a priority queue; +//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; + +// the x and y coordinates of the node in the grid + int x, y; + +// see if node has been visited. +// We don't need this as we will have an open and a closed set +//bool visited; +}; + +#endif // NODE_HPP diff --git a/include/priority_queue.hpp b/include/priority_queue.hpp index fe53335..c9fd6f8 100644 --- a/include/priority_queue.hpp +++ b/include/priority_queue.hpp @@ -17,6 +17,9 @@ public: // pops the first element from the queue and removes it while returning // it T pop(); + + // returns the lowest priority item + T get_first(); private: // size of the current queue unsigned int size; @@ -70,6 +73,11 @@ T PriorityQueue<T>::pop() { } template<typename T> +T PriorityQueue<T>::get_first() { + return priority_array[0]; +} + +template<typename T> void PriorityQueue<T>::resize_queue(double factor) { capacity = (double)capacity * factor; T *tmp_array = new T[capacity]; @@ -121,4 +129,4 @@ T PriorityQueue<T>::remove_queue(const unsigned int& loc) { return element; } -#endif +#endif // PRIORITY_QUEUE_HPP diff --git a/include/tilemap.hpp b/include/tilemap.hpp index c2d5c0e..9770cb8 100644 --- a/include/tilemap.hpp +++ b/include/tilemap.hpp @@ -28,4 +28,4 @@ private: sf::Texture m_texture; }; -#endif +#endif // TILEMAP_HPP diff --git a/src/astar.cpp b/src/astar.cpp index f5cabfb..b533130 100644 --- a/src/astar.cpp +++ b/src/astar.cpp @@ -2,44 +2,3 @@ AStar::AStar() { } - -Node::Node() : previous_node(NULL), f_score(-1), g_score(0), h_score(-1) { - update_f_score(); -} - -bool operator<(const Node& n1, const Node& n2) { - if(n1.f_score == n2.f_score) - return n1.h_score < n2.h_score; - 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) { - if(n1.f_score == n2.f_score) - return n1.h_score > n2.h_score; - 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.f_score >= n2.f_score; -} - -bool operator!=(const Node& n1, const Node& n2) { - return !(n1 == n2); -} - -std::ostream& operator<<(std::ostream& out, const Node& n) { - out << n.f_score; - return out; -} - -void Node::update_f_score() { - f_score = g_score + h_score; -} diff --git a/src/node.cpp b/src/node.cpp new file mode 100644 index 0000000..c10aa1b --- /dev/null +++ b/src/node.cpp @@ -0,0 +1,37 @@ +#include "node.hpp" + +Node::Node() : previous_node(NULL), f_score(-1), g_score(0), h_score(-1), x(0), y(0) { +} + +bool operator<(const Node& n1, const Node& n2) { + if(n1.f_score == n2.f_score) + return n1.h_score < n2.h_score; + 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) { + if(n1.f_score == n2.f_score) + return n1.h_score > n2.h_score; + 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.f_score >= n2.f_score; +} + +bool operator!=(const Node& n1, const Node& n2) { + return !(n1 == n2); +} + +std::ostream& operator<<(std::ostream& out, const Node& n) { + out << n.f_score; + return out; +} |