For some reason, it prints funny characters ???
hi,
This program prints funny characters which it shouldn't be doin it. And whenever i change something in the program, the output would change to different set of funny characters. Pls tell me whats wrong with the app. Thnx in advance!
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 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, ¤tPos_tortoise, ¤tPos_hare );
printPositions( positions, SIZE );
}
switch ( whoWins ) {
case 1:
printf( "\n\nTurtle wins!\n" );
break;
case 2:
printf( "\n\nHare wins!\n" );
break;
case 3:
printf( "\n\nIt'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 ( i >= 1 && i <= 5 )
*currentPos_Tortoise += moveTortoise[ 0 ];
else if ( i >= 6 && i <= 7 )
if ( *currentPos_Tortoise != 1 )
*currentPos_Tortoise += moveTortoise[ 1 ];
else if ( i >= 8 && i <= 10)
*currentPos_Tortoise += moveTortoise[ 2 ];
if ( i >= 1 && i <= 2 )
*currentPos_Hare += moveHare[ 0 ];
else if ( i >= 3 && i <= 4 )
*currentPos_Hare += moveHare[ 1 ];
else if ( i == 5 )
if ( *currentPos_Hare != 1 )
*currentPos_Hare += moveHare[ 2 ];
else if ( i >= 6 && i <= 8 )
*currentPos_Hare += moveHare[ 3 ];
else if ( i >= 9 && i <= 10 )
if ( *currentPos_Hare != 1 )
*currentPos_Hare += moveHare[ 4 ];
printf( "\nCurrent Pos of tortoise = %d ", *currentPos_Tortoise );
printf( "Current Pos of hare = %d", *currentPos_Hare );
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 ] );
}