horseRace.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. using namespace std;
  5. // horse class
  6. class Horse{
  7. public:
  8. // constructor
  9. Horse(){
  10. position = 0;
  11. }
  12. // access private position
  13. int getPosition(){
  14. return position;
  15. }
  16. // flips a coin, advances ~50% of times
  17. void advance(){
  18. if (rand() % 2){ // either 0 or 1
  19. position++;
  20. }
  21. }
  22. private:
  23. int position; // position of horse on track
  24. };
  25. // race as in event and race as in track
  26. class Race{
  27. public:
  28. // constructors
  29. Race(){
  30. length = 32; // default length
  31. numHorses = 4; // default number of horses
  32. horses = new Horse[numHorses]; // make some horses
  33. }
  34. Race(int length){
  35. this->length = length; // defined length
  36. this->numHorses = 4; // default number of horses
  37. horses = new Horse[numHorses]; // make some horses
  38. }
  39. // get me those horses
  40. Horse *getHorses(){
  41. return horses;
  42. }
  43. // prints one lane
  44. void printLane(int lane){ // which lane
  45. int i = 0;
  46. for (i = 0; i < length; i++){
  47. // prints the horses number at its latest position
  48. if (i == horses[lane].getPosition()){
  49. cout << lane;
  50. // prints a lane visual
  51. } else {
  52. cout << ".";
  53. } // end if
  54. } // end for
  55. cout << endl; // newline and stream flush
  56. }
  57. // all the horses flip a coin and advance accordingly
  58. void step(){
  59. int i = 0;
  60. // all horses in the race
  61. for (i = 0; i < numHorses; i++){
  62. horses[i].advance(); // horses know how to advance
  63. }
  64. }
  65. // prints each lane
  66. void printAllLanes(){
  67. int i = 0;
  68. for (i = 0; i < numHorses; i++){
  69. printLane(i);
  70. }
  71. }
  72. // checks if any horses have won or tied
  73. bool isFinished(){
  74. int i = 0;
  75. // all horses in the race
  76. for (i = 0; i < numHorses; i++){
  77. if (horses[i].getPosition() == length){
  78. return true; // one or more winners
  79. }
  80. }
  81. return false;
  82. }
  83. // this function is not used
  84. // it was mentioned in the UML diagram
  85. // I prefer to include the run loop in the main function
  86. // start the race
  87. void start(){
  88. while (!this->isFinished()){ // stop if there's a winner
  89. this->printAllLanes(); // update CLI
  90. this->step(); // all horses advance...
  91. cin.ignore(); // ...when the user presses enter
  92. }
  93. }
  94. // prints winner(s)
  95. void printWinners(){
  96. cout << "horse(s)";
  97. int i = 0;
  98. // check all the horses
  99. for (i = 0; i < numHorses; i++){
  100. // if horse at i finished...
  101. if (horses[i].getPosition() == length){
  102. cout << " " << i; // ...horse at i won!
  103. }
  104. }
  105. cout << " won (tied)!" << endl;
  106. }
  107. private:
  108. Horse *horses; // horses
  109. int numHorses; // number of horses (size of array)
  110. int length; // length of the track
  111. };
  112. int main(){
  113. // random number seed
  114. srand(time(NULL));
  115. // crate a race
  116. Race race = Race();
  117. // while the race is underway
  118. while(!race.isFinished()){
  119. race.printAllLanes(); // update CLI
  120. race.step(); // all horses advance...
  121. // tell the user what to do
  122. cout << "Press enter for another turn:";
  123. cin.ignore(); // ...when the user presses enter
  124. }
  125. race.printWinners(); // print the winners
  126. return EXIT_SUCCESS;
  127. } // end main