Thread: Tortoise and Hare Project

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Tortoise and Hare Project

    I have this tortoise and hare project. I'm having trouble with moving the hare and testing it's position. Am I doing anything else wrong?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    #include <cstdlib>
    
    #include <ctime>
    
    #include <iomanip>
    
    using std::setw;
    
    const int RACE_END = 70;
    
    double movetortoise ( int x )
    double movehare ( int y )
    double printCurrentPositions ( int t, int h )
    
    int main()
    {
       int tortoise = 1; 
       int hare = 1;
       int timer = 0;
    
       srand( time( 0 ) );
    
       cout << "ON YOUR MARK, GET SET\nBANG               !!!!"
            << "\nAND THEY’RE OFF    !!!!\n";
       
       // controls race
       while ( tortoise != RACE_END && hare != RACE_END ) {
          movetortoise ( x )
          movehare ( y )
          printCurrentPositions ( t, h )   
          ++timer;
    
       } // end while
    
       // determine winner
       if ( tortoise >= hare )
          cout << "\nTORTOISE WINS!!! YAY!!!\n";
    
       else
          cout << "Hare wins. Yuch.\n";
    
       cout << "TIME ELAPSED = " << timer << " seconds" << endl;
    
       return 0;
    
       
    } // end main
    
    // move tortoise
    double movetortoise ( int x )
    {
       int x = 1 + rand() % 10;
    
       // determine which move to make
       if ( x >= 1 && x <= 5 )        // fast plod
          turtlePtr += 3;
    
       else if ( x == 6 || x == 7 )   // slip
          turtlePtr -= 6;
    
       else                           // slow plod
          ++( turtlePtr );
       
       // ensure that tortoise remains within subscript range
       if ( turtlePtr < 1 )
          turtlePtr = 1;
    
       else if ( turtlePtr > RACE_END )
          turtlePtr = RACE_END;
    
    } // end function moveTortoise
    
    // move hare
    double movehare ( int y )
    {
       int y = 1 + rand() % 10;
    
       /* Write statements that move hare */
    
       /* Write statements that test if hare is before
          the starting point or has finished the race */
    
    } // end function moveHare
    
    // output positions of animals
    double printCurrentPositions ( int t, int h )
    {
       if ( bunnyPtr == snapperPtr ) 
          cout << setw( bunnyPtr ) << "OUCH!!!";      
    
       else if ( bunnyPtr < snapperPtr ) 
          cout << setw( bunnyPtr ) << "H" 
               << setw( snapperPtr - bunnyPtr ) << "T";
    
       else
          cout << setw( snapperPtr ) << "T" 
               << setw( bunnyPtr - snapperPtr ) << "H";
    
       cout << "\n";
    
    } // end function printCurrentPositions

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    At the very least you have numerous undeclared variables in your code along with some missing return statements.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <iomanip>
    
    using std::cout;
    using std::endl;
    using std::setw;
    
    const int RACE_END = 70;
    
    double movetortoise ( int x )
    double movehare ( int y )
    double printCurrentPositions ( int t, int h )
    
    int main()
    {
       int tortoise = 1; 
       int hare = 1;
       int timer = 0;
    
       srand( time( 0 ) );
    
       cout << "ON YOUR MARK, GET SET\nBANG               !!!!"
            << "\nAND THEY’RE OFF    !!!!\n";
       
       // controls race
       while ( tortoise != RACE_END && hare != RACE_END ) {
          movetortoise ( x )
          movehare ( y )
          printCurrentPositions ( t, h )   
          ++timer;
    
       } // end while
    
       // determine winner
       if ( tortoise >= hare )
          cout << "\nTORTOISE WINS!!! YAY!!!\n";
    
       else
          cout << "Hare wins. Yuch.\n";
    
       cout << "TIME ELAPSED = " << timer << " seconds" << endl;
    
       return 0;
    
       
    } // end main
    
    // move tortoise
    double movetortoise ( int x )
    {
        // You need to maybe think about a different name for this variable,
        // it interferes with the variable being passed in
        int x = 1 + rand() % 10;  
    
       // determine which move to make
       if ( x >= 1 && x <= 5 )        // fast plod
          turtlePtr += 3;
    
       else if ( x == 6 || x == 7 )   // slip
          turtlePtr -= 6;
    
       else                           // slow plod
          ++( turtlePtr );
       
       // ensure that tortoise remains within subscript range
       if ( turtlePtr < 1 )
          turtlePtr = 1;
    
       else if ( turtlePtr > RACE_END )
          turtlePtr = RACE_END;
    
        // What is turtlePtr?  It isn't declared anywhere.
    
        // You need to return a value
    
    } // end function moveTortoise
    
    // move hare
    double movehare ( int y )
    {
        // You need to maybe think about a different name for this variable
        // it interferes with the variable being passed in
        int y = 1 + rand() % 10;
    
       /* Write statements that move hare */
    
       /* Write statements that test if hare is before
          the starting point or has finished the race */
    
        // You need to return a value
    
    } // end function moveHare
    
    // output positions of animals
    double printCurrentPositions ( int t, int h )
    {
       if ( bunnyPtr == snapperPtr ) 
          cout << setw( bunnyPtr ) << "OUCH!!!";      
    
       else if ( bunnyPtr < snapperPtr ) 
          cout << setw( bunnyPtr ) << "H" 
               << setw( snapperPtr - bunnyPtr ) << "T";
    
       else
          cout << setw( snapperPtr ) << "T" 
               << setw( bunnyPtr - snapperPtr ) << "H";
    
        // What are bunnyPtr and snapperPtr?  They aren't declared anywhere.
    
       cout << "\n";
    
        // You need to return a value
    
    } // end function printCurrentPositions
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    A couple of things caught my eye, unless you are going to move less than a full unit, you should change the functions to
    Code:
    int functionname
    instead of double. Additionally, as you can see the hare function is not filled out, I don't know if you knew that or not, but pointing it out nonetheless. Also, do you need to know how much you have moved the hare and tortise or do you just need to set the current positions? It looks like the functions do not return anything and do not actually move the tortise or hare at all. The hare and turtle pointer are not being sent to the functions at all.

    You would need something more along the lines of this...

    Code:
    int * turtlepointer = 1;
    int * harepointer = 1;
    ...
    
    void movehare( int * harepointer )
    {
    //do stuff to harepointer
    }
    OR

    Code:
    int hare = 0;
    int tortise = 0;
    
    ...
    
    hare = hare + movehare( x );
    
    ...
    
    int movehare( int x )
    {
    //do stuff
    return modified_x;
    }
    Edit: Looks like hk beat me to the punch

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    I've done some more work on it. now when i run it it just says ouch!!! again and again. It's supposed to simulate a tortoise and hare race. Can someone help me please?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    #include <cstdlib>
    
    #include <ctime>
    
    #include <iomanip>
    
    using std::setw;
    
    const int RACE_END = 70;
    
    void movetortoise ( int turtlePtr );
    void movehare ( int harePtr );
    void printCurrentPositions ( int bunnyPtr, int snapperPtr );
    
    int main()
    {
       int tortoise = 1; 
       int hare = 1;
       int timer = 0;
    
       srand( time( 0 ) );
    
       cout << "ON YOUR MARK, GET SET\nBANG               !!!!"
            << "\nAND THEY’RE OFF    !!!!\n";
       
       // controls race
       while ( tortoise != RACE_END && hare != RACE_END ) {
          movetortoise ( tortoise );
          movehare ( hare );
          printCurrentPositions ( tortoise, hare );
          ++timer;
    
       } // end while
    
       // determine winner
       if ( tortoise >= hare )
          cout << "\nTORTOISE WINS!!! YAY!!!\n";
    
       else
          cout << "Hare wins. Yuch.\n";
    
       cout << "TIME ELAPSED = " << timer << " seconds" << endl;
    
       return 0;
    
       
    } // end main
    
    // move tortoise
    void movetortoise ( int turtlePtr )
    {
       int x = 1 + rand() % 10;
    
       // determine which move to make
       if ( x >= 1 && x <= 5 )        // fast plod
          turtlePtr += 3;
    
       else if ( x == 6 || x == 7 )   // slip
          turtlePtr -= 6;
    
       else                           // slow plod
          ++( turtlePtr );
       
       // ensure that tortoise remains within subscript range
       if ( turtlePtr < 1 )
          turtlePtr = 1;
    
       else if ( turtlePtr > RACE_END )
          turtlePtr = RACE_END;
    
    } // end function moveTortoise
    
    // move hare
    void movehare ( int harePtr )
    {
       int y = 1 + rand() % 10;
    
       if ( y == 1 || y == 2 )   // sleep
          harePtr += 0;
    
        else if ( y == 3 || y == 4 )   // big hop
          harePtr += 9;
    
    	else if ( y == 5 )   // big slip
          harePtr -= 12;
    
    	else if ( y >= 6 && y <= 8 )   // small hop
          harePtr += 1;
    
    	else // small slip 
    harePtr -= 2;
       
    	if ( harePtr < 1 )
          harePtr = 1;
    
       else if ( harePtr > RACE_END )
          harePtr = RACE_END;
    
    } // end function moveHare
    
    // output positions of animals
    void printCurrentPositions ( int bunnyPtr, int snapperPtr )
    {
       if ( bunnyPtr == snapperPtr ) 
          cout << setw( bunnyPtr ) << "OUCH!!!";      
    
       else if ( bunnyPtr < snapperPtr ) 
          cout << setw( bunnyPtr ) << "H" 
               << setw( snapperPtr - bunnyPtr ) << "T";
    
       else
          cout << setw( snapperPtr ) << "T" 
               << setw( bunnyPtr - snapperPtr ) << "H";
    
       cout << "\n";
    
    } // end function printCurrentPositions

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One thing I noticed . . .
    Code:
    while ( tortoise != RACE_END && hare != RACE_END ) {
    The hare could go past the finish line in one hop . . . you want this:
    Code:
    while ( tortoise < RACE_END && hare < RACE_END ) {
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well of course it prints OUCH.
    Code:
    int tortoise = 1;
    int hare = 1;
    // ...
    printCurrentPositions ( tortoise, hare );
    // ...
    void printCurrentPositions ( int bunnyPtr, int snapperPtr )
    {
       if ( bunnyPtr == snapperPtr ) 
          cout << setw( bunnyPtr ) << "OUCH!!!";
    It's like saying if(1 == 1).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Race between the hare and the tortoise: not working...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-14-2002, 12:29 PM
  2. Tortoise and the Hare
    By ghettoman in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 01:26 PM