Cleaned up the map for more work.
This commit is contained in:
parent
4f863c2635
commit
8b67a25732
5 changed files with 46 additions and 20 deletions
3
Makefile
3
Makefile
|
@ -10,7 +10,8 @@ test: build
|
|||
./builddir/runtests
|
||||
|
||||
run: build test
|
||||
./builddir/roguish
|
||||
powershell "cp ./builddir/roguish.exe ."
|
||||
./roguish
|
||||
|
||||
clean:
|
||||
meson compile --clean -C builddir
|
||||
|
|
8
main.cpp
8
main.cpp
|
@ -41,14 +41,18 @@ int main() {
|
|||
|
||||
auto c = Canvas(100, 100);
|
||||
|
||||
// A triangle following the mouse, using braille characters.
|
||||
Map game_map = Map(input, walls, 1000);
|
||||
|
||||
auto map = Renderer([&] {
|
||||
Matrix result = dijkstra_map(input, walls, 1000);
|
||||
game_map.make_paths();
|
||||
Matrix &result = game_map.paths();
|
||||
|
||||
for(size_t x = 0; x < result[0].size(); ++x) {
|
||||
for(size_t y = 0; y < result.size(); ++y) {
|
||||
auto val = result[y][x];
|
||||
const string tile = val == 1000 ? "#" : fmt::format("{}", result[y][x]);
|
||||
|
||||
|
||||
c.DrawText(22+x*2, 24+y*4, tile);
|
||||
}
|
||||
}
|
||||
|
|
17
map.cpp
17
map.cpp
|
@ -15,8 +15,7 @@ void dump_map(const std::string &msg, Matrix &map) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void add_neighbors(PairList &neighbors, Matrix &closed, size_t j, size_t i) {
|
||||
inline void add_neighbors(PairList &neighbors, Matrix &closed, size_t j, size_t i) {
|
||||
size_t h = closed.size();
|
||||
size_t w = closed[0].size();
|
||||
vector<size_t> rows{j - 1, j, j + 1};
|
||||
|
@ -35,15 +34,15 @@ void add_neighbors(PairList &neighbors, Matrix &closed, size_t j, size_t i) {
|
|||
}
|
||||
}
|
||||
|
||||
Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) {
|
||||
size_t h = input_map.size();
|
||||
size_t w = input_map[0].size();
|
||||
void Map::make_paths() {
|
||||
size_t h = m_input_map.size();
|
||||
size_t w = m_input_map[0].size();
|
||||
|
||||
// Initialize the new array with every pixel at limit distance
|
||||
// NOTE: this is normally ones() * limit
|
||||
limit = limit == 0 ? h * w : limit;
|
||||
int limit = m_limit == 0 ? h * w : m_limit;
|
||||
Matrix new_arr = Matrix(h, MatrixRow(w, limit));
|
||||
Matrix closed = walls_map;
|
||||
Matrix closed = m_walls_map;
|
||||
PairList starting_pixels;
|
||||
PairList open_pixels;
|
||||
|
||||
|
@ -51,7 +50,7 @@ Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) {
|
|||
for(size_t counter = 0; counter < h * w; counter++) {
|
||||
size_t i = counter % w;
|
||||
size_t j = counter / w;
|
||||
if(input_map[j][i] == 0) {
|
||||
if(m_input_map[j][i] == 0) {
|
||||
new_arr[j][i] = 0;
|
||||
closed[j][i] = 1;
|
||||
starting_pixels.push_back({.j=j,.i=i});
|
||||
|
@ -79,5 +78,5 @@ Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) {
|
|||
new_arr[sp.j][sp.i] = counter;
|
||||
}
|
||||
|
||||
return new_arr;
|
||||
m_paths = new_arr;
|
||||
}
|
||||
|
|
22
map.hpp
22
map.hpp
|
@ -15,4 +15,24 @@ typedef std::vector<MatrixRow> Matrix;
|
|||
void dump_map(const std::string &msg, Matrix &map);
|
||||
void add_neighbors(Matrix &closed, size_t j, size_t i);
|
||||
|
||||
Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit=0);
|
||||
class Map {
|
||||
Matrix m_input_map;
|
||||
Matrix m_walls_map;
|
||||
Matrix m_paths;
|
||||
int m_limit = 0;
|
||||
public:
|
||||
|
||||
void make_paths();
|
||||
|
||||
Matrix& paths() { return m_paths; }
|
||||
Matrix& input_map() { return m_input_map; }
|
||||
Matrix& walls() { return m_walls_map; }
|
||||
int limit() { return m_limit; }
|
||||
|
||||
Map(Matrix input_map, Matrix walls_map, int limit) :
|
||||
m_input_map(input_map),
|
||||
m_walls_map(walls_map), m_limit(limit) {
|
||||
}
|
||||
|
||||
Map(Map &map) = delete;
|
||||
};
|
||||
|
|
|
@ -17,18 +17,20 @@ TEST_CASE("dijkstra algo test", "[map]") {
|
|||
json data = load_test_data("./tests/dijkstra.json");
|
||||
|
||||
for(auto &test : data) {
|
||||
Matrix in_map = test["input"];
|
||||
Matrix walls = test["walls"];
|
||||
Matrix expected = test["expected"];
|
||||
int limit = test["limit"];
|
||||
Matrix res = dijkstra_map(in_map, walls, limit);
|
||||
Map map(test["input"],
|
||||
test["walls"],
|
||||
test["limit"]);
|
||||
|
||||
if(res != expected) {
|
||||
map.make_paths();
|
||||
Matrix &paths = map.paths();
|
||||
|
||||
if(paths != expected) {
|
||||
println("ERROR! ------");
|
||||
dump_map("EXPECTED", expected);
|
||||
dump_map("RESULT", res);
|
||||
dump_map("RESULT", paths);
|
||||
}
|
||||
|
||||
REQUIRE(res == expected);
|
||||
REQUIRE(paths == expected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue