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 ++-- 3 files changed, 29 insertions(+), 39 deletions(-) (limited to 'src/game') 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(); -- cgit v1.2.3-18-g5258