I am trying to code a hangman game in c (one of my first c programs) and I have a problem where the main function does not output anything when I call my runGame() function. The terminal doesnt terminate either so I thought it couldve been an infite loop in runGame(), but it doesnt even output the first printf line from runGame(). I am currently generating a random word from a text file through the randomWord() function and when I call this in the main function and not runGame(), it seems to work perfectly fine.
Does anyone know what may have caused the runGame() function to make the program act weirdly?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define maxLen 100
char *randomWord() { //generates a random word from txt file
FILE *word_list;
size_t line_no = 0;
size_t select_len;
//double drand48();
char chosen_word[9895]; //size represents total number of lines
char current_word[9895];
chosen_word[0] = '\0'; //if no lines then prevents crashing
int max = 1.0;
//reading in file with words to choose from
word_list = fopen("10000-most-common-eng-words.txt", "r");
if (word_list == NULL) {
printf("file not found\n");
exit(EXIT_FAILURE);
}
while(fgets(current_word, sizeof(current_word), word_list)) { //while there are still lines to read
if( (double)rand()/ ((RAND_MAX) / max) < 1.0 / ++line_no) { // odds of word being replaced given a random double
strcpy(chosen_word, current_word); //replace with new word
}
}
fclose(word_list); //close file
select_len = strlen(chosen_word); //get word length of random chosen word
if(select_len > 0 && chosen_word[select_len - 1] == '\n') { //if chosen word if a new line
chosen_word[select_len - 1] = '\0'; //then replace with null value
}
printf(chosen_word);
printf("\n");
return strdup(chosen_word);
}
int runGame() {
printf("hey"); //test
int game_over = 0;
int man_hung = 0;
int rand_word_len;
//char player_guess[1];
char guesses_made[10];
char player_guess;
char rand_word[maxLen];
printf("running game...\n");
while(!game_over) {
strcpy(rand_word, randomWord());
//rand_word = randomWord(); //get random word
rand_word_len = strlen(randomWord());
printf("Enter your guess (single letters only): \n");
player_guess = getchar(); //get length of random word
while(man_hung != 10) {
for(int a = 0; a < sizeof(guesses_made) / sizeof(guesses_made[0]); a++) { //for every value in array
if(guesses_made[a] == player_guess) { //see if guess has already been made
printf("You've already made that guess. Try something different...\n");
continue; //go back to second while loop (man_hung)
} else { //else append guess to array
strcat(guesses_made, &player_guess);
}
}
//iterate through random word array to see if letter exists in word
for(int b = 0; b < sizeof(rand_word) / sizeof(rand_word[0]); b++) {
if(rand_word[b] != player_guess) {
man_hung += 1;
printf("Sorry wrong guess, try again\n");
continue;
}
else { // delete letter from random word array
for(int c = b-1; c < rand_word_len; c++) {
rand_word[c] = rand_word[c+1];
}
}
} //end of for
printf("Correct guess!");
for(size_t i = 0; i < rand_word_len; ++i){
if(rand_word[i] != 0) { //if element is not 'empty'
continue; //ask for another input
}
}
printf("Well done,you won! The word was %s \n", rand_word);
game_over = 1;
break;
}//end of man hung while
}//end of game_over while
return 0;
}//end of run_game
int main() {
printf("uh"); //test
runGame();
printf("yes"); //test
srand(time(NULL));
return 0;
}