👔 Implement basic labyrinth, prototype a display func

main
Alexis Drai 2 years ago
parent 211a5ddb84
commit c6cc6403d1

@ -3,4 +3,4 @@ project(labyrinth)
set(CMAKE_CXX_STANDARD 17) 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)

@ -0,0 +1,53 @@
//
// Created by draia on 06/02/23.
//
#include <random>
#include "Labyrinth.h"
using namespace std;
random_device dev;
mt19937 rng(dev());
uniform_int_distribution<std::mt19937::result_type> 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<Position, Tile *>(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);
}

@ -0,0 +1,32 @@
//
// Created by draia on 06/02/23.
//
#ifndef LABYRINTH_LABYRINTH_H
#define LABYRINTH_LABYRINTH_H
#include <map>
#include "../position/Position.h"
#include "../tile/Tile.h"
class Labyrinth
{
private:
int m_max_rows;
int m_max_cols;
std::map<Position, Tile *> 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

@ -1,9 +1,13 @@
#include <iostream> #include <iostream>
#include "position/Position.h" #include "position/Position.h"
#include "tile/Tile.h" #include "tile/Tile.h"
#include "labyrinth/Labyrinth.h"
using namespace std; using namespace std;
#define ROWS 30
#define COLS 30
int main() int main()
{ {
////////////////////////// //////////////////////////
@ -38,7 +42,7 @@ int main()
auto t2 = Tile(p2, WALL); 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, * 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 * 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; return 0;
} }

@ -49,3 +49,10 @@ const int &Position::y_pos() const
{ {
return m_y_pos; 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));
}

@ -27,6 +27,8 @@ public:
Position &operator+(const Position &rhs); Position &operator+(const Position &rhs);
bool operator<(const Position &rhs) const;
friend std::ostream &operator<<(std::ostream &os, const Position &pos); friend std::ostream &operator<<(std::ostream &os, const Position &pos);
}; };

Loading…
Cancel
Save