Copying from gitlab
This commit is contained in:
106
src/rules.cpp
Normal file
106
src/rules.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* / ) */
|
||||
/* rules.cpp (\__/) ( ( */
|
||||
/* ) ( ) ) */
|
||||
/* By: lejulien <leo.julien.42@gmail.com> ={ }= / / */
|
||||
/* ) `-------/ / */
|
||||
/* Created: 2023/01/09 12:18:36 by lejulien ( / */
|
||||
/* Updated: 2023/01/14 16:47:01 by lejulien \ | */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/rules.hpp"
|
||||
#include "../includes/world.hpp"
|
||||
|
||||
// Contructor
|
||||
|
||||
Rules::Rules() {}
|
||||
|
||||
// Private functions
|
||||
|
||||
void Rules::offset_coord(int &i, int &j) {
|
||||
if (i < 0) {
|
||||
i = this->_width - 1;
|
||||
} else if (i >= this->_width) {
|
||||
i = 0;
|
||||
}
|
||||
if (j < 0) {
|
||||
j = this->_height - 1;
|
||||
} else if (j >= this->_height) {
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool Rules::is_alive(int i, int j) {
|
||||
offset_coord(i, j);
|
||||
if (this->_buffer[i + j * this->_width])
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void Rules::ortho_neighbors(int &neighbors, int i, int j) {
|
||||
// top
|
||||
if (is_alive(i, j - 1))
|
||||
neighbors++;
|
||||
// bottom
|
||||
if (is_alive(i, j + 1))
|
||||
neighbors++;
|
||||
// left
|
||||
if (is_alive(i - 1, j))
|
||||
neighbors++;
|
||||
// right
|
||||
if (is_alive(i + 1, j))
|
||||
neighbors++;
|
||||
}
|
||||
|
||||
void Rules::diag_neighbors(int &neighbors, int i, int j) {
|
||||
// top-left
|
||||
if (is_alive(i - 1, j - 1))
|
||||
neighbors++;
|
||||
// top-right
|
||||
if (is_alive(i + 1, j - 1))
|
||||
neighbors++;
|
||||
// bottom-left
|
||||
if (is_alive(i - 1, j + 1))
|
||||
neighbors++;
|
||||
// bottom-right
|
||||
if (is_alive(i + 1, j + 1))
|
||||
neighbors++;
|
||||
}
|
||||
|
||||
// Member function
|
||||
|
||||
void Rules::setup(World *world) {
|
||||
_world = world;
|
||||
_width = world->getWidth();
|
||||
_height = world->getHeight();
|
||||
}
|
||||
|
||||
void Rules::newWorld(World *world) {
|
||||
_world = world;
|
||||
_width = world->getWidth();
|
||||
_height = world->getHeight();
|
||||
}
|
||||
|
||||
void Rules::update() {
|
||||
int neighbors = 0;
|
||||
std::vector<bool> *world_data = _world->getWorldData();
|
||||
this->_buffer = *world_data;
|
||||
|
||||
// apply the rules for each cells
|
||||
for (int j = 0; j < this->_height; j++) {
|
||||
for (int i = 0; i < this->_width; i++) {
|
||||
// count neighbors
|
||||
neighbors = 0;
|
||||
ortho_neighbors(neighbors, i, j);
|
||||
diag_neighbors(neighbors, i, j);
|
||||
|
||||
// Apply the rules
|
||||
if (neighbors < 2 || neighbors > 3)
|
||||
(*world_data)[i + j * this->_width] = false;
|
||||
else if (neighbors == 3)
|
||||
(*world_data)[i + j * this->_width] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user