Race between the hare and the tortoise: not working...

This is a discussion on Race between the hare and the tortoise: not working... within the C Programming forums, part of the General Programming Boards category; Hi, This is a program i wrote to simulate the race between the hare and the tortoise. But for some ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Race between the hare and the tortoise: not working...

    Hi,

    This is a program i wrote to simulate the race between the hare and the tortoise. But for some reason it prints nothing. And i don't know why. Pls help me to correct this program and tell me which part is wrong ( it's my first try to use pointers...). Thnx in advance.

    also, i keep getting compile error: warning: assignment makes integer from pointer without a cast


    Code:
    /* Race between the tortoise and the hare */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 70
    
    void printPositions( const char positions[], int arraySize );
    int moveAnimals( char positions[], int *currentPos_Tortoise, int *currentPos_Hare );
    
    int main()
    {
       char positions[ SIZE ];
       int end = 0;
       int i;
       int whoWins = 0;
       int currentPos_tortoise = 1, currentPos_hare = 1;
    
       for ( i = 0; i < SIZE; i++ )
          positions[ i ] = " ";
    
       printf( "***---Race---***\n\n" );
    
       srand( time( NULL ) );
    
       while ( whoWins = 0 ) {
          whoWins = moveAnimals( positions, &currentPos_tortoise, &currentPos_hare );
          printPositions( positions, SIZE );
       }
    
       switch ( whoWins ) {
          case 1:
             printf( "Turtle wins!\n" );
             break;
          case 2:
             printf( "Hare wins!\n" );
             break;
          case 3:
             printf( "It's a tie.\n" );
             break;
       }
    
       getch();
       return 0;
    }
    
    int moveAnimals( char positions[], int *currentPos_Tortoise, int *currentPos_Hare )
    {
       static int moveTortoise[ 3 ] = { 3, -6, 1 };
       static int moveHare[ 5 ] = { 0, 9, -12, 1, -2 };
       int i;
    
       i = 1 + rand() % 10;
    
       if ( 1 <= i <= 5 )
          *currentPos_Tortoise += moveTortoise[ 0 ];
       else if ( 6 <= i <= 7 )
          *currentPos_Tortoise += moveTortoise[ 1 ];
       else if ( 8 <= i <= 10 )
          *currentPos_Tortoise += moveTortoise[ 2 ];
    
       if ( 1 <= i <= 2 )
          *currentPos_Hare += moveHare[ 0 ];
       else if ( 3 <= i <= 4 )
          *currentPos_Hare += moveHare[ 1 ];
       else if ( i == 5 )
          *currentPos_Hare += moveHare[ 2 ];
       else if ( 6 <= i <= 8 )
          *currentPos_Hare += moveHare[ 3 ];
       else if ( 9 <= i <= 10 )
          *currentPos_Hare += moveHare[ 4 ];
    
       for ( i = 0; i < 70; i++ )
          positions[ i ] = " ";   /* Clearing up the array */
    
       if ( *currentPos_Tortoise == *currentPos_Hare )
          positions[ *currentPos_Tortoise ] = "!";
       else {
          positions[ *currentPos_Tortoise ] = "T";
          positions[ *currentPos_Hare ] = "H";
       }
    
       if ( *currentPos_Tortoise >= 70 || *currentPos_Hare >= 70 )
          return 3;   /* it's a tie */
       else if ( *currentPos_Tortoise >= 70 )
          return 1;   /* turtle wins */
       else if ( *currentPos_Hare >= 70 )
          return 2;   /* hare wins */
       else
          return 0;   /* nobody wins yet */
    }
    
    void printPositions( const char positions[], int arraySize )
    {
       int i;
    
       printf( "\n" );
    
       for ( i = 0; i < arraySize; i++ )
          printf( "%c ", positions[ i ] );
    }
    Last edited by Nutshell; 01-14-2002 at 02:15 AM.

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok now i fixed the while part and now there is something being printed out. But why are most the of the characters pritned out are all 'r' and some 'v's ??? No idea!

    Pls tell mw whats wrong!

    thnx

    Code:
    /* Race between the tortoise and the hare */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 70
    
    void printPositions( const char positions[], int arraySize );
    int moveAnimals( char positions[], int *currentPos_Tortoise, int *currentPos_Hare );
    
    int main()
    {
       char positions[ SIZE ];
       int end = 0;
       int i;
       int whoWins = 0;
       int currentPos_tortoise = 1, currentPos_hare = 1;
    
       printf( "***---Race---***\n\n" );
    
       printf( "BANG!!!\nAND THEY'RE OFF!!!\n\n" );
    
       srand( time( NULL ) );
    
       while ( whoWins == 0 ) {
          whoWins = moveAnimals( positions, &currentPos_tortoise, &currentPos_hare );
          printPositions( positions, SIZE );
       }
    
       switch ( whoWins ) {
          case 1:
             printf( "Turtle wins!\n" );
             break;
          case 2:
             printf( "Hare wins!\n" );
             break;
          case 3:
             printf( "It's a tie.\n" );
             break;
       }
    
       getch();
       return 0;
    }
    
    int moveAnimals( char positions[], int *currentPos_Tortoise, int *currentPos_Hare )
    {
       static int moveTortoise[ 3 ] = { 3, -6, 1 };
       static int moveHare[ 5 ] = { 0, 9, -12, 1, -2 };
       int i;
    
       i = 1 + rand() % 10;
    
       if ( 1 <= i <= 5 )
          *currentPos_Tortoise += moveTortoise[ 0 ];
       else if ( 6 <= i <= 7 )
          *currentPos_Tortoise += moveTortoise[ 1 ];
       else if ( 8 <= i <= 10 )
          *currentPos_Tortoise += moveTortoise[ 2 ];
    
       i = 1 + rand() % 10;
    
       if ( 1 <= i <= 2 )
          *currentPos_Hare += moveHare[ 0 ];
       else if ( 3 <= i <= 4 )
          *currentPos_Hare += moveHare[ 1 ];
       else if ( i == 5 )
          *currentPos_Hare += moveHare[ 2 ];
       else if ( 6 <= i <= 8 )
          *currentPos_Hare += moveHare[ 3 ];
       else if ( 9 <= i <= 10 )
          *currentPos_Hare += moveHare[ 4 ];
    
       for ( i = 0; i < 70; i++ )
          positions[ i ] = "-";   /* Clearing up the array */
    
       if ( *currentPos_Tortoise == *currentPos_Hare )
          positions[ *currentPos_Tortoise ] = "!";
       else {
          positions[ *currentPos_Tortoise ] = "T";
          positions[ *currentPos_Hare ] = "H";
       }
    
       if ( *currentPos_Tortoise >= 70 || *currentPos_Hare >= 70 )
          return 3;   /* it's a tie */
       else if ( *currentPos_Tortoise >= 70 )
          return 1;   /* turtle wins */
       else if ( *currentPos_Hare >= 70 )
          return 2;   /* hare wins */
       else
          return 0;   /* nobody wins yet */
    }
    
    void printPositions( const char positions[], int arraySize )
    {
       int i;
    
       printf( "\n\n" );
    
       for ( i = 0; i < arraySize; i++ )
          printf( "%c ", positions[ i ] );
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Cool tortoise and the hare problems

    One of your problems is how you are assigning values to your character array: Remember that when you assign single characters, they should be enclosed in single quotes, not double quotes:

    "Apple" -- a string (can't be assigned)
    'a' -- a character (can be assigned)

    BAD: mystring = "apple"; /* not permitted in C */
    GOOD: mystring[0] = 'a';

    Hope this helps

    Walter

Popular pages Recent additions subscribe to a feed

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