This second assignment will build upon our knowledge of poin

This second assignment will build upon our knowledge of pointers/pass-by-reference and how they allow us, the programmer, to directly access and manipulate memory in C++. This assignment will also help to familiarize you with C++ as well as the concept of Makefiles. We will explore the full power of this Object-oriented programing language and its important concepts later this semester For this assignment, we are going to explore ho we can use pointers, functions, and conditionals in C to create a simulation of Spartan race. A Spartan race consists of a series of obstacle races of varying distance and difficulty. For this simulation we assume that you are competing against your friend and you want to prove to them that you can beat them in the race. There is one small problem here your friend has run two previous Spartan races and this is the first time you have ever attempted this. Fear not you consider yourself very athletic and have trained for this epic showdown. For simplicity we will assume that to complete the race you must move 50 \"spaces.\" Each space you or your friend move could either be a normal space (in which you can run or sprint on) or one in which you could face an obstacle (a muddy bog. a rope climb, or traversing the monkey bars). In addition, you and your friend cannot be on the same space or same obstacle at the same time. If you end up on the same space your friend, due to their experience, makes you fall back one spot. You can either conquer these obstacles or fail while attempting them. If you fail an obstacle, the obstacle will set you back and you must \"re-try\" the obstacle before moving on. The goal is for you to finish before your friend-can you do it!? We will use the following table to outline the obstacles that both you and your friend will face and the penalty or reward you will receive for landing on such a square in the game: Movement LPersentake oOEncounter PenaltyReward Run 30% Move 4 spaces Right Sprint Move s spaces Right FallIn Mud You Move 2 spaces Left 20% Move 3 spaces Left Fall Off Rope 10% Move 4 spacesLeft Fall Off Monkey Bars Run 400 Move 5 spaces Right Sprint IO4 Move 6 spaces Right Your Friend Fall in Mud 20 Move spaces Left Fall Off Rope Move 2 spaces Left Fall Off Monkey Bars Move 4 spaces Left Your goal is to create a program to simulate this race. You will be using C++ to accomplish this task, and capitalize on the languages implementation of pointers, control structures, functions, and libraries. I will provide the skeleton (bare bones) structure of the program in the form of the file (Racer.cpp) on Canvas You are to edit this file to accomplish the goal of the assignment. You may not add any additional methods and your program must make use of pointers and pass-by-reference to simulate the race.

Solution

#include <iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;

#define RACE_LENGTH 50

void advanceRacerA(int *ptrRacerA);
void advanceRacerB(int *ptrRacerB);
void printPosition(int *ptrRacerA, int *ptrRacerB);

int main()
{
// define racer\'s initial positions
int racerA=0, racerB=0;

cout << \"Welcome to Ryan\'s spartan Race!\";
cout << \"READY...SET...GO!\";

while(true) {
advanceRacerA(&racerA);
advanceRacerB(&racerB);
printPosition(&racerA, &racerB);

// if both fall at same spot, A looses
if(racerA == racerB){
racerA -= 1;
}

if((racerA == RACE_LENGTH) || (racerB == RACE_LENGTH)) {
// race is over. One has won.
if(racerB == RACE_LENGTH) {
cout << \"\ Your friend has won the race - I must train harder!\ \";
} else {
cout << \"\ Yipppppie! I won!\ \";
}
break;
}
}

return 0;
}


void advanceRacerA(int *ptrRacerA) {
   // get a random number between 1-10
int moveement = rand() % 10 + 1;
  
   int initialPosition = *ptrRacerA;
   if(moveement <=3) {
       // Run
       *ptrRacerA = initialPosition + 4;
   } else if(moveement <=4) {
       // Sprint
       *ptrRacerA = initialPosition + 5;
   } else if(moveement <=7) {
       // Fall in mud
       *ptrRacerA = initialPosition - 2;
   } else if(moveement <=9) {
       // Fall off rope
       *ptrRacerA = initialPosition - 3;
   } else if(moveement <=10) {
       // Fall off monkey bars
       *ptrRacerA = initialPosition - 4;
   }   
   // iF racer has moved to start position by shifting left
   if(*ptrRacerA < 0) {
   *ptrRacerA = 0;
   } else if(*ptrRacerA > RACE_LENGTH) {
   *ptrRacerA = RACE_LENGTH;
   }
}

void advanceRacerB(int *ptrRacerB) {
   // get a random number between 1-10
int moveement = rand() % 10 + 1;
  
   int initialPosition = *ptrRacerB;
   if(moveement <=4) {
       // Run
       *ptrRacerB = initialPosition + 5;
   } else if(moveement <=5) {
       // Sprint
       *ptrRacerB = initialPosition + 6;
   } else if(moveement <=7) {
       // Fall in mud
       *ptrRacerB = initialPosition - 1;
   } else if(moveement <=9) {
       // Fall off rope
       *ptrRacerB = initialPosition - 2;
   } else if(moveement <=10) {
       // Fall off monkey bars
       *ptrRacerB = initialPosition - 4;
   }   
   // iF racer has moved to start position by shifting left
   if(*ptrRacerB < 0) {
   *ptrRacerB = 0;
   } else if(*ptrRacerB > RACE_LENGTH) {
   *ptrRacerB = RACE_LENGTH;
   }
}

void printPosition(int *ptrRacerA, int *ptrRacerB) {
   int i=0;
   cout << \"\ \";
   for(i; i<= RACE_LENGTH; i++) {
       if(*ptrRacerA == i) {
           cout << \"A\";
       } else {
           cout << \" \";
       }
       if(*ptrRacerB == i) {
           cout << \"B\";
       } else {
           cout << \" \";
       }
   }
   cout << \"|\";
}

 This second assignment will build upon our knowledge of pointers/pass-by-reference and how they allow us, the programmer, to directly access and manipulate mem
 This second assignment will build upon our knowledge of pointers/pass-by-reference and how they allow us, the programmer, to directly access and manipulate mem
 This second assignment will build upon our knowledge of pointers/pass-by-reference and how they allow us, the programmer, to directly access and manipulate mem

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site