This assignment requires you to code a simulation race between the tortoise and
ID: 644948 • Letter: T
Question
This assignment requires you to code a simulation race between the tortoise and the hare. For this
project, you be using random-number generation to move the creatures. To make things more
interesting, the animals have to race up the side of a slippery mountain, which could cause them to
lose ground. In this race either animal could win or there could be a tie with no clear winner.
The animals begin at "Square 1" of 70 squares. Each of the squares represents a position the animal
can hold along the racetrack. The finish line is at Square 70. When an animal wins, a winning
message of your choice should be posted. For example:
Explanation / Answer
please dont upload question as html format....it was in unreadable format....
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void tortoiseStep(int * const tortoisePlace);
void hareStep(int * const harePlace);
int main()
{
// seed rand
srand(time(0));
enum Status {HAREWINS, TORTOISEWINS, TIE, CONTINUE};
Status gameStatus = CONTINUE;
int tortoise = 1;
int hare = 1;
// begin race
cout << "BANG!!!! AND THEY'RE OFF!!!! ";
while (gameStatus == CONTINUE)
{
// move the contenders
tortoiseStep(&tortoise);
hareStep(&hare);
// show race status
for (int line = 0; line < 70; line++)
{
if ((line == tortoise) && (line == hare))
cout << "Oh!";
else if (line == tortoise)
cout << 'T';
else if (line == hare)
cout << 'H';
else
cout << '-';
}
cout << endl;
// determine whether one or the other reached the end line
if ((tortoise >= 70) && (hare >= 70))
{
cout << "It's a tie." << endl;
gameStatus = TIE;
}
else if (tortoise >= 70)
{
cout << "TORTOISE WINS!! YAY!!!" << endl;
gameStatus = TORTOISEWINS;
}
else if (hare >= 70)
{
cout << "Hare wins. Ouch!" << endl;
gameStatus = HAREWINS;
}
}
}
void tortoiseStep(int * const tortoisePosition)
{
int step = 1 + rand() % 10;
// move tortoise
if ((step >= 1) && (step <= 5))
*tortoisePosition += 5;
else if ((step >=6) && (step <= 7))
*tortoisePosition -= 6;
else if ((step >= 8) && (step <= 10))
*tortoisePosition += 1;
if (*tortoisePosition < 1)
*tortoisePosition = 1;
}
void hareStep(int * const harePlace)
{
int step = 1 + (rand() % 10);
// move hare
if ((step >= 1) && (step <= 2));
else if ((step >= 3) && (step <= 4))
*harePlace += 9;
else if (step == 5)
*harePlace -= 12;
else if ((step >= 6) && (step <= 8))
*harePlace += 1;
else if ((step >= 9) && (step <= 10))
*harePlace -= 2;
if (*harePlace < 1)
*harePlace = 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.