aboutsummaryrefslogtreecommitdiff
path: root/src/world.h
blob: 72cc32f5b7bed8d7ca0d00cf8d1894a7b1bf0684 (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
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
135
136
137
138
139
140
141
142
#ifndef LIFE_SRC_WORLD_H_
#define LIFE_SRC_WORLD_H_

#include <array>
#include <stack>
#include <tuple>
#include <cstdint>

namespace life {

template<
	std::size_t WIDTH,
	std::size_t HEIGHT
>
class World {
	public:
		World() {
			for ( std::size_t j = 0; j < HEIGHT; j++ ) {
				for ( std::size_t i = 0; i < WIDTH; i++ ) {
					this->matrix_[j][i] = false;
				}
			}
		}

		bool isLifeAt(std::ptrdiff_t x, std::ptrdiff_t y) const {
			if ( x >= 0      &&
			     x <  WIDTH  &&
			     y >= 0      &&
			     y <  HEIGHT ) {
				return this->matrix_[y][x];
			} else {
				return false; // end of world is dead
			}
		}

		std::uint8_t lifeDensityAt(std::size_t x, std::size_t y) const {
			std::uint8_t counter = 0;

			if ( this->isLifeAt(x - 1, y) ) {
				counter++;
			}
			if ( this->isLifeAt(x + 1, y) ) {
				counter++;
			}
			if ( this->isLifeAt(x, y - 1) ) {
				counter++;
			}
			if ( this->isLifeAt(x, y + 1) ) {
				counter++;
			}
			if ( this->isLifeAt(x - 1, y + 1) ) {
				counter++;
			}
			if ( this->isLifeAt(x - 1, y - 1) ) {
				counter++;
			}
			if ( this->isLifeAt(x + 1, y + 1) ) {
				counter++;
			}
			if ( this->isLifeAt(x + 1, y - 1) ) {
				counter++;
			}

			return counter;
		}

		std::size_t getAge() const {
			return this->age_;
		}

		void summonLifeAt(std::size_t x, std::size_t y) {
			if ( x >= 0      &&
			     x <  WIDTH  &&
			     y >= 0      &&
			     y <  HEIGHT ) {
				this->matrix_[y][x] = true;
			}
		}

		void extinguishLifeAt(std::size_t x, std::size_t y) {
			if ( x >= 0      &&
			     x <  WIDTH  &&
			     y >= 0      &&
			     y <  HEIGHT ) {
				this->matrix_[y][x] = false;
			}
		}

		void tick() {
			this->age_++;

			std::stack<std::tuple<std::size_t, std::size_t, bool>> updates;

			for ( std::size_t j = 0; j < HEIGHT; j++ ) {
				for ( std::size_t i = 0; i < WIDTH; i++ ) {
					const std::uint8_t d = this->lifeDensityAt(i, j);

					if ( this->matrix_[j][i] ) {
						if ( d < 2 ) {
							updates.push(std::make_tuple(i, j, false));	
						} else if ( d == 2 || d == 3 ) {
							updates.push(std::make_tuple(i, j, true));	
						} else if ( d > 3 ) {
							updates.push(std::make_tuple(i, j, false));	
						}
					} else {
						if ( d == 3 ) {
							updates.push(std::make_tuple(i, j, true));	
						}
					}
				}
			}

			while ( !updates.empty() ) {
				const auto& update = updates.top();

				if ( std::get<2>(update) ) {
					this->summonLifeAt(
						std::get<0>(update),
						std::get<1>(update)
					);
				} else {
					this->extinguishLifeAt(
						std::get<0>(update),
						std::get<1>(update)
					);
				}
				
				updates.pop();
			}
		}

	private:
		std::size_t age_ = 0;

		std::array<std::array<bool, WIDTH>, HEIGHT> matrix_;

};

}

#endif  // LIFE_SRC_WORLD_H_