(Simulation: The Tortoise and the Hare) In this exercise, you’ll re-create the classic race of the tortoise and the hare. You’ll use random number generation to develop a simulation of this memorable event.

Our contenders begin the race at “square 1” of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 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 second. With each tick of the clock, your program should use function moveTortoise and moveHare to adjust the position of the animals according to the rules in Fig. 7.21. These functions should use pointer-based pass-by-reference to modify the position of the tortoise and the hare.



Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70). 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 ? i ? 10. For the tortoise, perform a “fast plod” when 1 ? i ? 5, a “slip” when 6 ? i ? 7 or a “slow plod” when 8 ? i ? 10. Use a similar technique to move the hare. Begin the race by printing

BANG !!!!!

AND THEY'RE OFF !!!!!

For each tick of the clock (i.e., each repetition of a loop), print a 70-position line showing the letter T in the tortoise’s position and the letter H in the hare’s position. Occasionally, the contenders land on the same square. In this case, the tortoise bites the hare and your program should print OUCH!!! beginning at that position. All print positions other than the T, the H or the OUCH!!! (in

case of a tie) should be blank. After printing each line, test if either animal has reached or passed square 70. If so, print the winner and terminate the simulation. If the tortoise wins, print TORTOISE WINS!!! YAY!!! If the are wins, print Hare wins. Yuch. If both animals win on the same clock tick, you may want to favor the tortoise (the “underdog”), or you may want to print It's a tie. If neither animal wins, perform the loop again to simulate the next tick of the clock.




#include

#include

#include

#include

using namespace std;



const int RACE_END = 70;



// prototypes

void moveTortoise( int *const );

void moveHare( int *const );

void printCurrentPositions( const int *const, const int *const );



int main()

{

int tortoise = 1;

int hare = 1;

int timer = 0;



srand( time( 0 ) );



cout << "ON YOUR MARK, GET SET\nBANG !!!!"

<< "\nAND THEY'RE OFF !!!!\n";



// loop through the events

while ( tortoise != RACE_END && hare != RACE_END )

{

moveTortoise( &tortoise );

moveHare( &hare );

printCurrentPositions( &tortoise, &hare );

timer++;

} // end loop



if ( tortoise >= hare )

cout << "\nTORTOISE WINS!!! YAY!!!\n";

else

cout << "Hare wins. Yuch.\n";



cout << "TIME ELAPSED = " << timer << " seconds" << endl;

} // end main



// progress for the tortoise

void moveTortoise( int * const turtlePtr )

{

int x = 1 + rand() % 10; // random number 1-10



if ( x >= 1 && x <= 5 ) // fast plod

*turtlePtr += 3;

else if ( x == 6 || x == 7 ) // slip

*turtlePtr -= 6;

else // slow plod

++( *turtlePtr );



if ( *turtlePtr < 1 )

*turtlePtr = 1;

else if ( *turtlePtr > RACE_END )

*turtlePtr = RACE_END;

} // end function moveTortoise



// progress for the hare

void moveHare( int * const rabbitPtr )

{

int y = 1 + rand() % 10; // random number 1-10



if ( y == 3 || y == 4 ) // big hop

*rabbitPtr += 9;

else if ( y == 5 ) // big slip

*rabbitPtr -= 12;

else if ( y >= 6 && y <= 8 ) // small hop

++( *rabbitPtr );

else if ( y > 8 ) // small slip

*rabbitPtr -= 2;



if ( *rabbitPtr < 1 )

*rabbitPtr = 1;

else if ( *rabbitPtr > RACE_END )

*rabbitPtr = RACE_END;

} // end function moveHare



// display new position

void printCurrentPositions( const int * const snapperPtr,

const int * const bunnyPtr )

{

if ( *bunnyPtr == *snapperPtr )

cout << setw( *bunnyPtr ) << "OUCH!!!";

else if ( *bunnyPtr < *snapperPtr )

cout << setw( *bunnyPtr ) << 'H'

<< setw( *snapperPtr - *bunnyPtr ) << 'T';

else

cout << setw( *snapperPtr ) << 'T'

<< setw( *bunnyPtr - *snapperPtr ) << 'H';



cout << '\n';

} // end function printCurrentPositions



Computer Science & Information Technology

You might also like to view...

Choosing ____ means that each subsequent frame will contain the next object in the blend.

A. Build B. Sequence C. Pull D. Fill

Computer Science & Information Technology

What will the status of the M and O flags be in the RA-ICMP field if a combination of SLAAC and DHCPv6 are being used?

A. 0, 0 B. 0, 1 C. 1, 0 D. 1, 1

Computer Science & Information Technology

A semicolon is used between two cell references to indicate a range of cells

Indicate whether the statement is true or false.

Computer Science & Information Technology

The _____ function calculates the difference between two dates and shows the result in months, days, or years.

A. nested IF B. CALIF C. DATEDIF D. DATE

Computer Science & Information Technology