aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-12-09 14:12:08 +0000
committerzedarider <ymherklotz@gmail.com>2016-12-09 14:12:08 +0000
commit841d70b4b22fca1d8fa963746bbc0d0f477cccca (patch)
tree683a77f7438cdaff425c7522d8ae954c69628c81
parent285c050be224a6dcdab5ee36e6ef758913453b02 (diff)
downloadA-star-algorithm-841d70b4b22fca1d8fa963746bbc0d0f477cccca.tar.gz
A-star-algorithm-841d70b4b22fca1d8fa963746bbc0d0f477cccca.zip
last commit
-rwxr-xr-xbin/mainbin272776 -> 202752 bytes
-rw-r--r--include/astar.hpp13
-rw-r--r--src/astar.cpp18
-rw-r--r--src/main.cpp66
-rw-r--r--src/main.cpp.window83
5 files changed, 114 insertions, 66 deletions
diff --git a/bin/main b/bin/main
index b760066..3f32396 100755
--- a/bin/main
+++ b/bin/main
Binary files differ
diff --git a/include/astar.hpp b/include/astar.hpp
index 5e0331b..5a379fb 100644
--- a/include/astar.hpp
+++ b/include/astar.hpp
@@ -13,17 +13,18 @@ private:
class Node {
public:
Node();
+ Node(Node *prev_node);
+ Node(Node *prev_node, int g);
~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);
+ 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;
diff --git a/src/astar.cpp b/src/astar.cpp
index 39af5fc..b5c424e 100644
--- a/src/astar.cpp
+++ b/src/astar.cpp
@@ -3,6 +3,19 @@
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;
+ update_f_score();
+}
+
+Node::Node(Node *prev_node) : previous_node(prev_node), f_score(-1), g_score(0), h_score(-1), visited(false) {
+ for(unsigned int i = 0; i < NEIGHBOUR_NUM; ++i)
+ next_nodes[i] = NULL;
+ update_f_score();
+}
+
+Node::Node(Node *prev_node, int g) : previous_node(prev_node), f_score(-1), g_score(g), h_score(-1), visited(false) {
+ for(unsigned int i = 0; i < NEIGHBOUR_NUM; ++i)
+ next_nodes[i] = NULL;
+ update_f_score();
}
Node::~Node() {
@@ -32,6 +45,11 @@ 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/main.cpp b/src/main.cpp
index e9bb016..5b950ea 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -18,66 +18,12 @@
using namespace std;
int main(int argc, char *argv[]) {
- cout << "executing " << argv[0] << endl;
- cout << "arguments given: " << argc - 1 << endl;
-
- // create a render window of size 800x600 with a title.
- sf::RenderWindow window(sf::VideoMode(800, 600), "A* Algorithm");
-
- // print out the window size.
- cout << "window size: " << window.getSize().x << ", " << window.getSize().y << endl;
-
- // set the constants int the file that define the grid to be displayed.
- const int tile_size = 21;
- const int rows = 41;
- const int cols = 34;
-
- // print out that information.
- cout << "tile size: " << tile_size << "px, rows: " << rows << ", cols: " <<
- cols << endl;
-
- // create the array of the right size using the constants.
- int tiles[cols * rows];
-
- // assign zeros to the tiles.
- for(int i = 0; i < cols * rows; ++i)
- tiles[i] = 0;
-
- // create a tile map that will be used to display the array.
- TileMap map;
-
- // event loop that runs the window.
- while(window.isOpen()) {
- // create an event.
- sf::Event event;
- // check if an event has been triggered.
- while(window.pollEvent(event))
- // if the event is the window closing, close the window.
- if(event.type == sf::Event::Closed)
- window.close();
-
- // check if mouse buttons are pressed.
- if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
- // get the position of the mouse.
- sf::Vector2i mouse = sf::Mouse::getPosition(window);
- // set the tile colour to 1.
- tiles[(int)(mouse.x / tile_size) + cols * (int)(mouse.y / tile_size)] = 1;
- } else if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
- sf::Vector2i mouse = sf::Mouse::getPosition(window);
- // set the tile colour to 2.
- tiles[(int)(mouse.x / tile_size) + cols * (int)(mouse.y / tile_size)] = 2;
- }
-
- // update tile map with the correct array.
- map.load("res/GridTileTexture3.png", sf::Vector2f(200, 200), sf::Vector2f(tile_size, tile_size), tiles, cols, rows);
-
- // clear the screen.
- window.clear(sf::Color(47, 47, 47));
- // draw the map onto the screen.
- window.draw(map);
- // display the window.
- window.display();
- }
+ Node n1(NULL, 5);
+ Node n2(NULL, 2);
+ cout << "n1 > n2: " << (n1 > n2) << endl;
+ cout << "n1 < n2: " << (n1 < n2) << endl;
+ cout << "n1 <= n2: " << (n1 <= n2) << endl;
+ cout << "n1 >= n2: " << (n1 >= n2) << endl;
return 0;
}
diff --git a/src/main.cpp.window b/src/main.cpp.window
new file mode 100644
index 0000000..e9bb016
--- /dev/null
+++ b/src/main.cpp.window
@@ -0,0 +1,83 @@
+/*
+ *
+ * description: Displays the A* Algorithm on a grid.
+ *
+ * author: Yann Herklotz <ymherklotz@gmail.com>
+ * date created: DD-MM-YYYY
+ *
+ */
+
+#include "tilemap.hpp"
+#include "astar.hpp"
+
+#include <SFML/Graphics.hpp>
+
+#include <iostream>
+#include <cstdlib>
+
+using namespace std;
+
+int main(int argc, char *argv[]) {
+ cout << "executing " << argv[0] << endl;
+ cout << "arguments given: " << argc - 1 << endl;
+
+ // create a render window of size 800x600 with a title.
+ sf::RenderWindow window(sf::VideoMode(800, 600), "A* Algorithm");
+
+ // print out the window size.
+ cout << "window size: " << window.getSize().x << ", " << window.getSize().y << endl;
+
+ // set the constants int the file that define the grid to be displayed.
+ const int tile_size = 21;
+ const int rows = 41;
+ const int cols = 34;
+
+ // print out that information.
+ cout << "tile size: " << tile_size << "px, rows: " << rows << ", cols: " <<
+ cols << endl;
+
+ // create the array of the right size using the constants.
+ int tiles[cols * rows];
+
+ // assign zeros to the tiles.
+ for(int i = 0; i < cols * rows; ++i)
+ tiles[i] = 0;
+
+ // create a tile map that will be used to display the array.
+ TileMap map;
+
+ // event loop that runs the window.
+ while(window.isOpen()) {
+ // create an event.
+ sf::Event event;
+ // check if an event has been triggered.
+ while(window.pollEvent(event))
+ // if the event is the window closing, close the window.
+ if(event.type == sf::Event::Closed)
+ window.close();
+
+ // check if mouse buttons are pressed.
+ if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
+ // get the position of the mouse.
+ sf::Vector2i mouse = sf::Mouse::getPosition(window);
+ // set the tile colour to 1.
+ tiles[(int)(mouse.x / tile_size) + cols * (int)(mouse.y / tile_size)] = 1;
+ } else if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
+ sf::Vector2i mouse = sf::Mouse::getPosition(window);
+ // set the tile colour to 2.
+ tiles[(int)(mouse.x / tile_size) + cols * (int)(mouse.y / tile_size)] = 2;
+ }
+
+ // update tile map with the correct array.
+ map.load("res/GridTileTexture3.png", sf::Vector2f(200, 200), sf::Vector2f(tile_size, tile_size), tiles, cols, rows);
+
+ // clear the screen.
+ window.clear(sf::Color(47, 47, 47));
+ // draw the map onto the screen.
+ window.draw(map);
+ // display the window.
+ window.display();
+ }
+
+ return 0;
+}