Hangman game program in C

The Hangman game is a traditional word guessing game where one player thinks of a word, and the other player tries to guess it by suggesting letters. A portion of a stick figure resembling a hanging man is drawn for each wrong guess. The object of the game is for the player to accurately guess the word before the stick figure has been drawn entirely.

The elements and ideas included in the given Hangman game program in C are broken down as follows:

  1. Word Selection: A variety of predetermined terms are used by the program. With the help of the time() and rand() functions, one of these words is chosen at random from the list. The word that was chosen is saved as selectedWord.
  2. Word Representation: A guessedWord array is initialized by the program with underscores. This array is the same size as the chosen word. The underscores in guessedword are changed with the actual letters as the player correctly guesses the letters.
  3. Guessing Loop: A loop is used to play the game. The player continues to guess letters until they correctly guess the entire word or make too many wrong guesses.
  4. Input: Using the scanf() function, the player enters their guessed letter. The computer program also determines if the letter has already been correctly predicted once.
  5. Verifying Guess: The chosenWord is compared to the guessed letter. The relevant locations in the guessedWord array are updated if the guessed letter appears in the word. The player's incorrect guess count is increased if the guessed letter is missing.
  6. Display: The program shows the number of wrong guesses, the number of letters that have already been correctly predicted, and the current status of the guessed word (guessedWord).
  7. End Messages: Depending on whether the player succeeds or fails, the program will either display a message congratulating them on their success or one that will say the word they were correct in.
  8. Data storage: The program uses arrays (guessedWord and guessedLetters) to keep track of the letters that have been correctly identified and the current guessword. Integer variables are sometimes used to record inaccurate predictions.

A C program for the Hangman game uses several important programming ideas, including:

  1. Words, guessed letters, and the state of the predicted word is stored in arrays and strings.
  2. It contains word selection through randomization.
  3. It uses the loops to manage the game's flow.
  4. Using conditional statements, game logic will be used to create judgments.
  5. It utilizes the user input with tools like the scanf() function.
  6. The use of string manipulation updates the state of the guessed word.
  7. It uses basic output to inform players of game status and messages.

Example:

Here is a simple Hangman game implementation in the C programming language:

Output:

Welcome to Hangman!
________
Incorrect Guesses: 0/6
Enter a letter: e
Correct guess!
_______e
Incorrect Guesses: 0/6
Enter a letter: r
Incorrect guess!
_______e
Incorrect Guesses: 1/6
Enter a letter: x
Incorrect guess!
_______e
Incorrect Guesses: 2/6
Enter a letter: a
Correct guess!
_a___a_e
Incorrect Guesses: 2/6
Enter a letter: s
Incorrect guess!
_a___a_e
Incorrect Guesses: 3/6
Enter a letter: g
Correct guess!
_a_g_age
Incorrect Guesses: 3/6
Enter a letter: n
Correct guess!
_ang_age
Incorrect Guesses: 3/6
Enter a letter: l
Correct guess!
lang_age
Incorrect Guesses: 3/6
Enter a letter: u
Correct guess!
Congratulations! You've guessed the word: language

Explanation:

  1. (#include directives) Header files:
    • #include <stdio.h>: This header file is used for input and output functions, including printf() and scanf().
    • #include <string.h>: This header file is used for string manipulation operations like strlen and strcmp.
    • #include <stdlib.h>: This header file is used to provide dynamic memory allocation and conversion functions.
    • #include <time.h>: This header file contains functions for working with time to seed the random number generator.
  2. Constants and Arrays:
    • #define MAX_TRIES 6: It define MAX_TRIES. It is the amount of incorrect predictions that can be made is set at six.
    • #define WORDS_COUNT 5: It is the amount of words that can be guessed as specified in option 5.
    • const char *words[WORDS_COUNT]: It is a list of pointers to constant strings with the words that can be deduced.
  3. Words are chosen at random:
    • srand(time(NULL));: The random number generator is seeded with the time using the statement srand(time(NULL));.
    • int randomIndex = rand() % WORDS_COUNT;: If you want to choose a word from the words array, use the random index created by int randomIndex = rand()% WORDS_COUNT;.
    • Const char *selectedWord = words[randomIndex];: It selects a word from the array and assigns it.
    • int wordLength = strlen(selectedWord);: The length of the selected word is determined by the formula int wordLength = strlen(selectedWord).
  4. Initializing the Guessed Word:
    • char guessedWord[wordLength + 1];: It is used to declare an array to hold the guessed word with blanks.
    • memset(guessedWord, '_', wordLength);: Fills the array with underscores to represent the unidentified characters.
    • guessedWord[wordLength] = '0';: Ends the array with a null character.
  5. Loop of Guessing:
    • int incorrectGuesses = 0;: It mistaken intTracks the quantity of erroneous guesses with incorrectGuesses = 0;.
    • char guessedLetters[26];: An array to hold the letters that have been correctly predicted.
    • int guessedLettersCount = 0;: The number of letters that have been correctly predicted is indicated by the counter int guessedLettersCount = 0.
    • As long as there haven't been more wrong guesses than permitted and the word that was guessed hasn't been chosen, the loop starts and goes on.
  6. Information Display and Input:
    • The player is prompted to submit a letter and the current state of the word they have guessed, the number of wrong guesses, and the word itself.
    • char guess; scanf("%c", &guess);: This command reads a character from the player's input while leaving a blank space before %c to absorb any newline characters.
  7. Checking Prior Guesses:
    • The program iterates through the guessedLetters array to see if the guessed letter has already been predicted.
    • In which case the player is informed, the loop continues if the letter has already been predicted.
  8. Updating Guess Records:
    • When a new letter is correctly predicted, it is added to the guessedLetters array, increasing its count.
    • After that, the software verifies that the guessed letter exists in the chosen word. If it is, guessedWord updates the corresponding locations.
  9. Feedback display:
    The program will provide the player feedback depending on whether the guessed letter appears in the chosen word. It displays "Correct guess!" or "Incorrect guess!" depending on the outcome.
  10. Final Score:
    • The cycle repeats until the player correctly guesses the complete word or until they have made all of their permitted wrong guesses.
    • After that, the program determines if the player correctly predicted the word when the loop stops and displays the relevant message.
  11. End Messages:
    • A message of congratulations is shown along with the correct word if the player correctly predicted the word.
    • A message indicating the correct word is presented if the player has exhausted their guesses.
  12. Return Statement:
    It returns 0 to show that the main function has been successfully executed.

By inputting letters, the player of this simple code's Hangman game attempts to guess a word that has been chosen at random. The user can see the user's game progress and the number of inaccurate predictions that have been made by the program. Until the participant correctly guesses the word or has made all of their wrong guesses, the game is still going on.

The Hangman game program demonstrates fundamental ideas, including input/output, arrays, loops, conditionals, text manipulation, and randomization. It can build on this framework to produce a game with more features and interactive features.






Latest Courses