Nofxthepirate

Tortoise and Hare Ch 7

Jun 14th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <string>
  5. #include <ctime>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. void runRace ();
  11. void printTrack(int, int);
  12. void moveTortoise(int*);
  13. void moveHare(int*);
  14. void printResults(int, int);
  15.  
  16. int main()
  17. {
  18.     srand(time(0));
  19.     bool isRacing{true};
  20.     while(isRacing) {
  21.          runRace();
  22.          cout << "Race Again? 1 for Yes, 0 for No";
  23.          cin >> isRacing;
  24.     }
  25. }
  26.  
  27. void runRace(){
  28.     int tortoise{1};
  29.     int hare{1};
  30.  
  31.     while((tortoise <= 70) && (hare <= 70)) {
  32.  
  33.         printTrack(tortoise, hare);
  34.         cout << endl;
  35.  
  36.         if (tortoise == 70) {
  37.             tortoise = 71;
  38.         }
  39.         else moveTortoise(&tortoise);
  40.  
  41.         if (hare == 70) {
  42.                 hare = 71;
  43.         }
  44.         else  moveHare(&hare);
  45.     }
  46.  
  47.     printResults(tortoise, hare);
  48. }
  49. void printTrack(int tortoise, int hare){
  50.     for(int i{1}; i <= 71; i++) {
  51.             bool ouch{false};
  52.  
  53.             if ((i == hare) && (tortoise == hare && tortoise + hare != 2)){
  54.                 cout << "OUCH!!!";
  55.                 ouch = true;
  56.                 i += 4;
  57.             }
  58.  
  59.             if (!ouch){
  60.                 if(i == tortoise){
  61.                     cout << 'T';
  62.                 }
  63.  
  64.                 if(i == hare){
  65.                     cout << 'H';
  66.                 }
  67.  
  68.                 if (i < 70) cout << '-';
  69.             }
  70.     }
  71. }
  72. void moveTortoise(int* tortPtr) {
  73.     int x{1 + rand() % 10};
  74.     if (x <= 5){ *tortPtr += 3; }
  75.  
  76.     else if (x <= 7){ *tortPtr -= 6; }
  77.     else if (x <= 10){ *tortPtr += 1; }
  78.  
  79.     if (*tortPtr < 1){ *tortPtr = 1; }
  80.  
  81.     if (*tortPtr > 70) { *tortPtr = 70; }
  82. }
  83. void moveHare(int* harePtr) {
  84.     int x{1 + rand() % 10};
  85.  
  86.     if (x >= 2 && x <= 4){ *harePtr += 9; }
  87.  
  88.     else if (x <= 5){ *harePtr -= 12; }
  89.  
  90.     else if (x <= 8){ *harePtr += 1; }
  91.     else *harePtr -= 2;
  92.  
  93.     if (*harePtr < 1){ *harePtr = 1; }
  94.  
  95.     if (*harePtr > 70){ *harePtr = 70; }
  96. }
  97. void printResults(int tortoise, int hare){
  98.     if (hare == tortoise) {
  99.     cout << "It's a Tie!" << endl;
  100.     }
  101.     else if (tortoise >= 70) {
  102.         cout << "Tortoise Wins!!!" << endl;
  103.     }
  104.     else if (hare >= 70) {
  105.         cout << "Hare Wins. Yuch." << endl;
  106.     }
  107. }
Add Comment
Please, Sign In to add comment