HasanRasulov

GameOfLife.cpp

Oct 25th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. //============================================================================
  2. // Name : Game.cpp
  3. // Author : Hasan Rasulov
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include<random>
  11. #include<array>
  12. #include<SFML/Graphics.hpp>
  13. using namespace std;
  14.  
  15. enum class State
  16. :size_t {
  17. dead = 0, alive = 1
  18. };
  19.  
  20. constexpr size_t w = 600;
  21. constexpr size_t h = 400;
  22. constexpr size_t res = 5;
  23. constexpr size_t cols = w / res;
  24. constexpr size_t rows = h / res;
  25. using Grid=array<array<State,rows>,cols>;
  26.  
  27. size_t get_count(Grid& grid, size_t x, size_t y) {
  28.  
  29. size_t count = 0;
  30.  
  31. for (int i = -1; i < 2; ++i)
  32.  
  33. for (int j = -1; j < 2; j++)
  34.  
  35. count += static_cast<size_t>(grid[(x + i + cols) % cols][(y + j
  36. + rows) % rows]);
  37.  
  38. count -= static_cast<size_t>(grid[x][y]);
  39.  
  40. return count;
  41. }
  42.  
  43. Grid generate(Grid& old_grid) {
  44.  
  45. Grid new_grid;
  46.  
  47. for (size_t i = 0; i < cols; ++i) {
  48.  
  49. for (size_t j = 0; j < rows; j++) {
  50.  
  51. size_t count_neighbours = get_count(old_grid, i, j);
  52.  
  53. if (old_grid[i][j] == State::dead && count_neighbours == 3) {
  54. new_grid[i][j] = State::alive;
  55. } else if (old_grid[i][j] == State::alive
  56. && (count_neighbours < 2 || count_neighbours > 3)) {
  57. new_grid[i][j] = State::dead;
  58. } else {
  59. new_grid[i][j] = old_grid[i][j];
  60. }
  61.  
  62. }
  63. }
  64.  
  65. return new_grid;
  66. }
  67.  
  68. int main() {
  69.  
  70. sf::RenderWindow window(sf::VideoMode(w, h), "Game of Life",
  71. sf::Style::Default);
  72.  
  73. sf::RectangleShape zero(sf::Vector2f(res, res));
  74. zero.setFillColor(sf::Color::White);
  75. sf::RectangleShape one(sf::Vector2f(res, res));
  76. one.setFillColor(sf::Color::Black);
  77.  
  78. Grid grid;
  79.  
  80. std::random_device rd;
  81. mt19937 gen(rd());
  82. std::bernoulli_distribution dist(0.1);
  83.  
  84. for (size_t i = 0; i < cols; ++i) {
  85.  
  86. for (size_t j = 0; j < rows; j++) {
  87.  
  88. grid[i][j] = dist(gen) ? State::alive : State::dead;
  89.  
  90. }
  91. }
  92.  
  93. while (window.isOpen()) {
  94.  
  95. sf::Event e;
  96.  
  97. while (window.pollEvent(e)) {
  98.  
  99. if (e.type == sf::Event::Closed)
  100. window.close();
  101.  
  102. }
  103.  
  104. for (size_t i = 0; i < cols; ++i) {
  105.  
  106. for (size_t j = 0; j < rows; j++) {
  107.  
  108. if (grid[i][j] == State::alive) {
  109.  
  110. one.setPosition(i * res, j * res);
  111. window.draw(one);
  112.  
  113. } else {
  114.  
  115. zero.setPosition(i * res, j * res);
  116. window.draw(zero);
  117. }
  118.  
  119. }
  120. }
  121.  
  122. grid = generate(grid);
  123. window.display();
  124.  
  125. }
  126.  
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment