-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.cpp
134 lines (117 loc) · 4.58 KB
/
GameOfLife.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "GameOfLife.h"
#include <catch2/catch_test_macros.hpp>
#include <algorithm>
namespace ranges = std::ranges;
TEST_CASE("Game state generated") {
static constexpr GameState<3, 3> initial_state{};
SECTION("Standard") {
static constexpr auto final_state = NextState<initial_state, 0>::value;
REQUIRE(initial_state == final_state);
}
SECTION("Variable template") {
static constexpr auto final_state = NextState_v<initial_state, 0>;
REQUIRE(initial_state == final_state);
}
}
TEST_CASE("Default game state") {
static constexpr GameState<3, 3> state{};
constexpr auto is_false = [](bool b) { return !b; };
REQUIRE(ranges::all_of(state.grid, is_false));
}
TEST_CASE("Set game state") {
static constinit GameState<3, 3> state{};
ranges::fill(state.grid, true);
constexpr auto is_true = [](bool b) { return b; };
REQUIRE(ranges::all_of(state.grid, is_true));
}
TEST_CASE("3x3, 1 alive, 1 step") {
static constexpr GameState<3, 3> initial_state{
true, false, false,
false, false, false,
false, false, false,
};
static constexpr GameState<3, 3> expected_state{};
static constexpr auto actual_state = NextState_v<initial_state, 1>;
REQUIRE(expected_state == actual_state);
}
TEST_CASE("4x4, 1 alive, 1 step") {
static constexpr GameState<4, 4> initial_state{
true, false, false, false,
false, false, false, false,
false, false, false, false
};
static constexpr GameState<4, 4> expected_state{};
static constexpr auto actual_state = NextState_v<initial_state, 1>;
REQUIRE(expected_state == actual_state);
}
//TEST_CASE("5x5, 3 alive (Blinker)") {
// static constexpr GameState<5, 5> initial_state{
// false, false, false, false, false,
// false, true, true, true, false,
// false, false, false, false, false,
// };
// SECTION("One step") {
// static constexpr GameState<5, 5> expected_state{
// false, false, true, false, false,
// false, false, true, false, false,
// false, false, true, false, false,
// };
// static constexpr auto actual_state = NextState_v<initial_state, 1>;
// REQUIRE(expected_state == actual_state);
// }
// SECTION("Two step") {
// static constexpr GameState<5, 5> expected_state{
// false, false, false, false, false,
// false, true, true, true, false,
// false, false, false, false, false,
// };
// static constexpr auto actual_state = NextState_v<initial_state, 2>;
// REQUIRE(expected_state == actual_state);
// }
//}
TEST_CASE("Get adjacent neighbors [3x3]") {
static constexpr GameState<3, 3> state{};
SECTION("(1, 1)") {
static constexpr auto center = Coordinates{ 1, 1 };
static constinit auto expected_coords = AdjacentCoords{ {
{ 0, 0 }, { 0, 1 }, { 0, 2 },
{ 1, 0 }, /*{1, 1}*/{ 1, 2 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }
}};
static constinit auto actual_coords = Adjacents_v<state, center>;
ranges::sort(expected_coords);
ranges::sort(actual_coords);
REQUIRE(expected_coords == actual_coords);
}
SECTION("(0, 1) [wraparound north]") {
static constexpr auto center = Coordinates{ 0, 1 };
static constinit auto expected_coords = AdjacentCoords{ {
{ 2, 0 }, { 2, 1 }, { 2, 2 },
{ 0, 0 }, /*{0, 1}*/{ 0, 2 },
{ 1, 0 }, { 1, 1 }, { 1, 2 }
} };
static constinit auto actual_coords = Adjacents_v<state, center>;
ranges::sort(expected_coords);
ranges::sort(actual_coords);
REQUIRE(expected_coords == actual_coords);
}
SECTION("(2, 1) [wraparound south]") {
static constexpr auto center = Coordinates{ 2, 1 };
static constinit auto expected_coords = AdjacentCoords{ {
{ 1, 0 }, { 1, 1 }, { 1, 2 },
{ 2, 0 }, /*{2, 1}*/{ 2, 2 },
{ 0, 0 }, { 0, 1 }, { 0, 2 }
} };
static constinit auto actual_coords = Adjacents_v<state, center>;
ranges::sort(expected_coords);
ranges::sort(actual_coords);
REQUIRE(expected_coords == actual_coords);
}
// TODO: Add mores cases
// TODO: Rename directions to use cardinal directions (i.e., N, S, SE, etc.)
// TODO: Refactor row wrap to require just max rows and row. No depend on game state
// TODO: Add unit tests for row wrap
// TODO: Add unit tests for directions
// TODO: Add tests directory to project structure
// TODO: Add demo app for the game of life
}