Skip to content

Instantly share code, notes, and snippets.

@1xch
Last active August 29, 2015 14:26
Show Gist options
  • Save 1xch/9355e4e6a50f08de9bff to your computer and use it in GitHub Desktop.
Save 1xch/9355e4e6a50f08de9bff to your computer and use it in GitHub Desktop.
table of grids that are maps of locations accessed by grid layer or location
#include <cmath>
#include <stdio.h>
#include <iostream>
#include "table.h"
bool Cubic::operator==(const Cubic& o) const {
return (X == o.X && Y == o.Y && Z == o.Z);
}
Cubic Cubic::operator+(const Cubic& o) const {
return {X + o.X, Y + o.Y, Z + o.Z};
}
Cubic Cubic::operator-(const Cubic& o) const {
return {X - o.X, Y - o.Y, Z - o.Z};
}
Cubic Cubic::operator*(int x) const { return {X * x, Y * x, Z * x}; }
int cubicLength(Cubic c) {
return (std::abs(c.X) + std::abs(c.Y) + std::abs(c.Z)) / 2;
};
int cubicDistance(Cubic a, Cubic b) { return cubicLength(a - b); };
Cubic cubicDirections[7] = {
{0, 0, 0},
{1, 0, -1},
{1, -1, 0},
{0, -1, 1},
{-1, 0, 1},
{-1, 1, 0},
{0, 1, -1},
};
Cubic cubicDirection(int d) {
if (d <= 6 && d >= 1) {
return cubicDirections[d];
}
return cubicDirections[0];
};
Cubic Cubic::Neighbor(int d) { return (Cubic{X, Y, Z} + cubicDirection(d)); };
Axial toAxial(Cubic c) { return {c.X, c.Z}; };
Cubic toCubic(Axial a) { return {a.Q, (-a.Q - a.R), a.R}; };
std::string Grid::Tag() { return this->tag_; };
Cubic EmptySpace::Location() { return {0, 0, 0}; }
bool EmptySpace::Open() { return this->open_; }
bool EmptySpace::Blocked() { return this->blocked_; }
int Grid::Bound() { return this->bound_; };
Cubic Grid::Center() { return this->center_; };
void Grid::Init() {
GridMap gm;
int bound = this->Bound();
int num = 0;
Cubic center = this->Center();
for (int x = -bound; x <= bound; x++) {
for (int y = std::max(-bound, -x - bound);
y <= std::min(bound, -x + bound); y++) {
int z = -x - y;
Cubic cb = center + Cubic{x, y, z};
// std::cout << const cb;
EmptySpace* e = new EmptySpace(cb, num++);
gm[cb] = e;
};
};
std::cout << gm.size() << "\n";
}
Space* Grid::Get(Cubic c) { return this->map_[c]; }
std::string Table::Tag() { return this->tag_; };
void Table::SetGrid(Grid* g) { this->grids_.push_back(g); }
Grid* Table::GetGrid(std::string tag) {
Grids g = this->grids_;
for (auto& i : g) {
if (i->Tag() == tag) {
return i;
};
};
};
Grids Table::All() { return this->grids_; }
Spaces Table::Hexagon(Cubic c) {
Spaces ret;
for (auto& i : this->grids_) {
Space* s = i->Get(c);
std::cout << "SPACE:" << s << "\n";
ret.push_back(s);
}
return ret;
}
std::ostream& operator<<(std::ostream& out, Cubic const& c) {
out << "CUBIC(";
out << " X " << c.X;
out << " Y " << c.Y;
out << " Z " << c.Z;
out << ")";
return out;
}
std::ostream& operator<<(std::ostream& out, Grids const& gs) {
out << "Grids(\n";
for (auto& i : gs) {
out << "Grid(\n";
out << " tag " << i->Tag();
out << " bound " << i->Bound();
out << " center " << i->Center();
out << "\n)\n";
// out << i << "\n";
}
out << "\n)\n";
return out;
}
std::ostream& operator<<(std::ostream& out, Grid* const& g) {
out << "Grid(\n";
out << " tag " << g->Tag();
out << " bound " << g->Bound();
out << " center " << g->Center();
out << "\n)\n";
return out;
}
std::ostream& operator<<(std::ostream& out, Table* const& t) {
out << "Table(\n";
out << " tag " << t->Tag() << "\n";
out << " grids " << t->All() << "\n";
out << "\n)\n";
return out;
}
std::ostream& operator<<(std::ostream& out, Spaces const& s) {
for (auto& i : s) {
out << "Space(\n";
out << i->Location();
out << i->Open();
out << i->Blocked();
out << "\n)\n";
}
return out;
}
int main() {
Table* t = new Table("TEST");
Grid* gr = new Grid("TEST-GRID-1", 5, {0, 0, 0});
gr->Init();
t->SetGrid(gr);
Grid* gr2 = new Grid("TEST-GRID-2", 5, {0, 0, 0});
gr2->Init();
t->SetGrid(gr2);
Space* s = gr->Get({1, 1, 1});
std::cout << s; // no matter what rght now, is 0
Spaces h = t->Hexagon({1, 1, 1});
std::cout << h; // faults when run
printf("DONE!\n");
return 0;
}
//#ifndef __X_H_INCLUDED__
//#define __X_H_INCLUDED__
#include <unordered_map>
#include <vector>
class Axial {
public:
int Q, R;
Axial() : Q(0), R(0) {}
Axial(int q, int r) : Q(q), R(r) {}
Axial Neighbor(int);
};
class Cubic {
public:
int X, Y, Z;
Cubic() : X(0), Y(0), Z(0) {}
Cubic(int x, int y, int z) : X(x), Y(y), Z(z) {}
bool operator==(const Cubic&) const;
Cubic operator+(const Cubic&) const;
Cubic operator-(const Cubic&) const;
Cubic operator*(int) const;
Cubic Neighbor(int);
};
Cubic toCubic(Axial a);
Axial toAxial(Cubic c);
struct CubicHasher {
std::size_t operator()(const Cubic& c) const {
using std::size_t;
using std::hash;
return ((hash<int>()(c.X) ^ (hash<int>()(c.Y) << 1)) >> 1) ^
(hash<int>()(c.Z) << 1);
}
};
class Space {
public:
// Space(){};
// virtual ~Space() {}
virtual Cubic Location() = 0;
virtual bool Open() = 0;
virtual bool Blocked() = 0;
};
class EmptySpace : public Space {
int number_;
Cubic location_;
bool open_;
bool blocked_;
public:
EmptySpace(){};
EmptySpace(Cubic c, int num)
: number_(num), location_(c), open_(true), blocked_(false){};
~EmptySpace(){};
Cubic Location();
bool Open();
bool Blocked();
};
typedef std::unordered_map<Cubic, Space*, CubicHasher> GridMap;
class Grid {
std::string tag_;
int bound_;
Cubic center_;
GridMap map_;
public:
std::string Tag();
int Bound();
Cubic Center();
Grid() {}
Grid(std::string t, int b, Cubic c) : tag_(t), bound_(b), center_(c){};
void Init();
Space* Get(Cubic);
};
typedef std::vector<Grid*> Grids;
typedef std::vector<Space*> Spaces;
class Table {
std::string tag_;
Grids grids_;
public:
Table() {}
Table(std::string t) : tag_(t){};
std::string Tag();
Grid* GetGrid(std::string);
void SetGrid(Grid*);
Grids All();
Spaces Hexagon(Cubic);
};
//#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment