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_impl.hpp | 44 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'src/game/cell_helper_impl.hpp') 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()}; -} -- cgit v1.2.3-18-g5258