Skip to content

Instantly share code, notes, and snippets.

@DTSCode
Last active August 29, 2015 14:10
Show Gist options
  • Save DTSCode/4350d08efa47af7339d1 to your computer and use it in GitHub Desktop.
Save DTSCode/4350d08efa47af7339d1 to your computer and use it in GitHub Desktop.
A* implementation in c++ using the standard library
#include <iostream>
#include <array>
#include <utility>
class node {
public:
node(bool=true);
node &operator=(bool);
friend std::ostream &operator<<(std::ostream&, const node&);
private:
bool walkable_;
};
node::node(bool walkable)
: walkable_(walkable) {}
node &node::operator=(bool rhs) {
this->walkable_ = rhs;
}
std::ostream &operator<<(std::ostream &out, const node &square) {
return (out << (square.walkable_ ? "*" : " "));
}
bool operator==(node *lhs, node *rhs) {
return lhs == rhs;
}
int main(int argc, char *argv[]) {
std::array<std::array<node, 7>, 5> map;
std::array<node, 7>::iterator startPoint = &map[2][1];
std::array<node, 7>::iterator endPoint = &map[2][5];
std::array<node, 7>::iterator player = startPoint;
for(auto &column : map) {
for(auto &row : column) {
row = true;
}
}
map[1][3] = false;
map[2][3] = false;
map[3][3] = false;
for(auto column : map) {
for(auto row : column) {
if(row == startPoint or row == endPoint or row == player) {
std::cout << "#";
}
else {
std::cout << row;
}
}
std::cout << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment