From 0fca054fdc11dba086541e2d5a48f6f1406925dd Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 1 Jul 2022 16:27:15 +0200 Subject: refactor: clarify cell helper method names --- src/game/cell_helper.hpp | 20 ++++++++----------- src/game/cell_helper_impl.hpp | 44 ++++++++++++++++++------------------------ src/game/game.cpp | 4 ++-- src/interfaces/cell_helper.hpp | 19 +++++++----------- test/cell_helper_test.cpp | 37 +++-------------------------------- 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 #include +#include "engine/data/vector2.hpp" +#include "interfaces/cell_helper.hpp" +#include "interfaces/matrix.hpp" + template class CellHelper : public ICellHelper { public: explicit CellHelper(std::shared_ptr> 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 &cell_positions) const noexcept - -> std::vector override; - - [[nodiscard]] auto find_neighbours(const Vector2 &cell_pos) const noexcept + [[nodiscard]] auto compute_birth_cell_positions( + const std::vector &cell_positions) const noexcept -> std::vector override; private: const std::shared_ptr> _matrix; - static auto _get_position_neighbours(const Vector2 &position) noexcept + [[nodiscard]] auto _get_valid_pos_neighbours(const Vector2 &position) const noexcept -> std::vector; }; 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::CellHelper( } template -auto CellHelper::is_cell_dying(const Vector2 &cell_pos) const noexcept - -> bool +auto CellHelper::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::is_cell_dying(const Vector2 &cell_pos) const noe } template -auto CellHelper::get_birth_cell_positions( +auto CellHelper::compute_birth_cell_positions( const std::vector &cell_positions) const noexcept -> std::vector { std::vector empty_neighbour_positions = @@ -43,7 +43,7 @@ auto CellHelper::get_birth_cell_positions( std::vector(), [this](std::vector acc, const Vector2 &pos) { - std::vector neighbours = find_neighbours(pos); + std::vector neighbours = _get_valid_pos_neighbours(pos); auto empty_neighbours = neighbours | ranges::views::filter( @@ -62,7 +62,8 @@ auto CellHelper::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::get_birth_cell_positions( } template -auto CellHelper::find_neighbours(const Vector2 &cell_pos) const noexcept - -> std::vector +auto CellHelper::_get_valid_pos_neighbours( + const Vector2 &position) const noexcept -> std::vector { + 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::find_neighbours(const Vector2 &cell_pos) const n }) | ranges::to(); } - -template -auto CellHelper::_get_position_neighbours(const Vector2 &position) noexcept - -> std::vector -{ - 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(); 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 - #include #include +#include + +#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 &cell_positions) const noexcept - -> std::vector = 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 &cell_positions) const noexcept -> std::vector = 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>(); - - auto cell_helper = CellHelper(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( - {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); } } -- cgit v1.2.3-18-g5258