I honestly do not even know where to start with this. I am completely lost and confused except that I know I need to use math.random() and some cases or if-else statements.
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.
This is what I have so far. It does not do anything when I open it in a browser right now and I really don't know where to go from here.
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<title>Tortoise and hare</title>
<body>
<script type = "text/javascript">
function moveT()//tortoise movement function
{
var tmp = Math.floor((Math.random()*10)+1);
if (tmp < 5)
return 3;
if (tmp < 7)
return -6;
return 1;
}
function moveH()//Hare movement function
{
var tmp = Math.floor((Math.random()*10)+1);
if (tmp < 2)
return 0;
if (tmp < 4)
return 9;
if (tmp < 5)
return -12;
if (tmp < 8)
return 1;
return -2;
}
function win()
{
if ( tortoise >= 69 )
{
document.writeln( "TORTOISE WINS!!! YAY!!!" );
winner = true;
}
else if ( hare >= 69 )
{
document.writeln( "Hare wins. Yuck!" );
winner = true;
}
else if ( (tortoise >= 69) && (hare >= 69) )
{
document.writeln( "It's a tie." );
}
}
function draw()
{
for (var i = 0; i < 80; i++)
{
if ( (i == tortoise) && (i == hare) )
{
document.write( "OUCH!!!" );
i += 6;
}
else if ( i == tortoise )
{
document.write( "T" );
}
else if ( i == hare )
{
document.write( "H" );
}
else if ( (i == tortoise) && (i == 69) )
{
document.write( "T" );
}
else if ( (i == hare) && (i == 69) )
{
document.write( "H" );
}
else if ( i == 69 )
{
document.write( "|" );
}
else if ( (i > 69) && (i != tortoise) && (i != hare) )
{
document.write( " " );
}
}
}
Nothing happens because you never call any of the functions at any point. Use the window.setInterval() function to execute a function at specific intervals. You'll need to add something like this:
Code:
function intervalFunc(){
tortoise += moveT();
hare += moveH();
draw();
if ( (tortoise >= 69) || (hare >= 69) )
win();
}
setInterval(intervalFunc, 1000);
And I don't recommend using document.write() when adding stuff to the page in this case. This is a better way if you want to replace everything on the page with something new:
Bookmarks