Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Tortoise and the Rabbit Race Simulation In this problem, you will re-create one of the truly great moments in history, namely the classic race

The Tortoise and the Rabbit

Race Simulation

In this problem, you will re-create one of the truly great moments in history, namely the classic race of the tortoise and the rabbit. You will use random number generation to develop a simulation of this memorable event.

Our contenders begin the race at "square 1" of trackLength squares. Each square represents a possible position along the race course. The finish line is at square trackLength. The first contender to reach or pass square trackLength is rewarded with a pail of fresh carrots and lettuce, The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground. There is a clock that ticks once per 500 Milliseconds. With each tick of the clock, your script should adjust the position of the animals according to the following rules:

Animal Move type Percentage Actual move

Tortoise Fast plod 50% 3 squares to the right

Slip 20% 6 squares to the left

Slow plod 30% 1 square to the right

Rabbit Sleep 20% No move at all

Big hop 20% 9 squares to the right

Big slip 10% 12 squares to the left

Small hop 30% 1 square to the right

Small slip 20% 2 squares to the left

Use variables to keep track of the positions of the animals (i.e., position numbers are 1 to trackLength). Start each animal at position 1 (i.e., the "starting gate"). If an animal slips left before square 1, move the animal back to square 1. Generate the percentages in the preceding table by producing a random integer i in the range 1 to 10. For the tortoise, perform a "fast plod" when i is 1 to 5, a "slip" when i is 6 to 7 and a "slow plod" when i is 8 to 10. Use a similar technique to move the rabbit. Begin the race by printing

BANG !!!!!

AND THEY'RE OFF !!!!!

Then, for each tick of the clock (i.e., each repetition of a loop), print a the animal's position by showing the animal's image. Occasionally, the contenders will land on the same square. In this case, the tortoise bites the rabbit, and your script should print OUCH!!!. After each line is printed, test whether either animal has reached or passed square trackLength. If so, print the winner, and terminate the simulation. If the tortoise wins, print TORTOISE WINS!!! YAY!!! If the rabbet wins, print Rabbet wins. Yuch. If both animals win on the same tick of the clock, you may want to favor the tortoise, or you may want to print It's a tie.

Sample output:

Define a class called Shape with only one public member function

virtual void move(int&, int&) = 0;

Define a class called Tortoise which publicly inherits the class Shape, and has the void public member function move(int&, int&).

Define a class called Rabbit which publicly inherits the class Shape, and has the void public member function move(int&, int&).

The following is part of the main function:

int main()

{

int tPosition=0, rPosition=0, finish = 50;

Shape *ptrArray[2];

ptrArray[0] = new Tortoise;

ptrArray[1] = new Rabbit;

srand(time(0));

for(int i=0; i<200; i++)

{

clrscr();

ptrArray[i%2]->move(tPosition, rPosition);

if(tPosition == rPosition && tPosition >= finish)

{

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago