diff options
| author | Bobby <[email protected]> | 2023-09-11 23:22:50 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-09-11 23:22:50 -0400 |
| commit | 2876b8b38f699b3062230a7b570b45cc7399716e (patch) | |
| tree | 16044732c690de0b92cdbd1f91135825b3f5c299 /src/gameOfLife.cpp | |
| download | gameoflife-2876b8b38f699b3062230a7b570b45cc7399716e.tar.xz gameoflife-2876b8b38f699b3062230a7b570b45cc7399716e.zip | |
Added Game of Life
Diffstat (limited to 'src/gameOfLife.cpp')
| -rw-r--r-- | src/gameOfLife.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/gameOfLife.cpp b/src/gameOfLife.cpp new file mode 100644 index 0000000..976d4ae --- /dev/null +++ b/src/gameOfLife.cpp @@ -0,0 +1,40 @@ +#include "game.h" + +int main() { + Game game; + GameScreen screen; + + // create random points + for (auto &row : game.display) { + std::generate(row.begin(), row.end(), zeroOrOne); + } + + // game loop + for(;;) { + // check for alive cells + for (int i = 0; i < SCREEN_WIDTH; ++i) { + for (int j = 0; j < SCREEN_HEIGHT; ++j) { + // check if the cell will be alive or dead in the next generation + game.swap[i][j] = isAlive(game.display, i, j) ? 1 : 0; + } + } + + // draw + for (int i = 0; i < SCREEN_WIDTH; ++i) { + for (int j = 0; j < SCREEN_HEIGHT; ++j) { + if (game.swap[i][j] == 1) { + screen.drawPixel(i, j); + } + } + } + + // swap buffers + std::copy(game.swap.begin(), game.swap.end(), game.display.begin()); + + // display to screen + screen.update(); + SDL_Delay(20); + screen.input(); + screen.clearPixels(); + } +} |
