Simple Hangman game in C
Below is a simple implementation of the Hangman game in C. This code assumes a console-based interaction.
// Include stdio.h, stdlib.h, string.h and time.h #include char* getRandomWord(); void displayHangman(int tries); void displayGuessedWord(char* secretWord, char* guessedLetters); int isLetterAlreadyGuessed(char guess, char* guessedLetters); void updateGuessedLetters(char* secretWord, char* guessedLetters, char guess); int isWordGuessed(char* secretWord); int main() { char* secretWord = getRandomWord(); char guessedLetters[20] = { '\0' }; int tries = 0; while (1) { displayHangman(tries); displayGuessedWord(secretWord, guessedLetters); printf("Enter a letter: "); char guess; scanf(" %c", &guess); if (isLetterAlreadyGuessed(guess, guessedLetters)) { printf("You've already guessed that letter. Try again.\n"); continue; } if (strchr(secretWord, guess) != NULL) { updateGuessedLetters(secretWord, guessedLetters, guess); } else { tries++; } if (isWordGuessed(secretWord)) { printf("Congratulations! You guessed the word: %s\n", secretWord); break; } if (tries == 6) { displayHangman(6); printf("Sorry, you ran out of tries. The correct word was: %s\n", secretWord); break; } } free(secretWord); return 0; } char* getRandomWord() { char* words[] = { "hangman", "computer", "programming", "language", "science" }; int numWords = sizeof(words) / sizeof(words[0]); srand(time(NULL)); int randomIndex = rand() % numWords; return strdup(words[randomIndex]); } void displayHangman(int tries) { switch (tries) { case 0: printf(" _______\n"); printf(" | |\n"); printf(" |\n"); printf(" |\n"); printf(" |\n"); printf("__|_________\n"); break; case 1: printf(" _______\n"); printf(" | |\n"); printf(" | O\n"); printf(" |\n"); printf(" |\n"); printf("__|_________\n"); break; case 2: printf(" _______\n"); printf(" | |\n"); printf(" | O\n"); printf(" | |\n"); printf(" |\n"); printf("__|_________\n"); break; case 3: printf(" _______\n"); printf(" | |\n"); printf(" | O\n"); printf(" | /|\n"); printf(" |\n"); printf("__|_________\n"); break; case 4: printf(" _______\n"); printf(" | |\n"); printf(" | O\n"); printf(" | /|\\\n"); printf(" |\n"); printf("__|_________\n"); break; case 5: printf(" _______\n"); printf(" | |\n"); printf(" | O\n"); printf(" | /|\\\n"); printf(" | /\n"); printf("__|_________\n"); break; case 6: printf(" _______\n"); printf(" | |\n"); printf(" | O\n"); printf(" | /|\\\n"); printf(" | / \\\n"); printf("__|_________\n"); break; default: break; } } void displayGuessedWord(char* secretWord, char* guessedLetters) { printf("Current word: "); for (int i = 0; i < strlen(secretWord); i++) { if (strchr(guessedLetters, secretWord[i]) != NULL) { printf("%c ", secretWord[i]); } else { printf("_ "); } } printf("\n"); } int isLetterAlreadyGuessed(char guess, char* guessedLetters) { return strchr(guessedLetters, guess) != NULL; } void updateGuessedLetters(char* secretWord, char* guessedLetters, char guess) { strncat(guessedLetters, &guess, 1); } int isWordGuessed(char* secretWord) { return strcspn(secretWord, "_") == strlen(secretWord); }