Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You will use random number generation to develop a simulation for the classic race of the tortoise and the hare. The contenders begin the race
You will use random number generation to develop a simulation for the classic race of the tortoise and the hare. The 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. Assume that there is a clock that ticks once per second. With each tick of the clock, your script should adjust the position of the animals according to the rules in Table 1 below Move tvpe Percentage of time 50% Actual move 3 squares to the right 6 squares to the left 1 square to the right Animal Tortoise Fast plod Sli Slow plod 30% Sleep Big hop Big slip Small hop Small slip Hare 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 Table 1: Rules for adjusting 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. If an animal slips left before square 1, move the animal back to square 1. Generate the percentages in Table 1 by producing a random integer i in the range 1 si 10. For the tortoise, perform a "fast plod" when 1 S i* 5, a "slip" when 6 Sis 7, and a "slow plod" when 8 3 i * 10. Use a similar technique to move the hare Provide a button labeled "Start Race", on which the user clicks to start the race. Begirn the race by printing: ON YOUR MARK, GET SET BANG!!! AND THEY RE OFF!!! Then for each tick of the clock (i.e., each repetition of a loop), print a 70-position line showing the letter T in the position of the tortoise and the letter H in the position of the hare. Occasionally, the contenders will land on the same square. In this case, the tortoise bites the hare, and your script should print OUCH!!! at that position. All print positions other than the T, the H, or the OUCH!!! should be blank. After each line is printed, test whether 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. YUCK! If both animals win on the same tick, print IT'S A TIE. Also, print the time elapsed (the number of ticks) of the race. If neither animal wins, perform the loop again to simulate the next tick of the clock. Separate your script (js file) and your CSS rules (.css file - if any) from the HTML5 file Note: Later in the book, we introduce a number of Dynamic HTML capabilities, such as graphics, images, animation and sound. As you study those features, you may enjoy enhancing your tortoise-and-hare contest simulation. [20 marks] You will use random number generation to develop a simulation for the classic race of the tortoise and the hare. The 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. Assume that there is a clock that ticks once per second. With each tick of the clock, your script should adjust the position of the animals according to the rules in Table 1 below Move tvpe Percentage of time 50% Actual move 3 squares to the right 6 squares to the left 1 square to the right Animal Tortoise Fast plod Sli Slow plod 30% Sleep Big hop Big slip Small hop Small slip Hare 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 Table 1: Rules for adjusting 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. If an animal slips left before square 1, move the animal back to square 1. Generate the percentages in Table 1 by producing a random integer i in the range 1 si 10. For the tortoise, perform a "fast plod" when 1 S i* 5, a "slip" when 6 Sis 7, and a "slow plod" when 8 3 i * 10. Use a similar technique to move the hare Provide a button labeled "Start Race", on which the user clicks to start the race. Begirn the race by printing: ON YOUR MARK, GET SET BANG!!! AND THEY RE OFF!!! Then for each tick of the clock (i.e., each repetition of a loop), print a 70-position line showing the letter T in the position of the tortoise and the letter H in the position of the hare. Occasionally, the contenders will land on the same square. In this case, the tortoise bites the hare, and your script should print OUCH!!! at that position. All print positions other than the T, the H, or the OUCH!!! should be blank. After each line is printed, test whether 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. YUCK! If both animals win on the same tick, print IT'S A TIE. Also, print the time elapsed (the number of ticks) of the race. If neither animal wins, perform the loop again to simulate the next tick of the clock. Separate your script (js file) and your CSS rules (.css file - if any) from the HTML5 file Note: Later in the book, we introduce a number of Dynamic HTML capabilities, such as graphics, images, animation and sound. As you study those features, you may enjoy enhancing your tortoise-and-hare contest simulation. [20 marks]
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