Im making a hangman game in C, and two of its functions dont seem to be working. The code is below, the troublemakers are int check_guess and int hidden_word the others seem to work fine on their own. For some reason the check_guess function doesnt determine if a guess is correct or not and the hidden_word function barely works.
int main(int argc, char **argv)
{char wordfile[256], option, temp[256] ;
char wordlist[MAX_WORDS][MAX_WORD_LENGTH] ;
int num_words, result ;
Game g ;`
// seed the rand() function first
srand ( time(NULL));
// check to see if command line argument was given, if so use it as the filename for the words
if ( argc > 1 ){
strcpy ( wordfile, argv[1] ) ;
} else {
strcpy ( wordfile, "wordlist.txt" ) ;
}
// now read word file
num_words = read_words ( wordfile, wordlist ) ;
if ( num_words == 0 ){
printf ( "No words were read from file, exiting\n") ;
exit ( -1 ) ;
}
printf ( "Read %d words from file\n", num_words ) ;
do {
setup_game ( &g, wordlist, num_words ) ;
result = play_game ( &g ) ;
printf ( "Would you like to play again (y/n)? " ) ;
scanf ( " %c", &option ) ;
fgets ( temp, 256, stdin ) ; // read the rest of the line to get rid of it from stdin
} while ( option == 'y' || option == 'Y' ) ;
return 0 ;
}
int draw_man ( int misses ){
char *platform[]={
" ===\n",
" |\n"
" |\n"
" |\n"
" ===\n",
" =====|\n"
" |\n"
" |\n"
" |\n"
" ===\n",
" |=====|\n"
" |\n"
" |\n"
" |\n"
" ===\n",
" |=====|\n"
" O |\n"
" |\n"
" |\n"
" ===\n",
" |=====|\n"
" O |\n"
" | |\n"
" |\n"
" ===\n",
" |=====|\n"
" O |\n"
" |- |\n"
" |\n"
" ===\n",
" |=====|\n"
" O |\n"
" -|- |\n"
" |\n"
" ===\n",
" |=====|\n"
" O |\n"
" -|- |\n"
" | |\n"
" ===\n",
" |=====|\n"
" O |\n"
" -|- |\n"
" // |\n"
" ===\n"
};
switch(misses)
{
case 0:
printf("\n\n%s\n", platform[0]);
break;
case 1:
printf("\n\n%s\n", platform[1]);
break;
case 2:
printf("\n\n%s\n", platform[2]);
break;
case 3:
printf("\n\n%s\n", platform[3]);
break;
case 4:
printf("\n\n%s\n", platform[4]);
break;
case 5:
printf("\n\n%s\n", platform[5]);
break;
case 6:
printf("\n\n%s\n", platform[6]);
break;
case 7:
printf("\n\n%s\n", platform[7]);
break;
case 8:
printf("\n\n%s\n", platform[8]);
break;
case 9:
printf("\n\n%s\n", platform[9]);
break;
}
}
int display_guesses( unsigned char guesses[] ){
for(int j=0;j<26;j++){
int i = j + 'A';
char c = (char)i;
if(guesses[j] == 1)
//printf("%d ", j);
printf("%c ", c);
}
printf("\n");
}
char read_guess ( unsigned char guesses[] ){
char c;
while(1){
printf("Enter a valid character: ");
scanf("\n%c",&c);
// printf("hi");
if(c>='a'&& c<='z'&& guesses[(int)(c-'a')] ==0)
return (char)(c-'a') +'A' ;
if(c>='A'&& c<='Z'&& guesses[(int)(c-'A')] ==0)
return c;
printf("\n Invalid Character!!\n");
}
}
// WEEK 2 FUNCTIONS
// add_guess()
// Adds the given guess to the guesses array, making the relevant entry 1. For exmpale, if guess is 'a' or 'A',
// element 0 of the guesses array is set to 1. If 'z' or 'Z' is input, then element 25 is set to 1. Your function
// should check that the character is a valid letter and return -1 if it is not.
// Returns 0 if successful, or -1 if an invalid character is entered.
int add_guess ( char guess, unsigned char guesses[] ){
if ((guess >= 'a' && guess <= 'z') || (guess >= 'A' && guess <= 'Z')) {
if (guess >= 'a' && guess <= 'z') {
guesses[guess - 'a'] = 1;
} else {
guesses[guess - 'A'] = 1;
}
return 0;
}
return -1;
}
// check_guess()
// Checks if the given character 'guess' is contained within the string 'word'.
// Returns 1 if it is, 0 otherwise
int check_guess ( char word[], char guess ){
int length = strlen(word);
int i;
for (i = 0; i < length; ++i) {
if (guess == word[i])
return 1;
}
return 0;
}
// hidden_word()
// Creates the word that is displayed to the user, with all the correctly guessed letters
// shown, and the rest displayed as underscores. Any non-letters (punctuation, etc) are displayed.
// The function takes two strings as inputs. word[] is the word that the player is trying to guess,
// and display_word[] is the output string to be displayed to the player. The guesses array is a binary
// array of size 26 indicating whether each letter (a-z) has been guessed yet or not.
// Returns 0 if successful, -1 otherwise.
int hidden_word ( char display_word[], char word[], unsigned char guesses[26] ){
int l = strlen(display_word);
if (l != strlen(word))
return -1;
//printf("%d", l);
int i;
for (i = 0; i < l; ++i) {
char c1 = (display_word[i] >= 'A' && display_word[i] <= 'Z') ? display_word[i] + 32 : display_word[i];
char c2 = (word[i] >= 'A' && word[i] <= 'Z') ? word[i] + 32 : word[i];
if (c1 == '_') {
if (guesses[c2 - 97] == 1)
return -1;
else {
guesses[c2 - 97] = 1;
}
}
else if ((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) {
if (c2 != c1)
return -1;
}
}
return 0;
}`