Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "console.h"
  6. using namespace std;
  7.  
  8. struct Competitor
  9. {
  10.     unsigned short int id;
  11.     string name;
  12.     char short_name;
  13.     unsigned short int position;
  14. };
  15.  
  16. /* Prototypes */
  17. void move(Competitor *competitor, short int amount); // move a competitor (passed) a specified amount left or right (- or +)
  18. int rand_int(int lower, int upper); // generate a random number between lower and upper, inclusive
  19. unsigned short int winner(Competitor competitors[], unsigned short int num_competitors, unsigned short int length);
  20. void render(Competitor competitors[], unsigned short int num_competitors, unsigned short int length);
  21. unsigned short int winner(Competitor competitors[], unsigned short int num_competitors, unsigned short int length);
  22.  
  23. int main()
  24. {
  25.     /* ------ Configuration ------ */
  26.     const unsigned short int LENGTH = 50; // the length of the hill/line/arena
  27.     const unsigned short int  NUM_COMPETITORS = 2; // the number of competitors
  28.     Competitor competitors[NUM_COMPETITORS]; // create an array of X competitors (structs)
  29.  
  30.     /* --- Create our competitors --- */
  31.     competitors[0].id = 1;
  32.     competitors[0].name = "Hare";
  33.     competitors[0].position = 0;
  34.     competitors[0].short_name = 'H';
  35.  
  36.     competitors[1].id = 2;
  37.     competitors[1].name = "Turtle";
  38.     competitors[1].position = 0;
  39.     competitors[1].short_name = 'T';
  40.  
  41.     // seed rand
  42.     srand( time(NULL) );
  43.  
  44.     // Game loop!
  45.     while(!winner(competitors, NUM_COMPETITORS, LENGTH))
  46.     {
  47.         // Move the players
  48.         for (int i = 0; i < NUM_COMPETITORS; i++)
  49.         {
  50.             unsigned short int random = rand_int(0, 100);
  51.  
  52.             gotoxy(5, 10); // set cmd output coords
  53.  
  54.             // Hard coded... handle probabilities... this would need to be adjusted to add more players
  55.             if (i == 0)
  56.             {
  57.                 // hare
  58.                 if (random < 20)
  59.                 {
  60.                     // sleep - nothing
  61.                    
  62.                 }
  63.                 else if (random >= 20 && random < 40)
  64.                 {
  65.                     // big hop - 9 right
  66.                     move(&competitors[1], 9);
  67.                 }
  68.                 else if (random >= 40 && random < 50)
  69.                 {
  70.                     // big slip - 12 left
  71.                     move(&competitors[1], -12);
  72.                 }
  73.                 else if (random >= 50 && random < 80)
  74.                 {
  75.                     // small hop - 1 right
  76.                     move(&competitors[1], 1);
  77.                 }
  78.                 else if (random >=80 && random < 100)
  79.                 {
  80.                     // small slip - 2 left
  81.                     move(&competitors[1], -2);
  82.                 }
  83.             }
  84.             else
  85.             {
  86.                 // turtle
  87.                 if (random < 50)
  88.                 {
  89.                     // fast plod - 3 right
  90.                     move(&competitors[0], 3);
  91.                 }
  92.                 else if (random >= 50 && random < 70)
  93.                 {
  94.                     // slip - 6 left
  95.                     move(&competitors[0], -6);
  96.                 }
  97.                 else if (random >= 70 && random < 100)
  98.                 {
  99.                     // slow plod - 1 right
  100.                     move(&competitors[0], 1);
  101.                 }
  102.             }
  103.         }
  104.  
  105.         // Sleep for a second
  106.         Sleep(1000);
  107.  
  108.         // Render the line
  109.         render(competitors, NUM_COMPETITORS, LENGTH);
  110.  
  111.     }
  112.  
  113.     char p = _getch(); // pause
  114.     return 0;
  115. }
  116.  
  117. /**
  118.  * Generate a random integer between lower and upper.
  119.  */
  120. int rand_int(int lower, int upper)
  121. {
  122.     upper++;
  123.     int range = upper - lower;
  124.     return (rand() % range) + lower;
  125. }
  126.  
  127. /**
  128.  * Move a competitor struct by a passed amount.
  129.  *
  130.  * If amount was -3, move 3 spaces left. 9 would be 9 places right and so on.
  131.  */
  132. void move(Competitor *competitor, short int amount)
  133. {
  134.     if (competitor->position + amount <= 0)
  135.     {
  136.         competitor->position = 0;
  137.     }
  138.     else
  139.     {
  140.         competitor->position += amount;
  141.     }
  142. }
  143.  
  144. /**
  145.  * Render the line. Pass in the array of competitors, the number of competitors and the length of the line
  146.  */
  147. void render(Competitor competitors[], unsigned short int num_competitors, unsigned short int length)
  148. {
  149.     unsigned short int count = 0;
  150.     char output;
  151.  
  152.     // start locations for gotoxy()
  153.     unsigned short int startx = 5, starty = 5;
  154.  
  155.     // foreach point on the line
  156.     for (int i = 0; i < length; i++)
  157.     {
  158.         gotoxy(i+startx, starty);
  159.  
  160.         // foreach competitor in this game
  161.         for (int j = 0; j < num_competitors; j++)
  162.         {
  163.             // is there a competitor at this point?
  164.             if (competitors[j].position == i)
  165.             {
  166.                 // yes
  167.  
  168.                 count++; // increment the count of competitors at this point
  169.  
  170.                 // is there more than one competitor at this point?
  171.                 if (count > 1)
  172.                 {
  173.                     output = 'C';
  174.                 }
  175.                 else
  176.                 {
  177.                     output = competitors[j].short_name; // output the character's short name (H or T for example)
  178.                 }
  179.             }
  180.             else
  181.             {
  182.                 // no
  183.                 output = '-'; // output blank line
  184.             }
  185.         }
  186.  
  187.         count = 0;
  188.         cout << output;
  189.         cout.flush(); // send output immediately
  190.     }
  191. }
  192.  
  193. unsigned short int winner(Competitor competitors[], unsigned short int num_competitors, unsigned short int length)
  194. {
  195.     int winner = 0; // id = 0 (no winner yet)
  196.  
  197.     for (int i = 0; i < num_competitors; i++)
  198.     {
  199.         if (competitors[i].position >= length)
  200.         {
  201.             winner = competitors[i].id;
  202.             break; // found the winner - break from the loop
  203.         }
  204.     }
  205.  
  206.     return winner;
  207. }
  208. /*
  209. gotoxy(10,10);
  210.     cout <<"hello2";
  211.     for (int i=0;i<20;i++)
  212.     {
  213.         cout<<"Hello ";
  214.         Sleep(1000);
  215.         cout.flush();
  216.     }
  217. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement