Code:
#include <stdio.h>
#include <string.h>
#define STRSIZ 30
int hareFunc( char hareCmd, int harePace );
int tortFunc( char tortCmd, int tortPace );
void raceCheck( int harePace, int tortPace );
int
main(void)
{
char hareCmd, tortCmd;
char hareStr[STRSIZ]="";
char tortStr[STRSIZ]="";
char raceStr[2][STRSIZ + 1]={{'_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_'}, {'_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_'}};
int i = 0, j = 0, c, x = 0, harePace = 0, tortPace = 0;
int m, n;
printf("Enter Hare Commands>");
while ( (c = getchar() ) != '\n')
hareStr[i++] = c;
hareStr[i] = '\0';
printf("Enter Tortoise Commands>");
while ( (c = getchar() ) != '\n')
tortStr[j++] = c;
tortStr[j] = '\0';
printf("BANG! AND THEY ARE OFF!\n");
while( hareStr[x] != '\0' || tortStr[x] != '\0' )
{
hareCmd = hareStr[x];
tortCmd = tortStr[x];
raceStr[0][harePace] = '_';
harePace = hareFunc(hareCmd, harePace);
if ( harePace != EOF)
{
if ( harePace > 30)
harePace = 30;
else if ( harePace < 0)
harePace = 0;
}
raceStr[0][harePace] = 'H';
raceStr[1][tortPace] = '_';
tortPace = tortFunc(tortCmd, tortPace);
if ( tortPace != EOF)
{
if ( tortPace > 30)
tortPace = 30;
else if ( tortPace < 0)
tortPace = 0;
}
raceStr[1][tortPace] = 'T';
for ( m = 0; m < 2; m++)
{
for ( n = 0; n < 31; n++)
{
printf("%c", raceStr[m][n]);
}
printf("\n");
}
printf("\n");
if (harePace == tortPace)
printf("OUCH!!!\n");
if (harePace == 30)
break;
else if (tortPace == 30)
break;
x++;
}
raceCheck( harePace, tortPace);
return(0);
}
int hareFunc( char hareCmd, int harePace )
{
if (hareCmd != '\0')
switch (hareCmd)
{
case 'h':
return(harePace = harePace + 1);
break;
case 'H':
return(harePace = harePace + 9);
break;
case 's':
return(harePace = harePace - 2);
break;
case 'S':
return(harePace = harePace - 12);
break;
case 'D':
return(harePace);
break;
default:
printf("Error: Invalid Command!\n");
return(harePace = -1);
break;
}
}
int tortFunc( char tortCmd, int tortPace )
{
if (tortCmd != '\0')
switch (tortCmd)
{
case 'P':
return(tortPace = tortPace + 3);
break;
case 'p':
return(tortPace = tortPace + 1);
break;
case 'S':
return(tortPace = tortPace - 6);
break;
default:
printf("Error: Invalid Command!\n");
return(tortPace = -1);
break;
}
}
void raceCheck( int harePace, int tortPace)
{
if ( (harePace == 30) || (tortPace == 30))
{
if ( harePace > tortPace)
printf("Hare wins, Yuch!\n");
else if ( harePace < tortPace )
printf("Tortoise wins!!! Yay!\n");
else if ( harePace == tortPace )
printf("Oh my! Its a TIE!!!\n");
}
}