The Tortoise and the Hare, a race simulation.
In this problem, you’ll recreate a version of the classic race of the tortoise and the hare. You’ll use a 𝑟𝑎𝑛𝑑𝑜𝑚 𝑛𝑢𝑚𝑏𝑒𝑟 𝑔𝑒𝑛𝑒𝑟𝑎𝑡𝑜𝑟 to simulate the race. The contenders begin race along a track of 100 squares. Each square represents a possible position along the race course. The finish line is at square 100. The first contender to reach or pass square 100 wins. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground. A clock ticks once per second. With each tick of the clock, your application should adjust the position of the animals according to the rules in Figure 8. Use variables to keep track of the positions of the animals (i.e., position numbers are 1–
100). Start each animal at position 1 𝑡ℎ𝑒 “𝑠𝑡𝑎𝑟𝑡𝑖𝑛𝑔 𝑔𝑎𝑡𝑒” . If an animal slips before square 1, move it back to square 1.
| Racer | Move Type | % of time | Direction | Squares to move and direction |
| Sleep | 10% | – | No movement | |
| Jump | 40% | Forward | Random # of Squares between 1 and 3 | |
| Slip | 30% | Backwards | Random # of Squares between 1 and 6 | |
| Walk | 20% | Forward | Random # of Squares between 0 and 1 | |
| Sleep | 10% | – | No movement | |
| Jump | 30% | Forward | Random # of Squares between 1 and 5 | |
| Small slip | 20% | Backwards | Random # of Squares between 1 and 2 | |
| Big Slip | 10% | Backwards | Random # of Squares between 1 and 7 | |
| Walk | 30% | Forward | Random # of Squares between 0 and 1 |
Figure 1: Rules for adjusting the positions of the Tortoise and the Hare.
Hint:
- Generate the percentages in Figure 1 by producing a random integer 𝑖 in the range 0 𝑖 9. For the Tortoise, perform a “𝐽𝑢𝑚𝑝” when 0 𝑖 3, a “𝑠𝑙𝑖𝑝” when 4 𝑖6, a “𝑤𝑎𝑙𝑘” when 7 𝑖8, or a sleep when 𝑖 9.
- Since you’re using random numbers, outputs will be different.
- To avoid long‐running simulations, stop the simulation if it goes over 200 iterations.
- A unit of time is one full iteration in the program. i.e. a counter shows how many iterations were made until one contestant wins. If the simulation takes 20 iterations, then this is treated as 20 seconds.
- Think of the 100‐square track as shown below. A forward move means, move up the track, a backward move means move down the track.
| start | … | finish |
0 1 2 3 4 5 6 7 8 9 10 … 99
𝑓𝑜𝑟𝑤𝑎𝑟𝑑 𝑚𝑜𝑣𝑒𝑠
Figures:
𝑂𝑁 𝑌𝑂𝑈𝑅 𝑀𝐴𝑅𝐾, 𝐺𝐸𝑇 𝑆𝐸𝑇 𝐵𝐴𝑁𝐺 !!!!!
𝐴𝑁𝐷 𝑇𝐻𝐸𝑌′𝑅𝐸 𝑂𝐹𝐹 !!!!!
Figure 2: initial message printed when the race starts
Figure 3: With every iteration, the racer’s position is printed. Positions differ with every iteration and each simulation.
𝑇𝑂𝑅𝑇𝑂𝐼𝑆𝐸 𝑊𝐼𝑁𝑆!!! 𝑌𝐴𝑌!!!
𝑇𝑖𝑚𝑒 𝐸𝑙𝑎𝑝𝑠𝑒𝑑 766 𝑠𝑒𝑐𝑜𝑛𝑑𝑠
Figure 4: Message if the Tortoise wins. Time differs for each simulation.
𝐻𝑎𝑟𝑒 𝑤𝑖𝑛𝑠. 𝑌𝑢𝑐ℎ!
| 𝑇𝑖𝑚𝑒 𝐸𝑙𝑎𝑝𝑠𝑒𝑑
𝐼𝑡′𝑠 𝑎 𝑡𝑖𝑒 |
305 𝑠𝑒𝑐𝑜𝑛𝑑𝑠
Figure 5: Message if the Hare wins. Time differs for each simulation. |
| 𝑇𝑖𝑚𝑒 𝐸𝑙𝑎𝑝𝑠𝑒𝑑 | 527 𝑠𝑒𝑐𝑜𝑛𝑑𝑠
Figure 6: Message if a tie occurs. Time differs for each simulation. |
𝑇𝑖𝑚𝑒 𝑂𝑢𝑡!
𝐻𝑎𝑟𝑒 𝑤𝑖𝑛𝑠. 𝑌𝑢𝑐ℎ!
𝑇𝑖𝑚𝑒 𝐸𝑙𝑎𝑝𝑠𝑒𝑑 2000 𝑠𝑒𝑐𝑜𝑛𝑑𝑠
Figure 7: Message if a timeout occurs after 2000 iterations (seconds). The winner message is also printed.
Figure 8: Class Diagram – UML legend shown below
UML Diagram Legend
| Symbol | Description |
| Underlined | Indicates a static member |
| A private member (i.e. variable or method) | |
| A private final member (i.e. variable) | |
| A public field (i.e. variable or method) | |
| A public abstract member (i.e. variable or method) | |
| A public constructor | |
| A static public member | |
| An interface | |
| A public class | |
| A public abstract class | |
| A hollowed arrow indicates inheritance | |
| An open-ended arrow indicates composition | |
| A dotted line and hollowed arrow indicate class implementation |





