Cover Image for Hangman Game in C
110 views

Hangman Game in C

Creating a simple Hangman game in C involves generating a random word for the player to guess and providing a mechanism for input and feedback. Here’s a basic implementation of a Hangman game in C:

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_TRIES 6

// Array of words for the game
const char *wordBank[] = {"programming", "hangman", "computer", "keyboard", "algorithm"};

// Function to choose a random word from the word bank
const char *chooseRandomWord() {
    srand(time(NULL));
    int randomIndex = rand() % (sizeof(wordBank) / sizeof(wordBank[0]));
    return wordBank[randomIndex];
}

// Function to initialize the guessed word with underscores
void initializeGuessedWord(char *guessedWord, const char *word) {
    int i;
    for (i = 0; word[i] != '\0'; i++) {
        if (word[i] == ' ')
            guessedWord[i] = ' ';
        else
            guessedWord[i] = '_';
    }
    guessedWord[i] = '\0';
}

// Function to display the hangman
void displayHangman(int tries) {
    printf("\n");
    if (tries >= 1)
        printf("  ______\n");
    else
        printf("        \n");

    if (tries >= 2)
        printf("  |    |\n");
    else
        printf("        \n");

    if (tries >= 3)
        printf("  |    O\n");
    else
        printf("        \n");

    if (tries == 4)
        printf("  |   /|\\\n");
    else if (tries == 5)
        printf("  |   /| \n");
    else if (tries >= 6)
        printf("  |   /|\\\n");
    else
        printf("        \n");

    printf("  |\n");
    printf("  |\n");
    printf("___\n");
}

// Function to check if the guessed word is complete
int isWordGuessed(const char *guessedWord) {
    for (int i = 0; guessedWord[i] != '\0'; i++) {
        if (guessedWord[i] == '_') {
            return 0;
        }
    }
    return 1;
}

int main() {
    const char *selectedWord = chooseRandomWord();
    char guessedWord[100];
    int tries = 0;
    char guessedLetters[26] = {0}; // Array to keep track of guessed letters

    initializeGuessedWord(guessedWord, selectedWord);

    printf("Welcome to Hangman!\n");
    printf("Guess the word: %s\n", guessedWord);

    while (tries < MAX_TRIES) {
        char guess;
        printf("Enter a letter: ");
        scanf(" %c", &guess);

        if (strchr(guessedLetters, guess) != NULL) {
            printf("You already guessed that letter.\n");
            continue;
        }

        guessedLetters[tries] = guess;

        int found = 0;
        for (int i = 0; selectedWord[i] != '\0'; i++) {
            if (selectedWord[i] == guess) {
                guessedWord[i] = guess;
                found = 1;
            }
        }

        if (!found) {
            tries++;
            printf("Incorrect guess. Tries left: %d\n", MAX_TRIES - tries);
        } else {
            printf("Good guess: %s\n", guessedWord);
        }

        displayHangman(tries);

        if (isWordGuessed(guessedWord)) {
            printf("Congratulations! You guessed the word: %s\n", selectedWord);
            break;
        }
    }

    if (tries == MAX_TRIES) {
        printf("Sorry, you're out of tries. The word was: %s\n", selectedWord);
    }

    return 0;
}

This code sets up a basic Hangman game where the player tries to guess a randomly selected word from a word bank. The player is allowed a maximum of six incorrect guesses before losing the game. The game keeps track of guessed letters and displays the hangman as the player makes incorrect guesses.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS