gpt-4o-2024-11-20· OpenAI
LMArena Human Preference 140K
how to write basic hangman game in C?
- User
how to write basic hangman game in C?
- 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:
- Word to Guess: The word to guess is stored in the
wordarray. You can change this to any word you like. - Guessed Array: An integer array
guessedis used to track which letters of the word have been guessed correctly. - Display Function: The
displayWordfunction shows the current state of the word, with underscores for unguessed letters. - Guess Input: The user inputs a letter, which is checked against the word.
- Correct/Wrong Guess: If the guess is correct, the corresponding positions in the
guessedarray are updated. If the guess is wrong, the number of tries is incremented. - Win/Loss Conditions: The game ends when the user either guesses the word or runs out of tries.
How to Run:
- Copy the code into a file, e.g.,
hangman.c. - Compile the code using a C compiler, e.g.,
gcc hangman.c -o hangman. - 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.
- Word to Guess: The word to guess is stored in the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.