Question
C++ Tortoise and Hare Simulation: (The Tortoise and the Hare) In this exercise, you will re-create the classic race of the tortoise and the hare.
C++
Tortoise and Hare
Simulation: (The Tortoise and the Hare) In this exercise, you will re-create the classic race of the tortoise and the hare. You will 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 adjust the position of the animals according to the rules in the following figure.
Rules for moving the tortoise and the hare.
Animal | Move type | Percentage of the time | Actual move |
Tortoise | Fast plod Slip Slow plod | 50% 20% 30% | 3 squares to the right 6 squares to the left 1 square to the right |
Hare | Sleep Big hop Big slip Small hop Small slip | 20% 20% 10% 30% 20% | No move at all 9 squares to the right 12 squares to the left 1 square to the right 2 squares to the left |
Use variables to keep track of the positions of the animals (i.e., position numbers are 0 - 70). Start each animal at position 0 (i.e., the "starting gate"). If an animal slips left before square 0, move the animal back to square 0.
Generate the percentages in the preceding table by producing a random integer i in the range 0 i < 10. For the tortoise, perform a "fast plod" when 0 i < 5, a "slip" when 5 i 6 or a "slow plod" when 7 i 9. 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 hare 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. When you are ready to run your program, assemble a group of fans to watch the race. You'll be amazed how involved the audience gets!
Hint: you may want to write separate functions to simulate tortoise moving and hare moving. For this assignment, you are required to use call-by-reference. Please use the following prototypes to simulate tortoise move and hare move. (You are required to use pass-by-reference in this exercise).
void tortoiseMove(int & tortoisePosition);
void hareMove(int & harePosition);
The flowchart for the main( ) is given as the following:
The flowchart for void tortoiseMove(int & tortoisePosition) is given as the following:
The following is a sample run of the program.
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