Advertisement
Guest User

golool

a guest
Aug 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. #include <SFML/Graphics.hpp>
  7. #include <SFML/Graphics/RectangleShape.hpp>
  8. #include <SFML/Graphics/Color.hpp>
  9.  
  10. int width = 190;
  11. int height = 106;
  12. int scale = 10;
  13. int arrSize = width * height;
  14.  
  15. using namespace std;
  16.  
  17. class Cell {
  18.     public:
  19.         int alive;
  20. };
  21.  
  22.  
  23.  
  24. int main() {
  25.     cout << "Arr Size " << arrSize;
  26.     sf::RenderWindow window(sf::VideoMode(width * scale, height * scale), "Grid");
  27.     window.setFramerateLimit(5);
  28.     Cell cells[width][height];
  29.     Cell tempCells[width][height];
  30.    
  31.     //rectangle.setSize(sf::Vector2<float>(scale,scale));
  32.     //rectangle.setPosition(10, 20);
  33.  
  34.     for ( int x = 0; x < width; x++ ) {
  35.         for ( int y = 0; y < height; y++ ) {
  36.             Cell rect = {alive: 0};
  37.             cells[x][y] = rect;
  38.         }
  39.     }
  40.  
  41.     bool playing = false;
  42.  
  43.     // Start the game loop
  44.     while (window.isOpen())
  45.     {
  46.         // Process events
  47.         sf::Event event;
  48.         while (window.pollEvent(event))
  49.         {
  50.             // Close window: exit
  51.             if (event.type == sf::Event::Closed)
  52.                 window.close();
  53.         }
  54.  
  55.         if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
  56.             sf::Vector2i localPosition = sf::Mouse::getPosition(window);
  57.             int x = (int)localPosition.x / scale;
  58.             int y = (int)localPosition.y / scale;
  59.             if(x < width && x > -1 && y < height && y > -1 ) {
  60.                 cells[x][y].alive = 0;
  61.             }
  62.         }
  63.  
  64.         if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
  65.             sf::Vector2i localPosition = sf::Mouse::getPosition(window);
  66.             int x = (int)localPosition.x / scale;
  67.             int y = (int)localPosition.y / scale;
  68.             if(x < width && x > -1 && y < height && y > -1 ) {
  69.                 cells[x][y].alive = 1;
  70.             }
  71.         }
  72.  
  73.         // Clear screen
  74.         window.clear();
  75.         // Draw the string
  76.  
  77.         for ( int x = 0; x < width; x++ ) {
  78.             for ( int y = 0; y < height; y++ ) {
  79.                 tempCells[x][y] = cells[x][y];
  80.             }
  81.         }
  82.  
  83.  
  84.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
  85.             for ( int x = 0; x < width; x++ ) {
  86.                 for ( int y = 0; y < height; y++ ) {
  87.                     tempCells[x][y] = cells[x][y];
  88.                     int neighbours = 0;
  89.  
  90.                     if(cells[x-1][y-1].alive == 1) { neighbours++; }
  91.                     if(cells[x-1][y+0].alive == 1) { neighbours++; }
  92.                     if(cells[x-1][y+1].alive == 1) { neighbours++; }
  93.  
  94.                     if(cells[x+0][y+1].alive == 1) { neighbours++; }
  95.                     if(cells[x+0][y-1].alive == 1) { neighbours++; }
  96.  
  97.                     if(cells[x+1][y-1].alive == 1) { neighbours++; }
  98.                     if(cells[x+1][y+0].alive == 1) { neighbours++; }
  99.                     if(cells[x+1][y+1].alive == 1) { neighbours++; }
  100.  
  101.                     if(neighbours > 0) {
  102.                         cout << "X: " << x << " Y: " << y << " N: " << neighbours << endl;
  103.                     }
  104.                     if(neighbours < 2 || neighbours > 3 && cells[x][y].alive == 1) { tempCells[x][y].alive = 0; }
  105.                     if(neighbours == 3 && cells[x][y].alive == 0)                  { tempCells[x][y].alive = 1; }
  106.                 }
  107.             }
  108.         }
  109.  
  110.         for ( int x = 0; x < width; x++ ) {
  111.             for ( int y = 0; y < height; y++ ) {
  112.                 cells[x][y] = tempCells[x][y];
  113.             }
  114.         }
  115.  
  116.         for ( int x = 2; x < width - 2; x++ ) {
  117.             for ( int y = 2; y < height - 2; y++ ) {
  118.                 Cell rect = cells[x][y];
  119.                
  120.                 sf::RectangleShape rectangle;
  121.  
  122.                 rectangle.setSize( sf::Vector2f( scale, scale ) );
  123.                 rectangle.setPosition( x * scale, y * scale );
  124.                 rectangle.setFillColor( sf::Color( 255 * rect.alive, 255 * rect.alive,  255 * rect.alive ) );
  125.                 rectangle.setOutlineColor( sf::Color( 0, 100, 100) );
  126.                 rectangle.setOutlineThickness(1.0f);
  127.  
  128.                 window.draw( rectangle );
  129.             }
  130.         }
  131.  
  132.         // Update the window
  133.         window.display();
  134.  
  135.     }
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement