Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Network.hpp>
  5. #include <random>
  6.  
  7. using namespace std;
  8.  
  9. class Matrix
  10. {
  11. public:
  12.     Matrix(sf::RenderWindow &win, sf::Vector2f origin, float immunity)
  13.     {
  14.         //window pointer takes address of window parameter to matrix constructor
  15.         window = &win;
  16.  
  17.         //take origin position as constructor parameter
  18.         originPos = origin;
  19.  
  20.         //set up text
  21.         font.loadFromFile("REFSAN.ttf");
  22.         text.setFont(font);
  23.         text.setString("hello, hello, default text.. CHANGE ME");
  24.         text.setCharacterSize(10);
  25.         text.setStyle(sf::Text::Bold);
  26.         text.setPosition(origin.x + 150, origin.y + 50);
  27.  
  28.         //create the circle objects which make up matrix
  29.         //init positions and colours
  30.         for (int i = 0; i < 1000; i += 10)
  31.         {
  32.             sf::CircleShape circle(5);
  33.  
  34.             //init positions
  35.             //offset by origin x,y values
  36.             int xOffset = origin.x;
  37.             int yOffset = origin.y;
  38.  
  39.             if (i < 100)
  40.                 circle.setPosition(xOffset + (i + 10), yOffset + 20);
  41.             if (i >= 100 && i < 200)
  42.                 circle.setPosition(xOffset + ((i - 100) + 10), yOffset + 30);
  43.             if (i >= 200 && i < 300)
  44.                 circle.setPosition(xOffset + ((i - 200) + 10), yOffset + 40);
  45.             if (i >= 300 && i < 400)
  46.                 circle.setPosition(xOffset + ((i - 300) + 10), yOffset + 50);
  47.             if (i >= 400 && i < 500)
  48.                 circle.setPosition(xOffset + ((i - 400) + 10), yOffset + 60);
  49.             if (i >= 500 && i < 600)
  50.                 circle.setPosition(xOffset + ((i - 500) + 10), yOffset + 70);
  51.             if (i >= 600 && i < 700)
  52.                 circle.setPosition(xOffset + ((i - 600) + 10), yOffset + 80);
  53.             if (i >= 700 && i < 800)
  54.                 circle.setPosition(xOffset + ((i - 700) + 10), yOffset + 90);
  55.             if (i >= 800 && i < 900)
  56.                 circle.setPosition(xOffset + ((i - 800) + 10), yOffset + 100);
  57.             if (i >= 900 && i < 1000)
  58.                 circle.setPosition(xOffset + ((i - 900) + 10), yOffset + 110);
  59.  
  60.             //set colours..
  61.             circle.setFillColor(sf::Color(200, 200, 200));
  62.  
  63.             //put each circle in vector container
  64.             circleVec.push_back(circle);
  65.         }
  66.  
  67.             //Random Utility
  68.             std::random_device dev;
  69.             std::mt19937 rng(dev());
  70.             std::uniform_int_distribution<std::mt19937::result_type> dist(0, 10); // distribution in range [1,10]
  71.             //to get a random number, for example write: int x = rng(dist)
  72.  
  73.             //set up immune cells
  74.             //x is threshold of immunity under which cells will be immunised
  75.             immunityThreshold = immunity;
  76.             for (int i = 0; i < 100; ++i)
  77.             {
  78.                 int r = dist(rng);
  79.                 float f = r / 10.0;
  80.                 if (f < immunityThreshold)
  81.                 {
  82.                     circleVec[i].setFillColor(sf::Color::Green);
  83.                 }
  84.             }
  85.  
  86.             //set up random location where 1st infected cell appears
  87.             std::uniform_int_distribution<std::mt19937::result_type> dist2(0, 100); // distribution in range [1,10]
  88.             int randInfElement = dist2(rng);
  89.             sf::Color green = sf::Color::Green;
  90.             //don't start the infected cell on an immunised guy
  91.             if (circleVec[randInfElement].getFillColor() == green)
  92.             {
  93.                 while (circleVec[randInfElement].getFillColor() == green)
  94.                 {
  95.                     randInfElement = dist2(rng);
  96.                 }
  97.                 circleVec[randInfElement].setFillColor(sf::Color::Cyan);
  98.             }
  99.             else
  100.                 circleVec[randInfElement].setFillColor(sf::Color::Cyan);
  101.            
  102.  
  103.             //end constructor
  104.         }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.     //Put this function just after the while(window.isopen()){} statement, before the events
  111.     void RunInfection()
  112.     {
  113.         //Check adjacent elements to infected elements, put the elements to infect in vector to process later
  114.         for (int i = 0; i < circleVec.size(); ++i)
  115.         {
  116.             if (circleVec[i].getFillColor() == sf::Color::Cyan)
  117.             {
  118.                 //cout << "Colour is cyan!\n";
  119.  
  120.                 //infect to right
  121.                 if (i < 99) //don't access vector if i is 99+
  122.                 {
  123.                     if (circleVec[i + 1].getFillColor() != sf::Color::Cyan && circleVec[i + 1].getFillColor() != sf::Color::Green)
  124.                     {
  125.                         //In this case, don't check neighbours to the right
  126.                         if (i != 9 && i != 19 && i != 29 && i != 39 && i != 49 && i != 59 && i != 69 && i != 79 && i != 89 && i != 99)
  127.                         {
  128.                             //cout << "i in infect right loop == " << i << endl;
  129.                             elemsToInfec.push_back(i + 1);
  130.                         }
  131.                     }
  132.                 }
  133.  
  134.  
  135.  
  136.                 //infect to left
  137.                 if (i > 0)
  138.                 {
  139.                     if (circleVec[i - 1].getFillColor() != sf::Color::Cyan && circleVec[i - 1].getFillColor() != sf::Color::Green)
  140.                     {
  141.                         //In this case, don't check neighbours to the left.
  142.                         if (i % 10 == 0 || i == 0)
  143.                         {
  144.                             //do nothing
  145.                         }
  146.                         else
  147.                             elemsToInfec.push_back(i - 1);
  148.                     }
  149.                 }
  150.  
  151.  
  152.                 //infect above
  153.                 //In this case, don't check neighbours above
  154.                 if (i > 9)
  155.                 {
  156.                     if (circleVec[i - 10].getFillColor() != sf::Color::Cyan && circleVec[i - 10].getFillColor() != sf::Color::Green)
  157.                     {
  158.                         if (i >= 0 && i <= 9)
  159.                         {
  160.                             //do nothing
  161.                             //circleVec[i].setFillColor(sf::Color::Red);
  162.                         }
  163.                         else
  164.                             elemsToInfec.push_back(i - 10);
  165.                     }
  166.                 }
  167.  
  168.  
  169.                 //infect below
  170.                 //In this case, don't check neighbours below
  171.                 if (i < 90)
  172.                 {
  173.                     if (circleVec[i + 10].getFillColor() != sf::Color::Cyan && circleVec[i + 10].getFillColor() != sf::Color::Green)
  174.                     {
  175.                         if (i >= 90 && i <= 99)
  176.                         {
  177.                             //do nothing
  178.                             //circleVec[i].setFillColor(sf::Color::Red);
  179.                         }
  180.                         else
  181.                         {
  182.                             //cout << "elem-" << i << endl;
  183.                             //cout << "elem to infect == " << i + 10 << endl;
  184.                             elemsToInfec.push_back(i + 10);
  185.                         }
  186.                     }
  187.                 }
  188.             }
  189.         }
  190.  
  191.         //Colour the infected elements with cyan
  192.         for (int x : elemsToInfec)
  193.         {
  194.             circleVec[x].setFillColor(sf::Color::Cyan);
  195.         }
  196.  
  197.  
  198.         //display text showing information about matrix
  199.         int inf = 0;
  200.         for (auto x : circleVec)
  201.         {
  202.             if (x.getFillColor() == sf::Color::Cyan)
  203.                 ++inf;
  204.         }
  205.         text.setString("Number infected: " + to_string(inf) + ", number uninfected: " + to_string(100 - inf) + "\n\nImmunity threshold: " + to_string(immunityThreshold));
  206.         //text.setString("testing.. testing");
  207.     }
  208.  
  209.     //If up/down keys pressed, scroll the matrices up and down the screen by changing their origins
  210.     void UpdateOrigin(int x, int y, int numMatricesDown) //num matrices down keeps the appropriate offset between matrices
  211.     {
  212.         if (numMatricesDown == 0)
  213.         {
  214.             sf::Vector2f newOrigin = sf::Vector2f(x, y);
  215.             originPos = newOrigin;
  216.         }
  217.         else
  218.         {
  219.             sf::Vector2f newOrigin = sf::Vector2f(x, y + (numMatricesDown*200));
  220.             originPos = newOrigin;
  221.         }
  222.  
  223.         //init positions
  224.         //offset by origin x,y values
  225.         int xOffset = originPos.x;
  226.         int yOffset = originPos.y;
  227.  
  228.         for (int i = 0; i < 1000; i += 10)
  229.         {
  230.             if (i < 100)
  231.                 circleVec[i/10].setPosition(xOffset + (i + 10), yOffset + 20);
  232.             if (i >= 100 && i < 200)
  233.                 circleVec[i/10].setPosition(xOffset + ((i - 100) + 10), yOffset + 30);
  234.             if (i >= 200 && i < 300)
  235.                 circleVec[i/10].setPosition(xOffset + ((i - 200) + 10), yOffset + 40);
  236.             if (i >= 300 && i < 400)
  237.                 circleVec[i/10].setPosition(xOffset + ((i - 300) + 10), yOffset + 50);
  238.             if (i >= 400 && i < 500)
  239.                 circleVec[i/10].setPosition(xOffset + ((i - 400) + 10), yOffset + 60);
  240.             if (i >= 500 && i < 600)
  241.                 circleVec[i/10].setPosition(xOffset + ((i - 500) + 10), yOffset + 70);
  242.             if (i >= 600 && i < 700)
  243.                 circleVec[i/10].setPosition(xOffset + ((i - 600) + 10), yOffset + 80);
  244.             if (i >= 700 && i < 800)
  245.                 circleVec[i/10].setPosition(xOffset + ((i - 700) + 10), yOffset + 90);
  246.             if (i >= 800 && i < 900)
  247.                 circleVec[i/10].setPosition(xOffset + ((i - 800) + 10), yOffset + 100);
  248.             if (i >= 900 && i < 1000)
  249.                 circleVec[i/10].setPosition(xOffset + ((i - 900) + 10), yOffset + 110);
  250.         }
  251.  
  252.         //change text position
  253.         text.setPosition(originPos.x + 150, originPos.y + 50);
  254.     }
  255.  
  256.  
  257.     //put this between window.clear() and window.display()
  258.     void Draw()
  259.     {
  260.         window->draw(text);
  261.         for (auto & c : circleVec)
  262.         {
  263.             window->draw(c);
  264.         }
  265.     }
  266.  
  267.  
  268.    
  269. private:
  270.     sf::RenderWindow* window;
  271.     sf::Font font;
  272.     sf::Text text;
  273.  
  274.     vector<sf::CircleShape> circleVec; //vector to hold the circles which make up the matrix
  275.     sf::Vector2f originPos; //position of the "origin", from which relative position of circles in matrix grid will be based
  276.  
  277.     float immunityThreshold; //what percentage of cells will be immune
  278.  
  279.     vector<int> elemsToInfec; //element numbers to spread infection to
  280.  
  281.    
  282.  
  283. };
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294. int main()
  295. {
  296.     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Infection Sim");
  297.     //good offset for successive origins? 300 too high. use 200
  298.     //sf::Vector2f origin(0, 0);
  299.     //sf::Vector2f origin2(0, 200);
  300.     //sf::Vector2f origin3(0, 400);
  301.  
  302.     //Matrix m1(window, origin, 0.5);
  303.     //Matrix m2(window, origin2, 0.5);
  304.     //Matrix m3(window, origin3, 0.5);
  305.  
  306.  
  307.  
  308.  
  309.  
  310.     //dynamically create matrices
  311.     //get input for number iterations, which immunity threshold to use
  312.     int numIter = 5;
  313.     float threshold = 0.5;
  314.  
  315.     //put n number origin positions in origin vector, offset (0, n*200)
  316.     vector<sf::Vector2f> originVec;
  317.     for (int i = 0; i < numIter; ++i)
  318.     {
  319.         sf::Vector2f orig = sf::Vector2f(0, i * 200);
  320.         originVec.push_back(orig);
  321.     }
  322.  
  323.  
  324.     //create Matrix objects
  325.     vector<Matrix> matrixVec;
  326.     for (int i = 0; i < numIter; ++i)
  327.     {
  328.         Matrix ma(window, originVec[i], threshold);
  329.         matrixVec.push_back(ma);
  330.     }
  331.  
  332.    
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.     //clock for slowly moving matrices
  342.     sf::Clock clock;
  343.     float upMovement = 0;
  344.  
  345.     while(window.isOpen())
  346.     {
  347.         //m1.RunInfection();
  348.         //m2.RunInfection();
  349.         //m3.RunInfection();
  350.  
  351.    
  352.         for (auto & m : matrixVec)
  353.         {
  354.             m.RunInfection();
  355.         }
  356.    
  357.  
  358.  
  359.     sf::Event event;
  360.     while (window.pollEvent(event))
  361.     {
  362.         if (event.type == sf::Event::Closed)
  363.             window.close();
  364.     }
  365.  
  366.  
  367.     auto deltaTime = clock.restart().asSeconds();
  368.     //cout << deltaTime << endl;
  369.  
  370.     //check input
  371.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  372.     {
  373.         upMovement += deltaTime;
  374.         //cout << upMovement << endl;
  375.    
  376.  
  377.         //m1.UpdateOrigin(0, -upMovement * 50, 0);
  378.         //m2.UpdateOrigin(0, -upMovement * 50, 1);
  379.         //m3.UpdateOrigin(0, -upMovement * 50, 2);
  380.  
  381.        
  382.         for (int i = 0; i < matrixVec.size(); ++i)
  383.         {
  384.             matrixVec[i].UpdateOrigin(0, -upMovement * 200, i);
  385.         }
  386.        
  387.     }
  388.  
  389.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  390.     {
  391.         upMovement -= deltaTime;
  392.         //cout << upMovement << endl;
  393.        
  394.         //.UpdateOrigin(0, -upMovement * 50, 0);
  395.         //.UpdateOrigin(0, -upMovement * 50, 1);
  396.         //.UpdateOrigin(0, -upMovement * 50, 2);
  397.  
  398.        
  399.         for (int i = 0; i < matrixVec.size(); ++i)
  400.         {
  401.             matrixVec[i].UpdateOrigin(0, -upMovement * 200, i);
  402.         }
  403.        
  404.     }
  405.  
  406.     window.clear();
  407.  
  408.    
  409.  
  410.     //m1.Draw();
  411.     //m2.Draw();
  412.     //m3.Draw();
  413.  
  414.    
  415.     for (auto & m : matrixVec)
  416.     {
  417.         m.Draw();
  418.     }
  419.    
  420.    
  421.  
  422.    
  423.  
  424.     window.display();
  425. }
  426.  
  427.  
  428.  
  429. return 0;
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement