Fixed the bug that made walls not receive the algorithm.

main
Zed A. Shaw 2 weeks ago
parent 4d748d1f48
commit ebb5360c5c
  1. 8
      map.cpp
  2. 3
      map.hpp
  3. 45
      tests/map.cpp

@ -5,8 +5,8 @@
using std::vector, std::pair; using std::vector, std::pair;
using namespace fmt; using namespace fmt;
void dump_map(Matrix &map) { void dump_map(const std::string &msg, Matrix &map) {
println("-----------------"); println("----------------- {}", msg);
for(auto row : map) { for(auto row : map) {
for(auto col : row) { for(auto col : row) {
print("{} ", col); print("{} ", col);
@ -40,13 +40,13 @@ Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) {
size_t w = input_map[0].size(); size_t w = input_map[0].size();
// Initialize the new array with every pixel at limit distance // Initialize the new array with every pixel at limit distance
Matrix new_arr = Matrix(h, MatrixRow(w, 1)); // NOTE: this is normally ones() * limit
Matrix new_arr = Matrix(h, MatrixRow(w, limit));
Matrix closed = walls_map; Matrix closed = walls_map;
PairList starting_pixels; PairList starting_pixels;
PairList open_pixels; PairList open_pixels;
limit = limit == 0 ? h * w : limit; limit = limit == 0 ? h * w : limit;
// First pass: Add starting pixels and put them in closed // First pass: Add starting pixels and put them in closed
for(size_t counter = 0; counter < h * w; counter++) { for(size_t counter = 0; counter < h * w; counter++) {
size_t i = counter % w; size_t i = counter % w;

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <utility> #include <utility>
#include <string>
struct Pair { struct Pair {
size_t j = 0; size_t j = 0;
@ -11,7 +12,7 @@ typedef std::vector<Pair> PairList;
typedef std::vector<int> MatrixRow; typedef std::vector<int> MatrixRow;
typedef std::vector<MatrixRow> Matrix; typedef std::vector<MatrixRow> Matrix;
void dump_map(Matrix &map); void dump_map(const std::string &msg, Matrix &map);
void add_neighbors(Matrix &closed, size_t j, size_t i); void add_neighbors(Matrix &closed, size_t j, size_t i);
Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit=0); Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit=0);

@ -1,35 +1,34 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include "map.hpp" #include "map.hpp"
#include <fmt/core.h> #include <fmt/core.h>
#include <nlohmann/json.hpp>
#include <fstream>
using namespace fmt; using namespace fmt;
using namespace nlohmann;
using std::string;
json load_test_data(const string &fname) {
std::ifstream infile(fname);
return json::parse(infile);
}
TEST_CASE("dijkstra algo test", "[map]") { TEST_CASE("dijkstra algo test", "[map]") {
Matrix in_map = { json data = load_test_data("./tests/dijkstra.json");
{1, 1, 1, 0},
{1, 0, 1, 1},
{1, 0, 1, 1},
{1, 1, 1, 1},
};
Matrix walls = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
};
Matrix expected = {
{1, 1, 1, 0},
{1, 0, 1, 1},
{1, 0, 0, 2},
{1, 1, 0, 3},
};
Matrix res = dijkstra_map(in_map, walls); 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);
println("--- EXPECTED:"); if(res != expected) {
dump_map(expected); println("ERROR! ------");
println("--- RESULT:"); dump_map("EXPECTED", expected);
dump_map(res); dump_map("RESULT", res);
}
REQUIRE(res == expected); REQUIRE(res == expected);
} }
}

Loading…
Cancel
Save