# Thread: Help Needed: tortoise and the hare simulation

1. Registered User
Join Date
Mar 2009
Posts
4

## Help Needed: tortoise and the hare simulation

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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 10.3: t&H.html -->
<!--tortoise and hare with JavaScript. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<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
{
document.writeln( "<h2>" + heading + "</h2>" );
document.writeln( "<table border = \"2\"" );

// 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>
</body>
</html>

Best Regards,

~Bill

2. ## Something to consider ...

The main problem with you code is that once the page is written and initially displayed,
the next time you try to use document.writeln() function
it will reload the program from the beginning.
I don't think you will ever get out of the starting block.

Consider using <DIV> tags that move about with CSS absolute position changes using JS.

Another search suggestion, try: 'javascript animation' on web or in this forum.

You have a good start, just needs a few modifications.
Good Luck!

3. Registered User
Join Date
Mar 2009
Posts
4

## Need to pay for help now.

Thank you very much for your suggestions however I still can not seem to get it to run. (at all) This is a very important project for me. I'm at a point with no time left and very little money. I have no choice other than to try to pay someone to help me. If you or someone else could give me a price to get this code working as per the directions posted, please let me know. I don’t know what else to do. Open to any and all additional suggestions. Thank you again.

4. Registered User
Join Date
Mar 2009
Posts
4

## Still needing additional help ...

Thank you very much for your suggestions however after making the suggested changes I still can not get it to run (at all). This is a very important project for me. I'm at a point with no time left so any additional help would be appreciated immensely. I tried to post for tutoring help need (from you or someone you know) but that post was pulled. If you or someone else could give me any other ideas as to how to get the code working as per the directions posted, I will dance at your next family wedding. However you should now, I'm not much of a dancer either. I'm open to any and all additional suggestions. (no throwing my box out of the window is not an option .. just yet anyway) Thanks a million.

5. Registered User
Join Date
Mar 2009
Posts
4
Haven't given up ... yet

Questions: Do the elseif(no space)statements or the else if(space) statements make a difference? How can you tell how many braces you need for the else if statements? How can I output the moves inside the array? thanks.

6. Registered User
Join Date
Jan 2012
Posts
2

## Same Situation...

Hi im in the exact same situation as you were 2 years ago...if you see this, can you help me out? It's due in 3 days. Thanks.

7. Originally Posted by jasonli2310
Hi im in the exact same situation as you were 2 years ago...if you see this, can you help me out? It's due in 3 days. Thanks.
Most of the solution was given in post #2.
If you want some help, post your changes ...

8. Registered User
Join Date
Jan 2012
Posts
2
It's okay, i figured it out. Yea, it sucks having to rely on others. A lot better if i just did it on my own for my deadlines.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.12691 seconds
• Memory Usage 2,937KB
• Queries Executed 15 (?)
Template Usage (33):
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (8)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (8)postbit
• (8)postbit_onlinestatus
• (8)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete