diff --git a/CMakeLists.txt b/CMakeLists.txt index 38870d8..d07e83b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(labyrinth) set(CMAKE_CXX_STANDARD 17) -add_executable(labyrinth main.cpp position/Position.cpp position/Position.h tile/Tile.cpp tile/Tile.h) +add_executable(labyrinth main.cpp position/Position.cpp position/Position.h tile/Tile.cpp tile/Tile.h labyrinth/Labyrinth.cpp labyrinth/Labyrinth.h) diff --git a/labyrinth/Labyrinth.cpp b/labyrinth/Labyrinth.cpp new file mode 100644 index 0000000..a4d750d --- /dev/null +++ b/labyrinth/Labyrinth.cpp @@ -0,0 +1,53 @@ +// +// Created by draia on 06/02/23. +// + +#include +#include "Labyrinth.h" + +using namespace std; + +random_device dev; +mt19937 rng(dev()); +uniform_int_distribution dist3(0, 2); // 0, 1, 2 + +Labyrinth::Labyrinth(int max_rows, int max_cols) + : m_max_rows(max_rows), m_max_cols(max_cols) +{ + for (int i = 0; i < max_rows; ++i) + { + for (int j = 0; j < max_cols; ++j) + { + auto pos = Position(i, j); + + auto tile = new Tile( + pos, + dist3(rng) == 0 ? WALL : EMPTY + ); + board.insert(pair(pos, tile)); + } + } +} + +Tile *Labyrinth::at(Position pos) +{ + if (0 > pos.x_pos() + || pos.x_pos() >= m_max_rows + || 0 > pos.y_pos() + || pos.y_pos() >= m_max_cols + || board[pos] == nullptr) + { + throw; + } + return board[pos]; +} + +//const Tile *Labyrinth::operator[](Position pos) const +//{ +// return board[pos]; +//} + +Tile *Labyrinth::operator[](Position pos) +{ + return at(pos); +} \ No newline at end of file diff --git a/labyrinth/Labyrinth.h b/labyrinth/Labyrinth.h new file mode 100644 index 0000000..6447328 --- /dev/null +++ b/labyrinth/Labyrinth.h @@ -0,0 +1,32 @@ +// +// Created by draia on 06/02/23. +// + +#ifndef LABYRINTH_LABYRINTH_H +#define LABYRINTH_LABYRINTH_H + + +#include +#include "../position/Position.h" +#include "../tile/Tile.h" + +class Labyrinth +{ +private: + int m_max_rows; + int m_max_cols; + std::map board; + + Tile * at(Position pos); + +public: + Labyrinth(int max_rows, int max_cols); + + + Tile *operator[](Position pos); + +// const Tile *operator[](Position pos) const; +}; + + +#endif //LABYRINTH_LABYRINTH_H diff --git a/main.cpp b/main.cpp index 369f57f..255c53e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,13 @@ #include #include "position/Position.h" #include "tile/Tile.h" +#include "labyrinth/Labyrinth.h" using namespace std; +#define ROWS 30 +#define COLS 30 + int main() { ////////////////////////// @@ -38,7 +42,7 @@ int main() auto t2 = Tile(p2, WALL); - cout << t2.pos() << " -- type: " << t2.getType() << " -- traversable?: " << t2.traversable() << endl; + cout << t2.pos() << " -- type: " << t2.getType() << " -- traversable?: " << t2.traversable() << endl << endl; /* * lack of flexibility in dynamic memory management for Tiles. If we don't know the number of tiles at compile-time, @@ -51,5 +55,22 @@ int main() * practice that */ + ///////////////////////////// + /// labyrinth + ///////////////////////////// + + auto lab = new Labyrinth(ROWS, COLS); + + for (int i = 0; i < ROWS; ++i) + { + for (int j = 0; j < COLS; ++j) + { + auto pos = Position(i, j); + cout << ((*lab)[pos]->traversable() ? "□" : "▨") << " "; + } + cout << endl; + } + + delete lab; return 0; } diff --git a/position/Position.cpp b/position/Position.cpp index 7b6b6f9..31639ac 100644 --- a/position/Position.cpp +++ b/position/Position.cpp @@ -49,3 +49,10 @@ const int &Position::y_pos() const { return m_y_pos; } + +bool Position::operator<(const Position &rhs) const +{ + return m_x_pos < rhs.m_x_pos + || ((m_x_pos == rhs.m_x_pos) + && (m_y_pos < rhs.m_y_pos)); +} diff --git a/position/Position.h b/position/Position.h index 3418cc6..9c7ac33 100644 --- a/position/Position.h +++ b/position/Position.h @@ -27,6 +27,8 @@ public: Position &operator+(const Position &rhs); + bool operator<(const Position &rhs) const; + friend std::ostream &operator<<(std::ostream &os, const Position &pos); };