aboutsummaryrefslogtreecommitdiff
path: root/src/game/cell_helper.tpp
blob: ebb35aa5bfb94bc6f1950dc8a70e041d2d500c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include "cell_helper.hpp"

#include "util/algorithm.hpp"

template <typename MatrixElement>
constexpr auto has_matrix_value(
	const IMatrix<MatrixElement> &matrix,
	const MatrixElement &value) noexcept
{
	return [&matrix, &value](const Vector2 &pos)
	{
		return matrix.get(pos) == value;
	};
}

template <typename MatrixElement>
CellHelper<MatrixElement>::CellHelper(const IMatrix<MatrixElement> &matrix) noexcept
	: _matrix(matrix)
{
}

template <typename MatrixElement>
auto CellHelper<MatrixElement>::is_cell_dying(const Vector2 &cell_pos) const noexcept
	-> bool
{
	const auto neighbour_cell_positions =
		container_filter(find_neighbours(cell_pos), has_matrix_value(_matrix, 'x'));

	const auto neighbour_cell_cnt = neighbour_cell_positions.size();

	return neighbour_cell_cnt < 2 || neighbour_cell_cnt >= 4;
}

template <typename MatrixElement>
auto CellHelper<MatrixElement>::get_birth_cell_positions(
	const std::list<Vector2> &cell_positions) const noexcept -> std::list<Vector2>
{
	auto all_empty_neighbour_positions = std::list<Vector2>();

	for (const auto &cell_pos : cell_positions)
	{
		const std::list<Vector2> empty_neighbour_positions =
			container_filter(find_neighbours(cell_pos), has_matrix_value(_matrix, ' '));

		all_empty_neighbour_positions.insert(
			all_empty_neighbour_positions.end(),
			empty_neighbour_positions.begin(),
			empty_neighbour_positions.end());
	}

	// Remove duplicates
	all_empty_neighbour_positions.sort();
	all_empty_neighbour_positions.unique();

	auto birth_cell_positions = container_filter(
		all_empty_neighbour_positions,
		[this](const Vector2 &cell_pos)
		{
			const auto neighbour_cell_positions = container_filter(
				find_neighbours(cell_pos),
				has_matrix_value(_matrix, 'x'));

			return neighbour_cell_positions.size() == 3;
		});

	return birth_cell_positions;
}

template <typename MatrixElement>
auto CellHelper<MatrixElement>::find_neighbours(const Vector2 &cell_pos) const noexcept
	-> std::list<Vector2>
{
	std::list<Vector2> cell_positions = {};

	const auto matrix_size =
		Bounds({.width = _matrix.get_column_cnt(), .height = _matrix.get_row_cnt()});

	const auto neighbours = _get_position_neighbours(cell_pos);

	for (const auto &neighbour_pos : neighbours)
	{
		if (matrix_size.validate_coords(neighbour_pos) == CoordsValidation::VALID)
		{
			cell_positions.push_back(neighbour_pos);
		}
	}

	return cell_positions;
}

template <typename MatrixElement>
auto CellHelper<MatrixElement>::_get_position_neighbours(
	const Vector2 &position) noexcept -> std::list<Vector2>
{
	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()};
}