Thread: The hare and the tortoise

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    Exclamation The hare and the tortoise

    Hi all,

    I wrote a program for the "tortoise and hare race". I need some help with it. The program works without a problem but it runs step by step with me having to press enter for each step. I want it to run automatically by just pressing enter once at the start. Could you guys help me out?

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int random_1( void );
    int move_tortoise( int );
    int move_hare( int );
    int end_condition( int, int );
    
    int main( void )
    {
    int i = 0;
    int j = 0;
    char play_again = '\n';
    
    printf( "BANG !!!!!\n" );
    printf( "AND THEY'RE OFF !!!!!\n" );
    
    while ( play_again == '\n' )
    {
    while ( i <= 69 && j <= 69 )
    {
    char line[ 71 ] =
    "----------------------------------------------------------------------";
    
    i = move_tortoise( i );
    j = move_hare( j );
    
    if ( i == j )
    {
    
    line[ i + 0 ] = 'O';
    line[ i + 1 ] = 'U';
    line[ i + 2 ] = 'C';
    line[ i + 3 ] = 'H';
    }
    else
    {
    
    line[ i ] = 'T';
    line[ j ] = 'H';
    }
    
    printf( "%s\n\n", line );
    
    break;
    }
    
    if ( end_condition( i, j ) )
    break;
    
    printf( "Press enter" );
    scanf( "%c", &play_again );
    }
    return 0;
    }
    
    int random_1( void )
    {
    
    srand( time( NULL ) );
    return ( 1 + rand() % 10 );
    }
    
    int move_tortoise( int i )
    {
    int check = random_1();
    
    if ( check == 1 || check == 2 || check == 3 || check == 4 || check
    == 5 )
    i += 3;
    
    if ( check == 6 || check == 7 )
    i -= 6;
    
    if ( check == 8 || check == 9 || check == 10 )
    i += 1;
    
    if ( i < 0 )
    {
    i = 0;
    }
    return i;
    }
    
    int move_hare( int j )
    {
    int check = random_1();
    
    if ( check == 3 || check == 4 )
    j += 9;
    
    if ( check == 5 )
    j += 12;
    
    if ( check == 6 || check == 7 || check == 8 )
    j++;
    
    if ( check == 9 || check == 10 )
    j -= 2;
    
    if ( j < 0 )
    {
    j = 0;
    }
    return j;
    }
    
    int end_condition( int i, int j )
    {
    if ( j >= 69 && i >= 69 )
    {
    printf( "IT'S A TIE\n" );
    return 1;
    }
    else if ( i >= 69 )
    {
    printf( "TORTOISE WINS!!! YAY!!!\n" );
    return 1;
    }
    else if ( j >= 69 )
    {
    printf( "HARE WINS. YUCH.\n" );
    return 1;
    }
    return 0;
    }
    

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    First help yourself and the rest of the world with reading about Indent_style .

    I really tried to read your code, but.... it is too hard for me :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The lines

    Code:
    printf( "Press enter" ); 
    scanf( "%c", &play_again );
    
    
    seem to be the only place where you wait for user input. So it must be here that you need to make a change.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Heed the advice of std10093. The only reason I went through your code is because I'm feeling generous this morning.

    Code:
    srand( time( NULL) );
    This should only be called once, at the start of your program (near the top of "main()").

    Code:
    while ( i <= 69 && j <= 69 )
    {
        // ....
    
        break;
    }
    The unconditional "break" at the end of this "while()" loop body ensures that the loop is breaking every time - hence, it's not really acting like a loop. Comment out this "break" for now.

    Code:
    printf( "Press enter" );
    scanf( "%c", &play_again );
    As c99tutorial pointed out, this is where you're looking for user input, so this is where a change is needed. Try commenting these out as an experiment.

    Try the program again after making these modifications and let us know what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writer's Block - Tortoise & The Hare!
    By JM1082 in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2011, 03:21 PM
  2. Tortoise and Hare Project
    By fenixataris182 in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2005, 12:05 PM
  3. Tortoise/Hare clock question
    By curlious in forum C++ Programming
    Replies: 8
    Last Post: 08-22-2003, 01:44 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, 01:26 PM

Tags for this Thread