1
0

Race.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <chrono>
  5. #include <thread>
  6. #include <fstream>
  7. #include "Race.h"
  8. #include "Horse.h"
  9. // race as in event and race as in track
  10. Race::Race(){
  11. length = 32; // default length
  12. numHorses = 4; // default number of horses
  13. horses = new Horse[numHorses]; // make some horses
  14. }
  15. Race::Race(int length){
  16. this->length = length; // defined length
  17. this->numHorses = 4; // default number of horses
  18. horses = new Horse[numHorses]; // make some horses
  19. }
  20. // formatted output
  21. std::string Race::raceToString(){
  22. std::string out = "";
  23. int i = 0;
  24. for (i = 0; i < numHorses; i++){
  25. out = out + laneToString(i) + "\n";
  26. }
  27. return out;
  28. }
  29. std::string Race::laneToString(int lane){
  30. std::string out = "";
  31. out = out + std::to_string(lane + 1) + "|";
  32. int i = 0;
  33. for (i = 0; i < length; i++){
  34. // prints the horses number at its latest position
  35. if (i == horses[lane].getPosition()){
  36. out = out + std::to_string(lane + 1);
  37. // prints a lane visual
  38. } else {
  39. out = out + "-";
  40. } // end if
  41. } // end for
  42. out = out + "|";
  43. // out = out + std::endl; // newline and stream flush
  44. return out;
  45. }
  46. // prints one lane
  47. void Race::printLane(int lane){ // which lane
  48. int i = 0;
  49. for (i = 0; i < length; i++){
  50. // prints the horses number at its latest position
  51. if (i == horses[lane].getPosition()){
  52. std::cout << lane;
  53. // prints a lane visual
  54. } else {
  55. std::cout << "-";
  56. } // end if
  57. } // end for
  58. std::cout << std::endl; // newline and stream flush
  59. }
  60. // all the horses flip a coin and advance accordingly
  61. void Race::step(){
  62. int i = 0;
  63. // all horses in the race
  64. for (i = 0; i < numHorses; i++){
  65. horses[i].advance(); // horses know how to advance
  66. }
  67. }
  68. // prints each lane
  69. void Race::printAllLanes(){
  70. int i = 0;
  71. for (i = 0; i < numHorses; i++){
  72. printLane(i);
  73. }
  74. }
  75. // checks if any horses have won or tied
  76. bool Race::isFinished(){
  77. int i = 0;
  78. // all horses in the race
  79. for (i = 0; i < numHorses; i++){
  80. if (horses[i].getPosition() == length){
  81. return true; // one or more winners
  82. }
  83. }
  84. return false;
  85. }
  86. // who won the race?
  87. int Race::getWinner(){
  88. int i = 0;
  89. for (i = 0; i < numHorses; i++){
  90. // if horse at i finished...
  91. if (horses[i].getPosition() == length){
  92. return (i + 1); // winner by number
  93. }
  94. }
  95. return -1;
  96. }
  97. // prints winner(s)
  98. void Race::printWinners(){
  99. std::cout << "horse(s)";
  100. int i = 0;
  101. // check all the horses
  102. for (i = 0; i < numHorses; i++){
  103. // if horse at i finished...
  104. if (horses[i].getPosition() == length){
  105. std::cout << " " << i; // ...horse at i won!
  106. }
  107. }
  108. std::cout << " won (tied)!" << std::endl;
  109. }
  110. // log the winner
  111. void Race::logWinner(){
  112. std::ofstream fout ("winners.log", std::ios::app);
  113. fout << std::to_string(getWinner()); // log by number not index
  114. fout.close();
  115. }