aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/game/cell_helper.hpp20
-rw-r--r--src/game/cell_helper_impl.hpp44
-rw-r--r--src/game/game.cpp4
-rw-r--r--src/interfaces/cell_helper.hpp19
-rw-r--r--test/cell_helper_test.cpp37
5 files changed, 39 insertions, 85 deletions
diff --git a/src/game/cell_helper.hpp b/src/game/cell_helper.hpp
index c54b12e..0439269 100644
--- a/src/game/cell_helper.hpp
+++ b/src/game/cell_helper.hpp
@@ -1,33 +1,29 @@
#pragma once
-#include "interfaces/cell_helper.hpp"
-#include "interfaces/matrix.hpp"
-
-#include "engine/data/vector2.hpp"
-
#include <memory>
#include <vector>
+#include "engine/data/vector2.hpp"
+#include "interfaces/cell_helper.hpp"
+#include "interfaces/matrix.hpp"
+
template <typename MatrixElement>
class CellHelper : public ICellHelper
{
public:
explicit CellHelper(std::shared_ptr<IMatrix<MatrixElement>> matrix) noexcept;
- [[nodiscard]] auto is_cell_dying(const Vector2 &cell_pos) const noexcept
+ [[nodiscard]] auto compute_is_cell_dying(const Vector2 &cell_pos) const noexcept
-> bool override;
- [[nodiscard]] auto
- get_birth_cell_positions(const std::vector<Vector2> &cell_positions) const noexcept
- -> std::vector<Vector2> override;
-
- [[nodiscard]] auto find_neighbours(const Vector2 &cell_pos) const noexcept
+ [[nodiscard]] auto compute_birth_cell_positions(
+ const std::vector<Vector2> &cell_positions) const noexcept
-> std::vector<Vector2> override;
private:
const std::shared_ptr<IMatrix<MatrixElement>> _matrix;
- static auto _get_position_neighbours(const Vector2 &position) noexcept
+ [[nodiscard]] auto _get_valid_pos_neighbours(const Vector2 &position) const noexcept
-> std::vector<Vector2>;
};
diff --git a/src/game/cell_helper_impl.hpp b/src/game/cell_helper_impl.hpp
index c03b80c..e2010fa 100644
--- a/src/game/cell_helper_impl.hpp
+++ b/src/game/cell_helper_impl.hpp
@@ -20,11 +20,11 @@ CellHelper<MatrixElement>::CellHelper(
}
template <typename MatrixElement>
-auto CellHelper<MatrixElement>::is_cell_dying(const Vector2 &cell_pos) const noexcept
- -> bool
+auto CellHelper<MatrixElement>::compute_is_cell_dying(
+ const Vector2 &cell_pos) const noexcept -> bool
{
int64_t neighbour_cell_cnt = ranges::count_if(
- find_neighbours(cell_pos),
+ _get_valid_pos_neighbours(cell_pos),
[this](const Vector2 &pos)
{
return _matrix->get(pos) == 'x';
@@ -34,7 +34,7 @@ auto CellHelper<MatrixElement>::is_cell_dying(const Vector2 &cell_pos) const noe
}
template <typename MatrixElement>
-auto CellHelper<MatrixElement>::get_birth_cell_positions(
+auto CellHelper<MatrixElement>::compute_birth_cell_positions(
const std::vector<Vector2> &cell_positions) const noexcept -> std::vector<Vector2>
{
std::vector<Vector2> empty_neighbour_positions =
@@ -43,7 +43,7 @@ auto CellHelper<MatrixElement>::get_birth_cell_positions(
std::vector<Vector2>(),
[this](std::vector<Vector2> acc, const Vector2 &pos)
{
- std::vector<Vector2> neighbours = find_neighbours(pos);
+ std::vector<Vector2> neighbours = _get_valid_pos_neighbours(pos);
auto empty_neighbours =
neighbours | ranges::views::filter(
@@ -62,7 +62,8 @@ auto CellHelper<MatrixElement>::get_birth_cell_positions(
ranges::views::filter(
[this](const Vector2 &cell_pos)
{
- auto neighbours = find_neighbours(cell_pos);
+ auto neighbours =
+ _get_valid_pos_neighbours(cell_pos);
int64_t neighbour_cell_cnt = ranges::count_if(
neighbours,
@@ -78,14 +79,22 @@ auto CellHelper<MatrixElement>::get_birth_cell_positions(
}
template <typename MatrixElement>
-auto CellHelper<MatrixElement>::find_neighbours(const Vector2 &cell_pos) const noexcept
- -> std::vector<Vector2>
+auto CellHelper<MatrixElement>::_get_valid_pos_neighbours(
+ const Vector2 &position) const noexcept -> std::vector<Vector2>
{
+ const auto neighbours = {
+ position + Vector2::up(),
+ position + Vector2::down(),
+ position + Vector2::left(),
+ position + Vector2::right(),
+ position + Vector2::up() + Vector2::left(),
+ position + Vector2::up() + Vector2::right(),
+ position + Vector2::down() + Vector2::left(),
+ position + Vector2::down() + Vector2::right()};
+
const auto matrix_size =
Bounds({.width = _matrix->get_column_cnt(), .height = _matrix->get_row_cnt()});
- const auto neighbours = _get_position_neighbours(cell_pos);
-
return neighbours |
ranges::views::filter(
[&matrix_size](const Vector2 &neighbour_pos)
@@ -95,18 +104,3 @@ auto CellHelper<MatrixElement>::find_neighbours(const Vector2 &cell_pos) const n
}) |
ranges::to<std::vector>();
}
-
-template <typename MatrixElement>
-auto CellHelper<MatrixElement>::_get_position_neighbours(const Vector2 &position) noexcept
- -> std::vector<Vector2>
-{
- return {
- position + Vector2::up(),
- position + Vector2::down(),
- position + Vector2::left(),
- position + Vector2::right(),
- position + Vector2::up() + Vector2::left(),
- position + Vector2::up() + Vector2::right(),
- position + Vector2::down() + Vector2::left(),
- position + Vector2::down() + Vector2::right()};
-}
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 86b6eac..c1bb162 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -618,12 +618,12 @@ void Game::_process_next_generation() noexcept
ranges::views::filter(
[this](const Vector2 &cell_pos)
{
- return _cell_helper->is_cell_dying(cell_pos);
+ return _cell_helper->compute_is_cell_dying(cell_pos);
}) |
ranges::to<std::vector>();
auto birth_cell_positions =
- _cell_helper->get_birth_cell_positions(_living_cell_positions);
+ _cell_helper->compute_birth_cell_positions(_living_cell_positions);
auto matrix = _scene->get_matrix();
diff --git a/src/interfaces/cell_helper.hpp b/src/interfaces/cell_helper.hpp
index 38e214d..bcc0a89 100644
--- a/src/interfaces/cell_helper.hpp
+++ b/src/interfaces/cell_helper.hpp
@@ -1,13 +1,11 @@
#pragma once
-#include "interfaces/matrix.hpp"
-
-#include "engine/data/vector2.hpp"
-
-#include <yacppdic/factory.hpp>
-
#include <memory>
#include <vector>
+#include <yacppdic/factory.hpp>
+
+#include "engine/data/vector2.hpp"
+#include "interfaces/matrix.hpp"
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
class ICellHelper
@@ -15,14 +13,11 @@ class ICellHelper
public:
virtual ~ICellHelper() noexcept = default;
- [[nodiscard]] virtual auto is_cell_dying(const Vector2 &cell_pos) const noexcept
- -> bool = 0;
-
[[nodiscard]] virtual auto
- get_birth_cell_positions(const std::vector<Vector2> &cell_positions) const noexcept
- -> std::vector<Vector2> = 0;
+ compute_is_cell_dying(const Vector2 &cell_pos) const noexcept -> bool = 0;
- [[nodiscard]] virtual auto find_neighbours(const Vector2 &cell_pos) const noexcept
+ [[nodiscard]] virtual auto compute_birth_cell_positions(
+ const std::vector<Vector2> &cell_positions) const noexcept
-> std::vector<Vector2> = 0;
};
diff --git a/test/cell_helper_test.cpp b/test/cell_helper_test.cpp
index 36f2a62..01c26b4 100644
--- a/test/cell_helper_test.cpp
+++ b/test/cell_helper_test.cpp
@@ -84,7 +84,7 @@ TEST_CASE("CellHelper")
.RETURN(' ')
.IN_SEQUENCE(matrix_call_seq);
- REQUIRE(cell_helper.is_cell_dying(cell_pos) == true);
+ REQUIRE(cell_helper.compute_is_cell_dying(cell_pos) == true);
REQUIRE_CALL(*mock_matrix, get(cell_pos_up))
// NOLINTNEXTLINE(readability-identifier-length)
@@ -126,7 +126,7 @@ TEST_CASE("CellHelper")
.RETURN(' ')
.IN_SEQUENCE(matrix_call_seq);
- REQUIRE(cell_helper.is_cell_dying(cell_pos) == false);
+ REQUIRE(cell_helper.compute_is_cell_dying(cell_pos) == false);
REQUIRE_CALL(*mock_matrix, get(cell_pos_up))
// NOLINTNEXTLINE(readability-identifier-length)
@@ -168,37 +168,6 @@ TEST_CASE("CellHelper")
.RETURN(' ')
.IN_SEQUENCE(matrix_call_seq);
- REQUIRE(cell_helper.is_cell_dying(cell_pos) == true);
- }
-
- SECTION("can find neighbours")
- {
- auto mock_matrix = std::make_shared<MockMatrix<char>>();
-
- auto cell_helper = CellHelper<char>(mock_matrix);
-
- constexpr auto MATRIX_ROWS = 12U;
- constexpr auto MATRIX_COLUMNS = 9U;
-
- const auto cell_pos = Vector2({.x = 8, .y = 3});
-
- // NOLINTNEXTLINE(readability-identifier-length)
- REQUIRE_CALL(*mock_matrix, get_row_cnt()).RETURN(MATRIX_ROWS);
-
- // NOLINTNEXTLINE(readability-identifier-length)
- REQUIRE_CALL(*mock_matrix, get_column_cnt()).RETURN(MATRIX_COLUMNS);
-
- const auto living_neighbour_cells = cell_helper.find_neighbours(cell_pos);
-
- REQUIRE(living_neighbour_cells.size() == 5U);
-
- REQUIRE_THAT(
- living_neighbour_cells,
- Catch::Matchers::Contains(std::vector<Vector2>(
- {cell_pos + Vector2::up(),
- cell_pos + Vector2::down(),
- cell_pos + Vector2::left(),
- cell_pos + Vector2::up() + Vector2::left(),
- cell_pos + Vector2::down() + Vector2::left()})));
+ REQUIRE(cell_helper.compute_is_cell_dying(cell_pos) == true);
}
}