Writer's Block - Tortoise & The Hare!

This is a discussion on Writer's Block - Tortoise & The Hare! within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm learning C++ from a textbook & am stumped for ideas with ways to finish this program. I ...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Thumbs up Writer's Block - Tortoise & The Hare!

    Hi all,

    I'm learning C++ from a textbook & am stumped for ideas with ways to finish this program.

    I need to know:

    1) If there's a way to slow program speed so that the race progress is updated per second ( rather than at processor speed ).

    2) A method for clearing the text so that updated race progress is printed on a fresh 'page' ( I hope that makes sense ).

    3) If anybody can see anything I might have missed from my program.

    Here's the code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    
    
    const int finish = 69;
    
    
    
    void moveHare( int *, int[] ); // to move the hare
    void moveTortoise( int *, int[] ); // to move the tortoise
    void displayRace( const int *, const int *, const int[] ); // to display the race
    
    
    
    int main()
    {
    
        srand( time( 0 ) );
    
        int mountain[ 70 ] = { NULL }; // 70 steps to the top of the mountain
        int tortoisePos = 0;
        int harePos = 0;
    
        cout << "BANG !!!!!" << endl << "AND THEY'RE OFF !!!!!" << endl << endl;
    
        while ( tortoisePos < finish && harePos < finish )
        {
    
            moveHare( &harePos, mountain );
            moveTortoise( &tortoisePos, mountain );
            displayRace( &tortoisePos, &harePos, mountain );
            cout << endl;
    
        } // end while
    
    } // end main
    
    
    
    // to display the race
    void displayRace( const int *tortoisePos, const int *harePos, const int mountain[] )
    {
    
        for ( int i = 0; i < finish; i++ )
        {
    
            if ( mountain[ i ] == 0 )
            {
    
                cout << ".";
    
            }
            else if ( mountain[ i ] == 1 )
            {
    
                cout << "T";
    
            }
            else if ( mountain[ i ] == 2 )
            {
    
                cout << "H";
    
            } // end if
    
        } // end for
    
    } // end function displayRace
    
    
    
    // to move the tortoise
    void moveTortoise( int *tortoisePosPtr, int mountain[] )
    {
    
        mountain[ *tortoisePosPtr ] = 0;
    
        int move = 1 + rand() % 10;
    
        switch ( move )
        {
    
            case 1: // fast plod
            case 2:
            case 3:
            case 4:
            case 5: *tortoisePosPtr += 3;
            break;
    
            case 6: // slip
            case 7: if ( *tortoisePosPtr -= 6 < 0 )
                    {
    
                        *tortoisePosPtr = 0;
    
                    }
                    else
                    {
    
                        *tortoisePosPtr -= 6;
    
                    } // end if
            break;
    
            case 8: // slow plod
            case 9:
            case 10: *tortoisePosPtr += 1;
            break;
    
        } // end switch
    
        mountain[ *tortoisePosPtr ] = 1;
    
    } // end function moveTortoise
    
    
    
    // to move the hare
    void moveHare( int *harePosPtr, int mountain[] )
    {
    
        mountain[ *harePosPtr ] = 0;
    
        int move = 1 + rand() % 10;
    
        switch ( move )
        {
    
            case 1: // sleep
            case 2:
            break;
    
            case 3: // big hop
            case 4: *harePosPtr += 9;
            break;
    
            case 5: if ( *harePosPtr -= 12 < 0 ) // big slip
                    {
    
                        *harePosPtr = 0;
    
                    }
                    else
                    {
    
                        *harePosPtr -= 12;
    
                    } // end if
            break;
    
            case 6: // small hop
            case 7:
            case 8: *harePosPtr += 1;
            break;
    
            case 9: // small slip
            case 10: if ( *harePosPtr -= 2 < 0 )
                     {
    
                         *harePosPtr = 0;
    
                     }
                     else
                     {
    
                         *harePosPtr -= 2;
    
                     } // end if
            break;
    
        } // end switch
    
        mountain[ *harePosPtr ] = 2;
    
    } // end function moveHare

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by JM1082 View Post
    1) If there's a way to slow program speed so that the race progress is updated per second ( rather than at processor speed ).
    This is going to be OS/compiler specific. You are going to want to look at Sleep() for windows or sleep() for *nix. Notice the difference in capitilization, it does make a difference.

    Quote Originally Posted by JM1082 View Post
    2) A method for clearing the text so that updated race progress is printed on a fresh 'page' ( I hope that makes sense ).
    Again, this is going to be specific to the OS. For a windows implementation, you could use system("cls") and in a *nix implementation you could try system("clear"). Mind you these are nonstandard however, for a simple program they get the job done.

    Quote Originally Posted by JM1082 View Post
    3) If anybody can see anything I might have missed from my program.
    1. Avoid magic numbers, like in your array declaration.
    2. Any reason you went with const int vice a #define for finish?
    3. Both tortoise and hair positions could just be made static inside your function and then you wouldn't have to pass them as arguments all the time.
    4. Your display function doesn't need the tortoise or hare position.
    5. Why don't you just make your mountain[] a char array and then you could just store the values '.', 'T', and 'H' directly into it?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by JM1082 View Post
    1) If there's a way to slow program speed so that the race progress is updated per second ( rather than at processor speed ).
    Use sleep([n]). Its implementation is OS specific; on *nix the parameter (n) is seconds, on windows milliseconds.

    2) A method for clearing the text so that updated race progress is printed on a fresh 'page' ( I hope that makes sense ).
    Just use an external command via system(). Those are OS specific too, eg on *nix, system("clear").
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    Thanks for your tips guys!

    I particularly like the ideas with the sleep function!

    Just to clarify, I'm using a Windows platform so am I correct in thinking ' sleep( 1000 ) ' will pause the program for 1 second?

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by JM1082 View Post
    Thanks for your tips guys!

    I particularly like the ideas with the sleep function!

    Just to clarify, I'm using a Windows platform so am I correct in thinking ' sleep( 1000 ) ' will pause the program for 1 second?
    Remember what I said, Sleep(1000), not sleep(1000). Also, you will need to include windows.h
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tortoise and Hare Project
    By fenixataris182 in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2005, 01:05 PM
  2. Tortoise/Hare clock question
    By curlious in forum C++ Programming
    Replies: 8
    Last Post: 08-22-2003, 02:44 PM
  3. need help with TGA writer.
    By gunne in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2002, 12:32 PM
  4. 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
  5. Tortoise and the Hare
    By ghettoman in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21