aboutsummaryrefslogtreecommitdiff
path: root/src/game/game.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-01 16:01:04 +0200
committerHampusM <hampus@hampusmat.com>2022-07-01 16:01:04 +0200
commit7307815e99a79dac42f2a9c06b0fe6171ea11ba0 (patch)
treead41ee819dc87fc2653caf720fa7d1df30c0caeb /src/game/game.cpp
parent2bff8c999edde11270ecaf6fbd2d24f54d0e360b (diff)
refactor: use ranges
Diffstat (limited to 'src/game/game.cpp')
-rw-r--r--src/game/game.cpp86
1 files changed, 46 insertions, 40 deletions
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 <cstdlib>
+#include <filesystem>
#include <fmt/color.h>
#include <fmt/core.h>
#include <fmt/format.h>
-#include <yacppdic/factory.hpp>
-#include <cstdlib>
-#include <filesystem>
#include <iostream>
#include <iterator>
+#include <range/v3/action/remove.hpp>
+#include <range/v3/algorithm/contains.hpp>
+#include <range/v3/range/conversion.hpp>
+#include <range/v3/view/filter.hpp>
#include <stdexcept>
#include <utility>
+#include <yacppdic/factory.hpp>
#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<std::vector>();
+
+ 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);
+ }
+ }
+}