Advertisement
bwukki

Untitled

Apr 9th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. //Tortoise and the hare race
  2. #include <iostream>
  3. #include <vector>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. int *updatePosHare(int*);
  9. int *updatePosTort(int*);
  10.  
  11. void startMessage() {
  12.     cout << "Bang!!" << endl;
  13.     cout << "And they're off!" << endl;
  14. }
  15.  
  16. int checkWin(int *currentPos) {
  17.     int result{0};
  18.     if (currentPos[70] >= 70 && currentPos[71 >= 70]) {
  19.         cout << "It's a tie!" << endl;
  20.         result = 1;
  21.         return(result);
  22.     }
  23.  
  24.     if (currentPos[70] >= 70) {
  25.         cout << "The hare wins!" << endl;
  26.         result = 1;
  27.     }
  28.     if (currentPos[71] >= 70) {
  29.         cout << "The tortoise wins!" << endl;
  30.         result = 1;
  31.     }
  32.     return(result);
  33. }
  34.  
  35. void displayPos(int *currentPos) {
  36.     for (int i = 0; i < 69; i++) {
  37.         switch (*currentPos++) {
  38.             case 0: cout << "_";
  39.                     break;
  40.             case 1: cout << "T";
  41.                     break;
  42.             case 2: cout << "H";
  43.                     break;
  44.             case 3: cout << "Ouch!!";
  45.                     break;
  46.         }
  47.     }
  48.     cout << endl;
  49. }
  50.  
  51. int* updatePos(int *currentPos) {
  52.     currentPos = updatePosHare(currentPos);
  53.     currentPos = updatePosTort(currentPos);
  54.     return(currentPos);
  55. }
  56.  
  57. int *updatePosHare(int *currentPos) {
  58.     if (currentPos[70] >= 0) { currentPos[currentPos[70]] = 0; }
  59.     int mover{rand() % 10 + 1};
  60.     if (mover <= 2) { currentPos[70] = currentPos[70] + 0; }
  61.     if (mover > 2 && mover <= 4) { currentPos[70] = currentPos[70] + 9; }
  62.     if (mover == 5) { currentPos[70] = currentPos[70] - 12; }
  63.     if (mover > 5 && mover <= 8) { currentPos[70] = currentPos[70] + 1; }
  64.     if (mover > 8) { currentPos[70] = currentPos[70] -2; }
  65.     if (currentPos[70] >= 0) {
  66.         currentPos[currentPos[70]] = 2;
  67.     }
  68.         if (currentPos[70] < 0) {
  69.         cout << "The hare has fallen backwards off the track! He is " << -1*currentPos[70] << " units away from the starting line!" << endl;
  70.     }
  71.     return currentPos;
  72. }
  73.  
  74. int *updatePosTort(int *currentPos) {
  75.     int mover{rand() % 10 + 1};
  76.     if (currentPos[71] >= 0) { currentPos[currentPos[71]] = 0; }
  77.     if (mover <= 5) { currentPos[71] = currentPos[71] + 3; }
  78.     if (mover > 5 && mover <= 7) { currentPos[71] = currentPos[71] -6; }
  79.     if (mover > 7) { currentPos[71] = currentPos[71] + 1; }
  80.     if (currentPos[71] >= 0) {
  81.         if (currentPos[currentPos[71]] == 0) { currentPos[currentPos[71]] = 1; }
  82.         else { currentPos[currentPos[71]] = 3; }
  83.     }
  84.     if (currentPos[71] < 0) {
  85.         cout << "The tortoise has fallen backwards off the track! He is " << -1*currentPos[71] << " units away from the starting line!" << endl;
  86.     }
  87.     return currentPos;
  88. }
  89.  
  90. void test() {
  91.     srand(time(NULL));
  92.     int wincheck{0};
  93.     int racePos [81] = {0}; //This is 81 because it's the max position the hare can go on a win.
  94.     int *raceCurrent{racePos};
  95.     racePos[0] = 3;
  96.     displayPos(raceCurrent);
  97.     startMessage();
  98.     while (wincheck != 1) { /* Gives us raceLength # of iterations, this is where the main body of the code is. Someday you might want to refactor it after tests are done. */
  99.         raceCurrent = updatePos(raceCurrent);
  100.         displayPos(raceCurrent);
  101.         wincheck = checkWin(raceCurrent);
  102.     }
  103.  
  104. }
  105.  
  106. int main()
  107. {
  108.     test();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement