Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. // --------------------------------------------------
  2. // THIS CODE FILE IS NOT TO BE MODIFIED IN ANY WAY!!!
  3. // --------------------------------------------------
  4.  
  5. // YOU ARE TO REFACTOR YOUR CODE FROM PREVIOUS HOMEWORKS
  6. // SO THAT THIS CODE WORKS PROPERLY WITH YOURS
  7.  
  8. // Include files
  9. #include <iostream>
  10. #include "Boov.h"
  11. #include "Gorg.h"
  12.  
  13. int main()
  14. {
  15. // Maintain counters for each competitor
  16. long long boov_wins = 0;
  17. long long gorg_wins = 0;
  18.  
  19. // Keep the battling to a minimum
  20. const long long NUMBER_OF_SIMULATIONS = 1000;
  21.  
  22. // Lots of battling
  23. for (long long i = 1; i <= NUMBER_OF_SIMULATIONS; ++i)
  24. {
  25. // Set up each object dynamically so virtual functions will work
  26. Competitor * oh = new Boov("Oh!");
  27. Competitor * george = new Gorg();
  28.  
  29. // Gorg gets attacked first
  30. // Boov only gets attacked if Gorg is still standing
  31. // See if we need to continue
  32. while ((oh->IsDefeated() == false) && (george->IsDefeated() == false))
  33. {
  34. george->GetsAttacked();
  35. if (george->IsDefeated() == false)
  36. {
  37. oh->GetsAttacked();
  38. }
  39. }
  40.  
  41. // Only one of the two is defeated... So let's figure out who it is
  42. // You'll need this link for programmming the print statements correctly
  43. // https://www.learncpp.com/cpp-tutorial/12-10-printing-inherited-classes-using-operator/
  44. if (george->IsDefeated())
  45. {
  46. ++boov_wins;
  47. std::cout << *oh << " defeats " << *george << std::endl;
  48. }
  49. else
  50. {
  51. ++gorg_wins;
  52. std::cout << *george << " defeats " << *oh << std::endl;
  53. }
  54.  
  55. // This particular simulation is over so give back the memory
  56. // Maybe if we continued development we would add a Refresh() function or something
  57. delete oh;
  58. delete george;
  59. }
  60.  
  61. // Print out the overall results
  62. double gorg = gorg_wins / static_cast<double>(NUMBER_OF_SIMULATIONS) * 100.0;
  63. double boov = boov_wins / static_cast<double>(NUMBER_OF_SIMULATIONS) * 100.0;
  64. // I personally get about 98.7% for George
  65. std::cout << "George wins " << gorg << "% of the time" << std::endl;
  66. // I personally get about 1.3% for Oh
  67. std::cout << "Oh wins " << boov << "% of the time" << std::endl;
  68.  
  69. // Pause program before closing down
  70. system("PAUSE");
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement