kylehannon

CH 8 Problem 12

May 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2.  using namespace std;
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <iomanip>
  6.  
  7.  
  8.  
  9. const int RACE_END{70};
  10. void moveTort(int* const);
  11. void moveHare(int* const);
  12. void printPosition(const int*, const int* const);
  13.  
  14. int main()
  15. {
  16.         int tort = 1, hare = 1, timer = 0;
  17.     srand(time(0));
  18.     cout <<"ON YOUR MARK, GET SET\n BANG!!!" << "\nAND THEY'RE OFF!!! \n";
  19.    
  20.     while (tort !=RACE_END && hare != RACE_END ) {
  21.    
  22.         moveTort(&tort);
  23.         moveHare(&hare);
  24.         printPosition(&tort,&hare);
  25.         ++timer;
  26.     }
  27.    
  28.    
  29.      cout << "\nTime Elapsed = " << timer << " seconds" << endl;
  30.      
  31.      
  32.    
  33.    
  34.     return 0;
  35. }
  36.  
  37. void moveTort(int* const tortPtr) {
  38.     int x= 1 + rand() %10;
  39.     if (x >= 1 && x <= 5) {// fast plod
  40.         *tortPtr += 3;
  41.     }else {
  42.         if (x==6||x==7) { //slip
  43.             *tortPtr -=6;
  44.         }
  45.     }
  46.     if (x >=8 && x <=10) { //slow plod
  47.         *tortPtr += 1;
  48.     } else {
  49.         if (*tortPtr > RACE_END) { //check if won
  50.             *tortPtr = RACE_END;
  51.         }
  52.        
  53.     }
  54. }
  55.  
  56.  
  57. void moveHare(int* const harePtr) {
  58.     int y = 1 +rand() %10;
  59.     if (y==3 || y ==4) {
  60.         *harePtr +=9;//big hop
  61.     }else {
  62.         if (y==5) {
  63.             *harePtr -=12;//big slip
  64.         } else {
  65.             if (y>=6 && y<=8) {
  66.                 ++(*harePtr);
  67.             }
  68.             else {
  69.                 if ( y> 8) {
  70.                     *harePtr -=2;//
  71.                 }
  72.                 else {
  73.                     if ( *harePtr > RACE_END) {
  74.                         *harePtr = RACE_END;
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83.  
  84. void printPosition (const int * const turtPtr, const int * const rabPtr) {
  85.    
  86.     if (*rabPtr == *turtPtr ) {
  87.         cout << setw(*rabPtr) << "OUCH!!!";
  88.     }
  89.         if ( *rabPtr < *turtPtr) {
  90.             cout << setw(*rabPtr) << 'H'
  91.             << setw(*turtPtr) << 'T';
  92.         }
  93.          
  94.          
  95.         if (*rabPtr > *turtPtr)
  96.         {
  97.            
  98.             cout << setw(*turtPtr) <<"T"
  99.                 <<setw(*rabPtr) << "H";
  100.            
  101.         }
  102.        
  103.        
  104.     if (*rabPtr == RACE_END){
  105.         cout << "\nRabbit wins";
  106.     }
  107.     if (*turtPtr == RACE_END) {
  108.         cout << "\nTortoise wins";
  109.     }
  110.     if ( *rabPtr == RACE_END && *turtPtr == RACE_END)
  111.     {
  112.         cout << "\nehh...looks like tortoise won";
  113.     }
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment