Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tortoise and the hare race
- #include <iostream>
- #include <vector>
- #include <time.h>
- using namespace std;
- int *updatePosHare(int*);
- int *updatePosTort(int*);
- void startMessage() {
- cout << "Bang!!" << endl;
- cout << "And they're off!" << endl;
- }
- int checkWin(int *currentPos) {
- int result{0};
- if (currentPos[70] >= 70 && currentPos[71 >= 70]) {
- cout << "It's a tie!" << endl;
- result = 1;
- return(result);
- }
- if (currentPos[70] >= 70) {
- cout << "The hare wins!" << endl;
- result = 1;
- }
- if (currentPos[71] >= 70) {
- cout << "The tortoise wins!" << endl;
- result = 1;
- }
- return(result);
- }
- void displayPos(int *currentPos) {
- for (int i = 0; i < 69; i++) {
- switch (*currentPos++) {
- case 0: cout << "_";
- break;
- case 1: cout << "T";
- break;
- case 2: cout << "H";
- break;
- case 3: cout << "Ouch!!";
- break;
- }
- }
- cout << endl;
- }
- int* updatePos(int *currentPos) {
- currentPos = updatePosHare(currentPos);
- currentPos = updatePosTort(currentPos);
- return(currentPos);
- }
- int *updatePosHare(int *currentPos) {
- if (currentPos[70] >= 0) { currentPos[currentPos[70]] = 0; }
- int mover{rand() % 10 + 1};
- if (mover <= 2) { currentPos[70] = currentPos[70] + 0; }
- if (mover > 2 && mover <= 4) { currentPos[70] = currentPos[70] + 9; }
- if (mover == 5) { currentPos[70] = currentPos[70] - 12; }
- if (mover > 5 && mover <= 8) { currentPos[70] = currentPos[70] + 1; }
- if (mover > 8) { currentPos[70] = currentPos[70] -2; }
- if (currentPos[70] >= 0) {
- currentPos[currentPos[70]] = 2;
- }
- if (currentPos[70] < 0) {
- cout << "The hare has fallen backwards off the track! He is " << -1*currentPos[70] << " units away from the starting line!" << endl;
- }
- return currentPos;
- }
- int *updatePosTort(int *currentPos) {
- int mover{rand() % 10 + 1};
- if (currentPos[71] >= 0) { currentPos[currentPos[71]] = 0; }
- if (mover <= 5) { currentPos[71] = currentPos[71] + 3; }
- if (mover > 5 && mover <= 7) { currentPos[71] = currentPos[71] -6; }
- if (mover > 7) { currentPos[71] = currentPos[71] + 1; }
- if (currentPos[71] >= 0) {
- if (currentPos[currentPos[71]] == 0) { currentPos[currentPos[71]] = 1; }
- else { currentPos[currentPos[71]] = 3; }
- }
- if (currentPos[71] < 0) {
- cout << "The tortoise has fallen backwards off the track! He is " << -1*currentPos[71] << " units away from the starting line!" << endl;
- }
- return currentPos;
- }
- void test() {
- srand(time(NULL));
- int wincheck{0};
- int racePos [81] = {0}; //This is 81 because it's the max position the hare can go on a win.
- int *raceCurrent{racePos};
- racePos[0] = 3;
- displayPos(raceCurrent);
- startMessage();
- 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. */
- raceCurrent = updatePos(raceCurrent);
- displayPos(raceCurrent);
- wincheck = checkWin(raceCurrent);
- }
- }
- int main()
- {
- test();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement