dcsimg
www.webdeveloper.com
Results 1 to 8 of 8

Thread: Help Needed: tortoise and the hare simulation

  1. #1
    Join Date
    Mar 2009
    Posts
    4

    Question 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">
    <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

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440

    Exclamation 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. #3
    Join Date
    Mar 2009
    Posts
    4

    Unhappy 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. #4
    Join Date
    Mar 2009
    Posts
    4

    Red face 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. #5
    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. #6
    Join Date
    Jan 2012
    Posts
    2

    Unhappy 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. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440

    Exclamation

    Quote Originally Posted by jasonli2310 View Post
    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 ...
    don't just ask for someone to do your work for you on a deadline!

  8. #8
    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.

Thread Information

Users Browsing this Thread

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
  •  
HTML5 Development Center



Recent Articles