Advertisement
Guest User

devin T

a guest
Oct 9th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <time.h>
  4. #include <cstdlib>
  5. #include <limits>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. class Horse
  11. {
  12.     private:
  13.         int position;
  14.  
  15.     public:
  16.         Horse();
  17.         void advance();
  18.         int getPosition();
  19.  
  20. };
  21.  
  22. class Race
  23. {
  24.     private:
  25.         int length;
  26.         Horse h[5];
  27.  
  28.     public:
  29.         Race();
  30.         Race(int length);
  31.         void printLane(int horseNum);
  32.         void start();
  33.  
  34. };
  35.  
  36. Horse::Horse()
  37. {
  38.     Horse::position = 0;
  39. }
  40.  
  41. void Horse::advance()
  42. {
  43.     int moveHorse = rand() % 2;
  44.  
  45.     if (moveHorse == 1)
  46.     {
  47.         position++;
  48.     }
  49. }
  50.  
  51. int Horse::getPosition()
  52. {
  53.     return position;
  54. }
  55.  
  56. Race::Race()
  57. {
  58.     int length = 10;
  59.  
  60. }
  61.  
  62. Race::Race(int length)
  63. {
  64.     length = length;
  65. }
  66.  
  67. void Race::printLane(int horseNum)
  68. {
  69.     int trackPosition = h[horseNum].getPosition();
  70.  
  71.     for (int j = 0; j < 10; j++)
  72.     {
  73.         if (j == h[horseNum].getPosition())
  74.         {
  75.             cout << horseNum << " ";
  76.         }
  77.  
  78.         else
  79.         {
  80.             cout << ". ";
  81.         }
  82.     }
  83.  
  84.     cout << endl;
  85. }
  86.  
  87. void Race::start()
  88. {
  89.     bool keepGoing = true;
  90.  
  91.     while (keepGoing == true)
  92.     {
  93.         for (int i = 0; i < 5; i++)
  94.         {
  95.             if (h[i].getPosition() == (9))
  96.             {
  97.                 printLane(i);
  98.                 keepGoing = false;
  99.                 cout << "Horse " << horseNum << " won!" << endl;
  100.             }
  101.  
  102.             else
  103.             {
  104.                 printLane(i);
  105.             }
  106.  
  107.             cout << endl;
  108.  
  109.         }
  110.  
  111.         for (int j = 0; j <= 5; j++)
  112.         {
  113.             h[j].advance();
  114.  
  115.         }
  116.  
  117.         cout << endl << "Press any key to continue" << endl;
  118.         cin.ignore();
  119.     }
  120.  
  121. }
  122.  
  123. int main()
  124. {
  125.     srand(time(NULL));
  126.     Race test;
  127.     test.start();
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement