Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-11-20· OpenAIHugging Face LMArena Human Preference 140K

how to write basic hangman game in C?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    how to write basic hangman game in C?

  2. Assistant

    Creating a basic Hangman game in C involves using fundamental programming concepts such as arrays, loops, conditionals, and functions. Below is an example of a simple Hangman game:

    Code Example: Basic Hangman Game in C

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define MAX_TRIES 6
    
    void displayWord(char word[], int guessed[]) {
        for (int i = 0; i < strlen(word); i++) {
            if (guessed[i]) {
                printf("%c ", word[i]);
            } else {
                printf("_ ");
            }
        }
        printf("\n");
    }
    
    int isWordGuessed(int guessed[], int length) {
        for (int i = 0; i < length; i++) {
            if (!guessed[i]) {
                return 0; // Word is not fully guessed
            }
        }
        return 1; // Word is fully guessed
    }
    
    int main() {
        char word[] = "programming"; // The word to guess
        int wordLength = strlen(word);
        int guessed[wordLength]; // Array to track guessed letters
        memset(guessed, 0, sizeof(guessed)); // Initialize guessed array to 0
    
        int tries = 0;
        char guess;
    
        printf("Welcome to Hangman!\n");
        printf("You have %d tries to guess the word.\n\n", MAX_TRIES);
    
        while (tries < MAX_TRIES) {
            printf("Word: ");
            displayWord(word, guessed);
    
            printf("Enter a letter: ");
            scanf(" %c", &guess);
            guess = tolower(guess); // Convert to lowercase for consistency
    
            int correctGuess = 0;
            for (int i = 0; i < wordLength; i++) {
                if (word[i] == guess && !guessed[i]) {
                    guessed[i] = 1; // Mark the letter as guessed
                    correctGuess = 1;
                }
            }
    
            if (!correctGuess) {
                tries++;
                printf("Wrong guess! You have %d tries left.\n", MAX_TRIES - tries);
            } else {
                printf("Good guess!\n");
            }
    
            if (isWordGuessed(guessed, wordLength)) {
                printf("\nCongratulations! You guessed the word: %s\n", word);
                return 0;
            }
        }
    
        printf("\nGame over! You've run out of tries. The word was: %s\n", word);
        return 0;
    }
    

    Explanation of the Code:

    1. Word to Guess: The word to guess is stored in the word array. You can change this to any word you like.
    2. Guessed Array: An integer array guessed is used to track which letters of the word have been guessed correctly.
    3. Display Function: The displayWord function shows the current state of the word, with underscores for unguessed letters.
    4. Guess Input: The user inputs a letter, which is checked against the word.
    5. Correct/Wrong Guess: If the guess is correct, the corresponding positions in the guessed array are updated. If the guess is wrong, the number of tries is incremented.
    6. Win/Loss Conditions: The game ends when the user either guesses the word or runs out of tries.

    How to Run:

    1. Copy the code into a file, e.g., hangman.c.
    2. Compile the code using a C compiler, e.g., gcc hangman.c -o hangman.
    3. Run the program: ./hangman.

    Example Gameplay:

    Welcome to Hangman!
    You have 6 tries to guess the word.
    
    Word: _ _ _ _ _ _ _ _ _ _ _ 
    Enter a letter: p
    Good guess!
    
    Word: p _ _ _ _ _ _ _ _ _ _ 
    Enter a letter: z
    Wrong guess! You have 5 tries left.
    
    Word: p _ _ _ _ _ _ _ _ _ _ 
    Enter a letter: r
    Good guess!
    
    ...
    

    This is a simple implementation. You can enhance it by adding features like:

    • Randomly selecting a word from a list.
    • Validating user input (e.g., ensuring only letters are entered).
    • Displaying a visual representation of the hangman.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.