write a hangman program in c language that guess random words with letter
Assistant
User
add hangman when the guess is wrong to the above program
Assistant
Sure! Here's an updated version of the hangman game program in C that includes a hangman graphic that gets built up as the player makes incorrect guesses:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure, here's a basic implementation of a hangman game in C that randomly chooses a word and asks the player to guess letters until the word is complete or the player runs out of attempts:
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_ATTEMPTS 6
#define MAX_WORD_LENGTH 20
int main() {
char word[MAX_WORD_LENGTH + 1];
char guessed_letters[MAX_WORD_LENGTH];
int num_guesses = 0;
int num_incorrect_guesses = 0;
int word_length;
int i;
// List of words to choose from
char *word_list[] = {
"apple",
"banana",
"cherry",
"orange",
"pear",
"watermelon"
};
int num_words = sizeof(word_list) / sizeof(word_list[0]);
// Seed the random number generator with the current time
srand(time(NULL));
// Choose a random word from the list
int word_index = rand() % num_words;
strcpy(word, word_list[word_index]);
word_length = strlen(word);
// Initialize the guessed letters array to all underscores
for (i = 0; i < word_length; i++) {
guessed_letters[i] = '_';
}
guessed_letters[word_length] = '\0';
// Main game loop
while (num_incorrect_guesses < MAX_ATTEMPTS && strcmp(word, guessed_letters) != 0) {
printf("Word: %s\n", guessed_letters);
printf("Guess a letter: ");
char guess;
scanf(" %c", &guess);
// Check if the guess is in the word
int correct_guess = 0;
for (i = 0; i < word_length; i++) {
if (word[i] == guess) {
guessed_letters[i] = guess;
correct_guess = 1;
}
}
// Update the number of guesses and incorrect guesses
num_guesses++;
if (!correct_guess) {
num_incorrect_guesses++;
}
printf("\n");
}
// Print the result of the game
if (num_incorrect_guesses == MAX_ATTEMPTS) {
printf("You lose! The word was %s.\n", word);
} else {
printf("You win! The word was %s.\n", word);
}
return 0;
}
This code uses an array of words to choose from, but you could modify it to read a list of words from a file or database instead. The srand function is used to seed the random number generator with the current time, so that the choice of word is different each time the program is run. The guessed_letters array is initialized to all underscores, and then updated with each correct guess. The strcmp function is used to compare the word and guessed_letters strings to see if the game is over. If the player runs out of attempts (specified by the MAX_ATTEMPTS constant), the game is over and the player loses. Otherwise, if the player correctly guesses all the letters in the word, the game is over and the player wins.
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_ATTEMPTS 6
#define MAX_WORD_LENGTH 20
void draw_hangman(int num_incorrect_guesses) {
printf(" +----+\n");
printf(" | |\n");
printf(" | %c\n", num_incorrect_guesses >= 1 ? 'O' : ' ');
printf(" | %c%c%c\n", num_incorrect_guesses >= 3 ? '/' : ' ', num_incorrect_guesses >= 2 ? '|' : ' ', num_incorrect_guesses >= 4 ? '\\' : ' ');
printf(" | %c %c\n", num_incorrect_guesses >= 5 ? '/' : ' ', num_incorrect_guesses >= 6 ? '\\' : ' ');
printf("=======\n");
}
int main() {
char word[MAX_WORD_LENGTH + 1];
char guessed_letters[MAX_WORD_LENGTH];
int num_guesses = 0;
int num_incorrect_guesses = 0;
int word_length;
int i;
// List of words to choose from
char *word_list[] = {
"apple",
"banana",
"cherry",
"orange",
"pear",
"watermelon"
};
int num_words = sizeof(word_list) / sizeof(word_list[0]);
// Seed the random number generator with the current time
srand(time(NULL));
// Choose a random word from the list
int word_index = rand() % num_words;
strcpy(word, word_list[word_index]);
word_length = strlen(word);
// Initialize the guessed letters array to all underscores
for (i = 0; i < word_length; i++) {
guessed_letters[i] = '_';
}
guessed_letters[word_length] = '\0';
// Main game loop
while (num_incorrect_guesses < MAX_ATTEMPTS && strcmp(word, guessed_letters) != 0) {
// Draw the hangman
draw_hangman(num_incorrect_guesses);
// Print the word with underscores for unguessed letters
printf("Word: %s\n", guessed_letters);
// Get a guess from the user
printf("Guess a letter: ");
char guess;
scanf(" %c", &guess);
// Check if the guess is in the word
int correct_guess = 0;
for (i = 0; i < word_length; i++) {
if (word[i] == guess) {
guessed_letters[i] = guess;
correct_guess = 1;
}
}
// Update the number of guesses and incorrect guesses
num_guesses++;
if (!correct_guess) {
num_incorrect_guesses++;
}
printf("\n");
}
// Draw the final hangman
draw_hangman(num_incorrect_guesses);
// Print the result of the game
if (num_incorrect_guesses == MAX_ATTEMPTS) {
printf("You lose! The word was %s.\n", word);
} else {
printf("You win! The word was %s.\n", word);
}
return 0;
}
This code includes a new function called draw_hangman that takes the number of incorrect guesses as a parameter and prints a graphic of a hangman with the appropriate body parts