Advertisement
cartagenae

main.cpp

Apr 18th, 2025 (edited)
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. // Include important C++ libraries here
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/Audio.hpp>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <vector>
  7.  
  8. //Make the code easier to type with "using namespace"
  9. using namespace sf;
  10. using namespace std;
  11.  
  12. // Generate random number between 1 and 3 inclusive
  13. int GenerateRandomNumber()
  14. {
  15.     int randomNumber = (rand() % 5) + 1;            // change the size here
  16.     return randomNumber - 1;
  17. }
  18.  
  19. // Calulate the midpoint between the user point and the designated random point
  20. Vector2f GenerateMidpoint(Vector2f userPoint, Vector2f randomPoint)
  21. {
  22.     Vector2f midPoint;
  23.  
  24.     midPoint.x = (userPoint.x + randomPoint.x) / 2;
  25.     midPoint.y = (userPoint.y + randomPoint.y) / 2;
  26.  
  27.     return midPoint;
  28. }
  29.  
  30. int main()
  31. {
  32.     // Create a video mode object
  33.     VideoMode vm(1920, 1080);
  34.     // Create and open a window for the game
  35.     RenderWindow window(vm, "Chaos Game!!", Style::Default);
  36.    
  37.     vector<Vector2f> vertices;
  38.     vector<Vector2f> points;
  39.  
  40.     Font font;
  41.  
  42.     if(!font.loadFromFile("/usr/share/fonts/truetype/ubuntu/UbuntuSans[wdth,wght].ttf"))
  43.     {
  44.         cout << "Unable to load UbuntuSans from memory" << endl;
  45.         return -1;
  46.     }
  47.  
  48.     Text userText("Please complete your triangle", font, 24);
  49.     userText.setFillColor(Color::White);
  50.     userText.setPosition(150, 50);
  51.  
  52.     int randomVertexPoint = GenerateRandomNumber();
  53.     int previousRandomVertexPoint = (vertices.size() == 5) ? randomVertexPoint : GenerateRandomNumber();
  54.  
  55.     srand(time(NULL));
  56.  
  57.     // for(int i = 0; i < 5; i++)
  58.     // {
  59.     //  randomVertexPoint = GenerateRandomNumber();
  60.     //  cout << randomVertexPoint << endl;
  61.     // }
  62.  
  63.     while (window.isOpen())
  64.     {
  65.         /*
  66.         ****************************************
  67.         Handle the players input
  68.         ****************************************
  69.         */
  70.  
  71.         Event event;
  72.         while (window.pollEvent(event))
  73.         {
  74.             if (event.type == Event::Closed)
  75.             {
  76.                 // Quit the game when the window is closed
  77.                 window.close();
  78.             }
  79.             if (event.type == sf::Event::MouseButtonPressed)
  80.             {
  81.                 if (event.mouseButton.button == sf::Mouse::Left)
  82.                 {
  83.                     std::cout << "the left button was pressed" << std::endl;
  84.                     std::cout << "mouse x: " << event.mouseButton.x << std::endl;
  85.                     std::cout << "mouse y: " << event.mouseButton.y << std::endl;
  86.        
  87.                     if(vertices.size() < 5) // change the size here
  88.                     {
  89.                         vertices.push_back(Vector2f(event.mouseButton.x, event.mouseButton.y));
  90.                     }
  91.                     else if(points.size() == 0)
  92.                     {
  93.                         ///fourth click
  94.                         ///push back to points vector
  95.                         points.push_back(Vector2f(event.mouseButton.x, event.mouseButton.y));
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.         if (Keyboard::isKeyPressed(Keyboard::Escape))
  101.         {
  102.             window.close();
  103.         }
  104.         /*
  105.         ****************************************
  106.         Update
  107.         ****************************************
  108.         */
  109.    
  110.         if(points.size() > 0)
  111.         {
  112.             ///generate more point(s)
  113.             ///select random vertex
  114.             ///calculate midpoint between random vertex and the last point in the vector
  115.             ///push back the newly generated coord.
  116.             Vector2f midPoint;
  117.             // Reminder: GenerateRandomNumber() generates random number between 1 and 3 inclusive
  118.             while(points.size() < 20000)
  119.             {
  120.                 randomVertexPoint = GenerateRandomNumber();
  121.                 if(vertices.size() == 4)
  122.                 {
  123.                     while(randomVertexPoint == previousRandomVertexPoint)
  124.                     {
  125.                         randomVertexPoint = GenerateRandomNumber();
  126.                     }
  127.                 }
  128.                 else if(vertices.size() == 5)
  129.                 {
  130.                     // previousRandomVertexPoint = randomVertexPoint;
  131.                     int sum = (previousRandomVertexPoint + 2) % 5;
  132.                     int difference = (((previousRandomVertexPoint - 2) % 5) + 5) % 5;
  133.                     while(randomVertexPoint == sum || randomVertexPoint == difference)
  134.                     {
  135.                         randomVertexPoint = GenerateRandomNumber();
  136.  
  137.                         cout << endl;
  138.                         cout << "sum: " << sum << endl;
  139.                         cout << "difference: " << difference << endl;
  140.                         cout << "randomVertexPoint: " << randomVertexPoint << endl;
  141.                         cout << endl;
  142.                     }
  143.                     cout << "done iterating randomVertexPoint" << endl;
  144.                     cout << "sum: " << sum << endl;
  145.                     cout << "difference: " << difference << endl;
  146.                     cout << "randomVertexPoint: " << randomVertexPoint << endl;
  147.                     // cout << endl;
  148.                     // cout << "sum: " << sum << endl;
  149.                     // cout << "difference: " << difference << endl;
  150.                     // cout << "randomVertexPoint: " << randomVertexPoint << endl;
  151.                     // cout << endl;
  152.                 }
  153.                 if(vertices.size() > 3)
  154.                 {
  155.                     previousRandomVertexPoint = randomVertexPoint;
  156.                 }
  157.                
  158.                 // cout << "\nrandomVertexPoint:\n";
  159.                 // cout << "------------------\n";
  160.                 // cout << randomVertexPoint << "\n\n";
  161.  
  162.                 midPoint = GenerateMidpoint(points[points.size() - 1], vertices[randomVertexPoint]);
  163.  
  164.                 // cout << "midPoint:\n";
  165.                 // cout << "---------\n";
  166.                 // cout << "(" << midPoint.x << ", " << midPoint.y << ")\n";
  167.  
  168.                 // cout << "\nvertices[" << randomVertexPoint << "]:\n";
  169.                 // cout << "------------\n";
  170.                 // cout << "(" << vertices[randomVertexPoint].x << ", " << vertices[randomVertexPoint].y << ")\n";
  171.  
  172.                 points.push_back(midPoint);
  173.             }
  174.         }
  175.    
  176.         /*
  177.         ****************************************
  178.         Draw
  179.         ****************************************
  180.         */
  181.         window.clear();
  182.         for(size_t i = 0; i < vertices.size(); i++)
  183.         {
  184.             RectangleShape rect(Vector2f(10,10));
  185.             rect.setPosition(Vector2f(vertices[i].x, vertices[i].y));
  186.             rect.setFillColor(Color::Blue);
  187.             window.draw(rect);
  188.         }
  189.         ///TODO:  Draw points
  190.         for(size_t i = 0; i < points.size(); i++)
  191.         {
  192.             RectangleShape rect(Vector2f(10, 10));
  193.             rect.setPosition(Vector2f(points[i].x, points[i].y));
  194.             if(i == 0)
  195.             {
  196.                 //  The color of the point the user clicks is yellow
  197.                 rect.setFillColor(Color::Yellow);
  198.             }
  199.             else
  200.             {
  201.                 //  The colors the points the computer generates are red
  202.                 rect.setFillColor(Color::Red);
  203.             }
  204.             window.draw(rect);
  205.         }
  206.         window.draw(userText);
  207.         window.display();
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement