Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <string.h>
- #include <time.h>
- #include "console.h"
- using namespace std;
- struct Competitor
- {
- unsigned short int id;
- string name;
- char short_name;
- unsigned short int position;
- };
- /* Prototypes */
- void move(Competitor *competitor, short int amount); // move a competitor (passed) a specified amount left or right (- or +)
- int rand_int(int lower, int upper); // generate a random number between lower and upper, inclusive
- unsigned short int winner(Competitor competitors[], unsigned short int num_competitors, unsigned short int length);
- void render(Competitor competitors[], unsigned short int num_competitors, unsigned short int length);
- unsigned short int winner(Competitor competitors[], unsigned short int num_competitors, unsigned short int length);
- int main()
- {
- /* ------ Configuration ------ */
- const unsigned short int LENGTH = 50; // the length of the hill/line/arena
- const unsigned short int NUM_COMPETITORS = 2; // the number of competitors
- Competitor competitors[NUM_COMPETITORS]; // create an array of X competitors (structs)
- /* --- Create our competitors --- */
- competitors[0].id = 1;
- competitors[0].name = "Hare";
- competitors[0].position = 0;
- competitors[0].short_name = 'H';
- competitors[1].id = 2;
- competitors[1].name = "Turtle";
- competitors[1].position = 0;
- competitors[1].short_name = 'T';
- // seed rand
- srand( time(NULL) );
- // Game loop!
- while(!winner(competitors, NUM_COMPETITORS, LENGTH))
- {
- // Move the players
- for (int i = 0; i < NUM_COMPETITORS; i++)
- {
- unsigned short int random = rand_int(0, 100);
- gotoxy(5, 10); // set cmd output coords
- // Hard coded... handle probabilities... this would need to be adjusted to add more players
- if (i == 0)
- {
- // hare
- if (random < 20)
- {
- // sleep - nothing
- }
- else if (random >= 20 && random < 40)
- {
- // big hop - 9 right
- move(&competitors[1], 9);
- }
- else if (random >= 40 && random < 50)
- {
- // big slip - 12 left
- move(&competitors[1], -12);
- }
- else if (random >= 50 && random < 80)
- {
- // small hop - 1 right
- move(&competitors[1], 1);
- }
- else if (random >=80 && random < 100)
- {
- // small slip - 2 left
- move(&competitors[1], -2);
- }
- }
- else
- {
- // turtle
- if (random < 50)
- {
- // fast plod - 3 right
- move(&competitors[0], 3);
- }
- else if (random >= 50 && random < 70)
- {
- // slip - 6 left
- move(&competitors[0], -6);
- }
- else if (random >= 70 && random < 100)
- {
- // slow plod - 1 right
- move(&competitors[0], 1);
- }
- }
- }
- // Sleep for a second
- Sleep(1000);
- // Render the line
- render(competitors, NUM_COMPETITORS, LENGTH);
- }
- char p = _getch(); // pause
- return 0;
- }
- /**
- * Generate a random integer between lower and upper.
- */
- int rand_int(int lower, int upper)
- {
- upper++;
- int range = upper - lower;
- return (rand() % range) + lower;
- }
- /**
- * Move a competitor struct by a passed amount.
- *
- * If amount was -3, move 3 spaces left. 9 would be 9 places right and so on.
- */
- void move(Competitor *competitor, short int amount)
- {
- if (competitor->position + amount <= 0)
- {
- competitor->position = 0;
- }
- else
- {
- competitor->position += amount;
- }
- }
- /**
- * Render the line. Pass in the array of competitors, the number of competitors and the length of the line
- */
- void render(Competitor competitors[], unsigned short int num_competitors, unsigned short int length)
- {
- unsigned short int count = 0;
- char output;
- // start locations for gotoxy()
- unsigned short int startx = 5, starty = 5;
- // foreach point on the line
- for (int i = 0; i < length; i++)
- {
- gotoxy(i+startx, starty);
- // foreach competitor in this game
- for (int j = 0; j < num_competitors; j++)
- {
- // is there a competitor at this point?
- if (competitors[j].position == i)
- {
- // yes
- count++; // increment the count of competitors at this point
- // is there more than one competitor at this point?
- if (count > 1)
- {
- output = 'C';
- }
- else
- {
- output = competitors[j].short_name; // output the character's short name (H or T for example)
- }
- }
- else
- {
- // no
- output = '-'; // output blank line
- }
- }
- count = 0;
- cout << output;
- cout.flush(); // send output immediately
- }
- }
- unsigned short int winner(Competitor competitors[], unsigned short int num_competitors, unsigned short int length)
- {
- int winner = 0; // id = 0 (no winner yet)
- for (int i = 0; i < num_competitors; i++)
- {
- if (competitors[i].position >= length)
- {
- winner = competitors[i].id;
- break; // found the winner - break from the loop
- }
- }
- return winner;
- }
- /*
- gotoxy(10,10);
- cout <<"hello2";
- for (int i=0;i<20;i++)
- {
- cout<<"Hello ";
- Sleep(1000);
- cout.flush();
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement