Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <windows.h>
- const int width = 1920;
- const int height = 1080;
- const int xSize = 8;
- const int ySize = 8;
- const int cols = width / xSize;
- const int rows = height / ySize;
- int grid[cols][rows];
- int newLife[cols][rows];
- void Start()
- {
- srand(time(NULL));
- for (int i = 0; i < cols; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- grid[i][j] = rand() % 2;
- }
- }
- }
- void CalcNewStep()
- {
- for (int g = 0; g < cols; g++)
- {
- for (int h = 0; h < rows; h++)
- {
- int neigh = 0;
- for (int i = -1; i < 2; i++)
- {
- for (int j = -1; j < 2; j++)
- {
- if (grid[g+i][h+j] == 1)
- {
- neigh++;
- }
- }
- }
- neigh = neigh - grid[g][h];
- if (grid[g][h] == 0)
- {
- if (neigh == 3)
- {
- newLife[g][h] = 1;
- }
- else
- {
- newLife[g][h] = 0;
- }
- }
- if (grid[g][h] == 1)
- {
- if (neigh == 2 || neigh == 3)
- {
- newLife[g][h] = 1;
- }
- if (neigh > 3 || neigh < 2)
- {
- newLife[g][h] = 0;
- }
- }
- }
- }
- for (int i = 0; i < cols; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- grid[i][j] = newLife[i][j];
- }
- }
- }
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(width, height), "Conway's Game of Life");
- Start();
- for (int i = 0; i < cols; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- sf::Vertex linesY[] =
- {
- sf::Vertex(sf::Vector2f(i* xSize, 0), sf::Color(105, 105, 105)),
- sf::Vertex(sf::Vector2f(i* xSize, height), sf::Color(105, 105, 105))
- };
- window.draw(linesY, 2, sf::Lines);
- sf::Vertex linesX[] =
- {
- sf::Vertex(sf::Vector2f(0, j* ySize), sf::Color(105, 105, 105)),
- sf::Vertex(sf::Vector2f(width, j* ySize), sf::Color(105, 105, 105))
- };
- window.draw(linesX, 2, sf::Lines);
- }
- }
- while (window.isOpen())
- {
- sf::Event event;
- while(window.pollEvent(event))
- {
- if(event.type == sf::Event::Closed)
- {
- window.close();
- }
- }
- window.clear(sf::Color::Black);
- for (int i = 0; i < cols; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- if (grid[i][j] == 0)
- {
- }
- else
- {
- sf::RectangleShape rect(sf::Vector2f(xSize, ySize));
- rect.setFillColor(sf::Color::White);
- rect.setPosition(i* xSize, j* ySize);
- window.draw(rect);
- }
- sf::Vertex linesY[] =
- {
- sf::Vertex(sf::Vector2f(i* xSize, 0), sf::Color(105, 105, 105)),
- sf::Vertex(sf::Vector2f(i* xSize, height), sf::Color(105, 105, 105))
- };
- window.draw(linesY, 2, sf::Lines);
- sf::Vertex linesX[] =
- {
- sf::Vertex(sf::Vector2f(0, j* ySize), sf::Color(105, 105, 105)),
- sf::Vertex(sf::Vector2f(width, j* ySize), sf::Color(105, 105, 105))
- };
- window.draw(linesX, 2, sf::Lines);
- }
- }
- window.display();
- CalcNewStep();
- Sleep(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement