From 7307815e99a79dac42f2a9c06b0fe6171ea11ba0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 1 Jul 2022 16:01:04 +0200 Subject: refactor: use ranges --- src/game/game.cpp | 86 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 40 deletions(-) (limited to 'src/game/game.cpp') diff --git a/src/game/game.cpp b/src/game/game.cpp index a121089..86b6eac 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1,20 +1,23 @@ #include "game.hpp" +#include +#include #include #include #include -#include -#include -#include #include #include +#include +#include +#include +#include #include #include +#include #include "engine/data/bounds.hpp" #include "engine/escape.hpp" #include "engine/graphics/matrix_iterator.hpp" -#include "engine/graphics/matrix_iterator_impl.hpp" #include "engine/keycodes.hpp" #include "errors/RLE_reader.hpp" #include "errors/io.hpp" @@ -24,9 +27,8 @@ #include "interfaces/input.hpp" #include "interfaces/matrix.hpp" #include "interfaces/status_manager.hpp" -#include "util/algorithm_impl.hpp" #include "util/fs.hpp" -#include "util/string_impl.hpp" +#include "util/string.hpp" Game::Game( IStatusLineFactory statusline_factory, @@ -190,7 +192,7 @@ void Game::on_start() scene_matrix->set(col_pos, col); - if (!container_has(_living_cell_positions, col_pos)) + if (!ranges::contains(_living_cell_positions, col_pos)) { _living_cell_positions.push_back(col_pos); } @@ -311,7 +313,7 @@ void Game::_on_normal_mode_update() noexcept } _set_space(matrix, position, ' '); - _living_cell_positions.remove(position); + std::erase(_living_cell_positions, position); break; } @@ -390,38 +392,7 @@ void Game::_on_normal_mode_update() noexcept _last_gen_update_time = time_now; - auto matrix = _scene->get_matrix(); - - const auto dying_cell_positions = container_filter( - _living_cell_positions, - [this](const Vector2 &cell_pos) - { - return _cell_helper->is_cell_dying(cell_pos); - }); - - auto birth_cell_positions = - _cell_helper->get_birth_cell_positions(_living_cell_positions); - - for (const auto &dying_cell_pos : dying_cell_positions) - { - _set_space(matrix, dying_cell_pos, ' '); - - const auto cell_found = container_find(_living_cell_positions, dying_cell_pos); - - if (cell_found != _living_cell_positions.end()) - { - _living_cell_positions.erase(cell_found); - } - } - - for (const auto &birth_cell_pos : birth_cell_positions) - { - if (birth_cell_pos.get_y() >= _minimum_cursor_pos_y) - { - _set_space(matrix, birth_cell_pos, 'x'); - _living_cell_positions.push_back(birth_cell_pos); - } - } + _process_next_generation(); } void Game::_on_command_mode_update() noexcept @@ -640,3 +611,38 @@ void Game::_erase_line_from_cursor() noexcept std::cout.flush(); } +void Game::_process_next_generation() noexcept +{ + const auto dying_cell_positions = + _living_cell_positions | + ranges::views::filter( + [this](const Vector2 &cell_pos) + { + return _cell_helper->is_cell_dying(cell_pos); + }) | + ranges::to(); + + auto birth_cell_positions = + _cell_helper->get_birth_cell_positions(_living_cell_positions); + + auto matrix = _scene->get_matrix(); + + for (const auto &dying_cell_pos : dying_cell_positions) + { + _set_space(matrix, dying_cell_pos, ' '); + + if (ranges::contains(_living_cell_positions, dying_cell_pos)) + { + ranges::actions::remove(_living_cell_positions, dying_cell_pos); + } + } + + for (const auto &birth_cell_pos : birth_cell_positions) + { + if (birth_cell_pos.get_y() >= _minimum_cursor_pos_y) + { + _set_space(matrix, birth_cell_pos, 'x'); + _living_cell_positions.push_back(birth_cell_pos); + } + } +} -- cgit v1.2.3-18-g5258