Advertisement
Guest User

Conway's Game of Life C++ SFML

a guest
Mar 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <windows.h>
  3.  
  4. const int width = 1920;
  5. const int height = 1080;
  6. const int xSize = 8;
  7. const int ySize = 8;
  8. const int cols = width / xSize;
  9. const int rows = height / ySize;
  10. int grid[cols][rows];
  11. int newLife[cols][rows];
  12.  
  13. void Start()
  14. {
  15.     srand(time(NULL));
  16.     for (int i = 0; i < cols; i++)
  17.     {
  18.         for (int j = 0; j < rows; j++)
  19.         {
  20.             grid[i][j] = rand() % 2;
  21.         }
  22.     }
  23. }
  24.  
  25. void CalcNewStep()
  26. {
  27.     for (int g = 0; g < cols; g++)
  28.     {
  29.         for (int h = 0; h < rows; h++)
  30.         {
  31.             int neigh = 0;
  32.  
  33.             for (int i = -1; i < 2; i++)
  34.             {
  35.                 for (int j = -1; j < 2; j++)
  36.                 {
  37.                     if (grid[g+i][h+j] == 1)
  38.                     {
  39.                         neigh++;
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             neigh = neigh - grid[g][h];
  45.  
  46.             if (grid[g][h] == 0)
  47.             {
  48.                 if (neigh == 3)
  49.                 {
  50.                     newLife[g][h] = 1;
  51.                 }
  52.                 else
  53.                 {
  54.                     newLife[g][h] = 0;
  55.                 }
  56.             }
  57.  
  58.             if (grid[g][h] == 1)
  59.             {
  60.                 if (neigh == 2 || neigh == 3)
  61.                 {
  62.                     newLife[g][h] = 1;
  63.                 }
  64.  
  65.                 if (neigh > 3 || neigh < 2)
  66.                 {
  67.                     newLife[g][h] = 0;
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     for (int i = 0; i < cols; i++)
  73.     {
  74.         for (int j = 0; j < rows; j++)
  75.         {
  76.             grid[i][j] = newLife[i][j];
  77.         }
  78.     }
  79. }
  80.  
  81. int main()
  82. {
  83.     sf::RenderWindow window(sf::VideoMode(width, height), "Conway's Game of Life");
  84.     Start();
  85.  
  86.     for (int i = 0; i < cols; i++)
  87.     {
  88.         for (int j = 0; j < rows; j++)
  89.         {
  90.             sf::Vertex linesY[] =
  91.             {
  92.                 sf::Vertex(sf::Vector2f(i* xSize, 0), sf::Color(105, 105, 105)),
  93.                 sf::Vertex(sf::Vector2f(i* xSize, height), sf::Color(105, 105, 105))
  94.             };
  95.             window.draw(linesY, 2, sf::Lines);
  96.                 sf::Vertex linesX[] =
  97.             {
  98.                 sf::Vertex(sf::Vector2f(0, j* ySize), sf::Color(105, 105, 105)),
  99.                 sf::Vertex(sf::Vector2f(width, j* ySize), sf::Color(105, 105, 105))
  100.             };
  101.             window.draw(linesX, 2, sf::Lines);
  102.         }
  103.     }
  104.  
  105.     while (window.isOpen())
  106.     {
  107.         sf::Event event;
  108.         while(window.pollEvent(event))
  109.         {
  110.             if(event.type == sf::Event::Closed)
  111.             {
  112.                 window.close();
  113.             }
  114.         }
  115.  
  116.         window.clear(sf::Color::Black);
  117.  
  118.         for (int i = 0; i < cols; i++)
  119.             {
  120.                 for (int j = 0; j < rows; j++)
  121.                 {
  122.                     if (grid[i][j] == 0)
  123.                     {
  124.  
  125.                     }
  126.                     else
  127.                     {
  128.                         sf::RectangleShape rect(sf::Vector2f(xSize, ySize));
  129.                         rect.setFillColor(sf::Color::White);
  130.                         rect.setPosition(i* xSize, j* ySize);
  131.                         window.draw(rect);
  132.                     }
  133.                     sf::Vertex linesY[] =
  134.                     {
  135.                         sf::Vertex(sf::Vector2f(i* xSize, 0), sf::Color(105, 105, 105)),
  136.                         sf::Vertex(sf::Vector2f(i* xSize, height), sf::Color(105, 105, 105))
  137.                     };
  138.                     window.draw(linesY, 2, sf::Lines);
  139.  
  140.                     sf::Vertex linesX[] =
  141.                     {
  142.                         sf::Vertex(sf::Vector2f(0, j* ySize), sf::Color(105, 105, 105)),
  143.                         sf::Vertex(sf::Vector2f(width, j* ySize), sf::Color(105, 105, 105))
  144.                     };
  145.                     window.draw(linesX, 2, sf::Lines);
  146.                 }
  147.             }
  148.  
  149.  
  150.         window.display();
  151.         CalcNewStep();
  152.         Sleep(0);
  153.     }
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement