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; }



LinkBack URL
About LinkBacks


