diff --git a/Cell.cpp b/Cell.cpp index 5646e31..db535c3 100644 --- a/Cell.cpp +++ b/Cell.cpp @@ -1,4 +1,5 @@ #include +#include #include "Headers/Grid.hpp" #include "Headers/Cell.hpp" @@ -35,6 +36,18 @@ void Cell::setNeighbors(int _neighbors) neighbors = _neighbors; } +void Cell::toggle() +{ + alive = !alive; + render(); +} + +void Cell::kill() +{ + alive = false; + render(); +} + void Cell::update() { diff --git a/Cell.o b/Cell.o index 91673d4..44a41e7 100644 Binary files a/Cell.o and b/Cell.o differ diff --git a/Demo.png b/Demo.png new file mode 100644 index 0000000..cf756c3 Binary files /dev/null and b/Demo.png differ diff --git a/Grid.cpp b/Grid.cpp index 92959c3..6940c1b 100644 --- a/Grid.cpp +++ b/Grid.cpp @@ -64,6 +64,22 @@ void Grid::update() } } +void Grid::clear() +{ + for (int i = 0; i < x; i++) + { + for (int j = 0; j < y; j++) + { + getCell(i, j)->kill(); + } + } +} + +void Grid::toggle(int xpos, int ypos) +{ + getCell(xpos / size, ypos / size)->toggle(); +} + void Grid::render(sf::RenderWindow &window) { for (int i = 0; i < x; i++) @@ -75,9 +91,4 @@ void Grid::render(sf::RenderWindow &window) window.draw(cell->shape); } } -} - -Grid::~Grid() -{ - free(cells); } \ No newline at end of file diff --git a/Grid.o b/Grid.o index 37e5139..b0ce582 100644 Binary files a/Grid.o and b/Grid.o differ diff --git a/Headers/Cell.hpp b/Headers/Cell.hpp index 6943e3a..34758dd 100644 --- a/Headers/Cell.hpp +++ b/Headers/Cell.hpp @@ -11,7 +11,8 @@ class Cell bool alive; void render(); void update(); - + void toggle(); + void kill(); void setNeighbors(int _neighbors); Cell(); diff --git a/Headers/Grid.hpp b/Headers/Grid.hpp index 5250bb0..616d8e9 100644 --- a/Headers/Grid.hpp +++ b/Headers/Grid.hpp @@ -10,10 +10,11 @@ class Grid public: Grid(int _size, int base_x, int base_y, int _x, int _y); - ~Grid(); void update(); void render(sf::RenderWindow &window); + void toggle(int xpos, int ypos); + void clear(); Cell *getCell(int xpos, int ypos); void setNeighbors(int xpos, int ypos); diff --git a/README.md b/README.md new file mode 100644 index 0000000..10fc29d --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# Game of Life + +A simulation based on Conway's Game of Life. +Implemented in C++ using SFML Library. + +![img](Demo.png) + +## Rules + +It's an example of Cellular Automata and follows the following rules: + +- Any cell with 3 or more neighbors will die (Overpopulation). +- Any cell with 2 or less neighbors will die (Underpopulation). +- Any dead cell with 3 neighbors will come alive (Reproduction). + +## Download Instructions + +Download the run file and run it. + +## Controls + +Left Mouse Button - Toggle Cells +P - Pause/Play +C - Clear +R - Randomize +Right Arrow Key - Speed Up +Left Arrow Key - Slow Down diff --git a/main.cpp b/main.cpp index 7c8de6e..dd99b4d 100644 --- a/main.cpp +++ b/main.cpp @@ -43,6 +43,7 @@ int main() // Create Clock sf::Clock clock; + double timeBetweenFrames = 1.0; // Main Loop while (1 == window.isOpen()) { @@ -61,6 +62,36 @@ int main() { paused = !paused; } + if (event.key.code == sf::Keyboard::R) + { + grid = Grid(size, sideBarGap, titleHeight, x, y); + } + if (event.key.code == sf::Keyboard::C) + { + grid.clear(); + } + if (event.key.code == sf::Keyboard::Right) + { + timeBetweenFrames /= 2; + } + if (event.key.code == sf::Keyboard::Left) + { + timeBetweenFrames *= 2; + } + } + + if (event.type == sf::Event::MouseButtonPressed) + { + if (event.mouseButton.button == sf::Mouse::Left) + { + int xpos = event.mouseButton.x; + int ypos = event.mouseButton.y; + + if ((xpos >= sideBarGap) && (xpos <= sideBarGap + width) && (ypos >= titleHeight) && (ypos <= titleHeight + height)) + { + grid.toggle(xpos - sideBarGap, ypos - titleHeight); + } + } } } @@ -69,7 +100,7 @@ int main() // Draw Text window.draw(title); - if (clock.getElapsedTime().asSeconds() > 1.0f && !paused) + if (clock.getElapsedTime().asSeconds() > timeBetweenFrames && !paused) { grid.update(); clock.restart(); diff --git a/main.o b/main.o index 4760099..e9e44cf 100644 Binary files a/main.o and b/main.o differ diff --git a/run b/run index 877cf2d..fd037a9 100755 Binary files a/run and b/run differ