The following is a Javascript class exercise (my first javascript course – which I’m sure you can tell) I have worked on this for well over 100 hours with no luck.
Any help would be GREATLY appreciated. I’m about ready to jump out of the window. Many, many, many, thanks.
Instructions are as follows: <In this problem, you will recreate one of the truly great moments in history, namely 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 script should adjust the position of the animals according to the following rules: Animal Move type Percentage of the time 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 Hare 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 – 70). Start each animal at poisiton 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 and 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!!!” 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!!!” 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 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 of the clock, print “It’s a tie”. If neither animal wins, perform the loop again to simulate the next tick of the clock.>
Code I’ve written so far …
<?xml version = “1.0” encoding = “utf-8”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“
<!– Fig. 10.3: t&H.html –>
<!–tortoise and hare with JavaScript. –>
<html xmlns = “
<head>
<title>Tortoise and hare</title>
<style type = “text/css”>
table { width:10 em}
th { text-align: center}
</style>
<script type = “text/javascript”>
// create (declare) one new array
var posH = H;
var posT = T;
var track = new Array (71); // allocates seventy one elements in Array var raceCounter; // counts number of races finished function startScript ()
// Initialization phase
raceCounter = 0; // prepare to loop
// assign values to elements of Array track
{
for (var i = 0; i < track.length; ++i )
track[i] =i;
}
outputArray( “Array track:”,track );
// output the heading followed by a seven-column table
// containing subscripts and elements of “theArray”
// output posT && posH in Array
function outputArray( heading, theArray )
{
document.writeln( “<h2>” + heading + “</h2>” );
document.writeln( “<table border = “2”” );
document.writeln( “<thead><th>Subscript</th>” +
“<th>Value</th></thead><tbody>” );
// output the subscript and value of array element
for ( var i = 0; i < theArray.length; i++ )
document.writeln( “<tr><td>” + i + “</td><td>” +
theArray[ i ] + “</td></tr>” );
document.writeln( “</tbody></table>” );
}
} // end function outputArray
// function start of race
function raceStarts()
{
document.write(“Starting gate”, “Bang !!!!! And They’re Off !!!!!”); } } // ends function raceStarts
function moveposT()
// moves by posT
{
posT = Math.floor( Math.random( ) * 71 );
}
{
if (1<= i <=5); } // fast plod
posT = posT+3; // move three squares to right
{
if (6<= i <=7); } // slip
posT = posT-6; // move six squares to left
{
if (8<= i <=10); } // slow plod
posT = posT+1; // move one square to right
{
if (posT< 1);} // slips
posT = posT+1; // move one square to right
} // ends function moveposT
// tortoise and hare on same square
{
if (posT = posH)
document.write(“Ouch!!!!”)
}
// ends if
function moveposH()
// moves by posH
{
posH = Math.floor( Math.random( ) * 71 );
}
{
if (1<= i <=2); } // sleep
posH = posH+0; // move zero squares
{
if (3<= i <=4); } // big hop
posH = posH+9; // move nine squares to right
{
if (5<= i <=6); } // big slip
posH= posH+12; // move twelve squares to right
{
if (7<= i <=8); } // small hop
posH = posH+1; // move one square to right
{
if (5<= i <=6); } // small slip
posH = posH+2; // move two squares to left
{
if (posH < 1);} // slips
posH = posH+1; // move one square to right
} // ends function moveposH
// function end of race
function raceEnds()
// declare winner
{
if
(posT > 69)
document.write(“Tortoise wins!!! Yay!!!”);
}
{
if
(posT < 69)
document..write(“Hare wins.Yuck!”);
}
{
if
(posT && posH == 69)
document.write(“It’s a tie.”,”The underdog wins”);
}
} // ends function raceEnds
// Termination phase
if (raceCounter != 0)
{
track = posT * posH;
}
// ends if
// –>
</script>
</head>
<body onload =”startScript();”>
</body>
</html>
Best Regards,
~Bill