Question
C++ language! I need the code on code blocks with the output. Pls make sure that your output matches the given sample run output that
Race Between the Tortoise and the Hare:
There are three functions you need to implement for this homework, along with the main function.
The following constraints apply to all of your source code:
- Do not use the [] subscript operator (except in the initial array creation or when used as the delete[] operator)
- All of your variables must be dynamically allocated (so everything should be a pointer)
The program uses a char array to hold the track. An empty spot is indicated as a -. The tortoise uses a T as its marker, while the hare uses a H. If both animals are on the same spot on the track, use a X. Initially, both animals are at the start of the track.
You cannot change any of the given code.
The instructions on how to do:
int main()
The main function is still missing the actual game loop that will run the simulation! You will need to write a loop that allows the animals to take turns moving on the track and to check whether or not the race has ended. You also need to create dynamic variables that represent the positions of the animals (initialize them both to be 0).
The tortoise always move first in the race, followed by the hare, and then continuing to take turns. After each move, display the track, so the user can visually see what's happening in the race. Make sure to check if the race has ended after each move. Once an animal has reached the end of the track, print a message to indicate which animal won the race.
Once the simulation has finished, don't forget to release all the dynamic memory you allocated for your variables!
void displayTrack(char*, const int*)
The first parameter is an array pointer to the race track. The second parameter is a pointer to the size of the array.
This function just displays the contents of the race track array. You must use a loop to perform this output.
bool moveTortoise(char*, const int*, int*)
The first parameter is an array pointer to the race track. The second parameter is a pointer to the size of the array. The third parameter is a pointer to the current position of the tortoise on the track.
This function decides which move the tortoise is going to perform and update both the track and position accordingly. The move that is used is based on a random number r between 1 and 10 (inclusive). Print a message saying which action was performed. Use the following table to decide on which action to perform:
Be wary of out-of-bound calculations! If the move makes the tortoise have a negative position or a position that's beyond the track's size, you need to correct the calculation so that it stays within the track. In other words,
- a negative position needs to round up to position 0; and
- a position larger than the size needs to round down to the last position on the track.
When updating the track, keep in mind if the hare is already on the spot, as that will affect what character marker you put in that position.
Lastly, the function needs to return a bool value that represents whether or not the tortoise has reached the end of the track.
bool moveHare(char*, const int*, int*)
The first parameter is an array pointer to the race track. The second parameter is a pointer to the size of the array. The third parameter is a pointer to the current position of the hare on the track.
This function decides which move the hare is going to perform and update both the track and position accordingly. The move that is used is based on a random number r between 1 and 10 (inclusive). Print a message saying which action was performed. Use the following table to decide on which action to perform:
Be wary of out-of-bound calculations! If the move makes the hare have a negative position or a position that's beyond the track's size, you need to correct the calculation so that it stays within the track. In other words,
- a negative position needs to round up to position 0; and
- a position larger than the size needs to round down to the last position on the track.
When updating the track, keep in mind if the tortoise is already on the spot, as that will affect what character marker you put in that position.
Lastly, the function needs to return a bool value that represents whether or not the hare has reached the end of the track.
X-------------------------------------------------
BANG!
And they're off!
Tortoise slips!!!
X-------------------------------------------------
Hare fell asleep . . .
X-------------------------------------------------
Tortoise uses slow plod.
HT------------------------------------------------
Hare fell asleep . . .
HT------------------------------------------------
Tortoise uses fast plod!
H---T---------------------------------------------
Hare has a huge slip!!!
H---T---------------------------------------------
Tortoise uses fast plod!
H------T------------------------------------------
Hare performs a big hop!!
-------TH-----------------------------------------
Tortoise uses fast plod!
--------H-T---------------------------------------
Hare slips a small amount.
------H---T---------------------------------------
Tortoise uses slow plod.
------H----T--------------------------------------
Hare hops just a bit.
-------H---T--------------------------------------
Tortoise slips!!!
-------X------------------------------------------
Hare hops just a bit.
-------TH-----------------------------------------
Tortoise slips!!!
---T----H-----------------------------------------
Hare has a huge slip!!!
H--T----------------------------------------------
Tortoise uses slow plod.
H---T---------------------------------------------
Hare hops just a bit.
-H--T---------------------------------------------
Tortoise uses fast plod!
-H-----T------------------------------------------
Hare fell asleep . . .
-H-----T------------------------------------------
Tortoise uses slow plod.
-H------T-----------------------------------------
Hare hops just a bit.
--H-----T-----------------------------------------
Tortoise uses fast plod!
--H--------T--------------------------------------
Hare has a huge slip!!!
H----------T--------------------------------------
Tortoise uses slow plod.
H-----------T-------------------------------------
Hare slips a small amount.
H-----------T-------------------------------------
Tortoise uses slow plod.
H------------T------------------------------------
Hare hops just a bit.
-H-----------T------------------------------------
Tortoise uses fast plod!
-H--------------T---------------------------------
Hare has a huge slip!!!
H---------------T---------------------------------
Tortoise uses fast plod!
H------------------T------------------------------
Hare hops just a bit.
-H-----------------T------------------------------
Tortoise uses fast plod!
-H--------------------T---------------------------
Hare slips a small amount.
H---------------------T---------------------------
Tortoise uses fast plod!
H------------------------T------------------------
Hare fell asleep . . .
H------------------------T------------------------
Tortoise uses fast plod!
H---------------------------T---------------------
Hare hops just a bit.
-H--------------------------T---------------------
Tortoise uses slow plod.
-H---------------------------T--------------------
Hare performs a big hop!!
---------H-------------------T--------------------
Tortoise uses slow plod.
---------H--------------------T-------------------
Hare performs a big hop!!
-----------------H------------T-------------------
Tortoise uses fast plod!
-----------------H---------------T----------------
Hare performs a big hop!!
-------------------------H-------T----------------
Tortoise uses fast plod!
-------------------------H----------T-------------
Hare fell asleep . . .
-------------------------H----------T-------------
Tortoise uses slow plod.
-------------------------H-----------T------------
Hare performs a big hop!!
---------------------------------H---T------------
Tortoise uses fast plod!
---------------------------------H------T---------
Hare fell asleep . . .
---------------------------------H------T---------
Tortoise uses slow plod.
---------------------------------H-------T--------
Hare hops just a bit.
----------------------------------H------T--------
Tortoise uses slow plod.
----------------------------------H-------T-------
Hare hops just a bit.
-----------------------------------H------T-------
Tortoise uses fast plod!
-----------------------------------H---------T----
Hare fell asleep . . .
-----------------------------------H---------T----
Tortoise uses fast plod!
-----------------------------------H------------T-
Hare slips a small amount.
---------------------------------H--------------T-
Tortoise uses fast plod!
---------------------------------H---------------T
Somehow, someway, tortoise has won!!!
This is what's given in the code so you have to finish this up by following the steps given. The Sample run of the code has been given so make sure that your code should match with the sample output:
#include #include #include
using namespace std;
/******************************************************************************* * Function prototypes *******************************************************************************/
void displayTrack(char*, int*); bool moveTortoise(char*, int*, int*); bool moveHare(char*, int*, int*);
/******************************************************************************* * int main() * Starting point of the program. Seed the RNG using the current time. * Creates a race track. Contains logic to simulate the race. Releases * dynamic memory. * * Input(s): * N/A * * Output: * An integer that signals the exit code to the operating system (OS) *******************************************************************************/
int main() { // seed the random number generator using the current time srand(time(0));
// create the track int* trackSize = new int(50); char* track = new char[*trackSize]; *track = 'X'; for (char* ptr = track + 1; ptr *ptr = '-'; }
// display the initial race track // displayTrack(track, trackSize);
// display starting message cout cout
// TODO: game loop
// TODO: release dynamic memory
// terminate return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started