Untitled
unknown
plain_text
2 years ago
6.9 kB
9
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <windows.h>
#define MAX_WRONG_GUESSES 10 // Maximum number of incorrect guesses allowed
#define MAX_WORD_LENGTH 20 // Maximum length of the word to be guessed
// Function to select a random word from a list
const char *selectRandomWord(const char *words[])
{
srand(time(NULL)); // Seed the random number generator with the current time
return words[rand() % 10]; // Return a random word from the list
}
// Function to check if the guessed letter is in the word
int checkGuess(const char *word, char guess, char *guessedWord)
{
int found = 0;
for (int i = 0; word[i] != '\0'; i++)
{
if (tolower(word[i]) == guess)
{ // Convert to lowercase for case-insensitive comparison
guessedWord[i] = word[i]; // Update the guessed word with the correct letter
found = 1;
}
}
return found;
}
int design_part(int x)
{
printf("\n");
printf("\n");
printf("\n");
printf(" -------+\n");
printf(" | | \n ");
printf("| O \n ");
printf("| / \\ \n");
printf(" | | \n");
printf(" | / \\ \n");
printf(" |\n\n");
for (int i = 1; i <= 25; i++)
{
Sleep(50); printf("\r");
for (int j = 1; j <= i; j++) { printf("."); }
}
printf("\n");
printf(" | H A N G M A N | \n");
printf("\n\n");
Sleep(500);
system("cls");
printf("..................................................\n");
printf("| Hangman Rules |\n");
printf("..................................................\n");
printf("| Maximum 6 mistakes are allowed. | ");
printf("\n| All alphabet are in lower case. |");
printf("\n| All words are name of very popular Websites | ");
printf("\n| If you enjoy continue, otherwise close it |\n");
printf("..................................................\n");
return 1;
}
int main()
{
const char *animals[] = {"cow", "dog", "tiger", "lion", "fox", "monkey", "cat", "horse", "camel", "buffalo"};
const char *countries[] = {"germany", "bangladesh", "india", "finland", "poland", "pakistan", "korea", "argentina", "nepal", "scotland"};
const char *flowers[] = {"rose", "tulip", "lily", "sunflower", "orchid", "daisy", "jasmine", "hydrangea", "peony", "daffodil"};
const char *people[] = {"akib", "aryan", "shaad", "supta", "hridoy", "masud", "jayed", "arman", "alvi", "shovan"};
design_part(1);
printf("Press ENTER to start\n");
getchar();
system("cls");
printf("\n\n ---------------------------------\n");
printf(" | Please! Choose any option |\n");
printf(" ---------------------------------\n");
printf(" | 1 Words |\n");
printf(" | 2 Country |\n");
printf(" | 3 People |\n");
printf(" | 4 Flower |\n");
printf(" ---------------------------------\n");
// Prompt the user to select a category
int x;
for(;1;)
{
printf("Enter your option:- ");
scanf("%d", &x);
if (x >= 1 && x <= 4)
{
system("cls"); // Clear the screen if a valid option is chosen
break;
}
else
{
printf("Invalid!!! Please type between 1 to 4\n");
}
}
const char *g_word;
if (x == 1)
{
g_word = selectRandomWord(animals);
}
else if (x == 2)
{
g_word = selectRandomWord(countries);
}
else if (x == 3)
{
g_word = selectRandomWord(flowers);
}
else
{
g_word = selectRandomWord(people);
}
int wordLength = strlen(g_word);
char guessedWord[MAX_WORD_LENGTH];
memset(guessedWord, '_', wordLength); // Initialize guessedWord with underscores
guessedWord[wordLength] = '\0'; // Null-terminate the guessed word
int wrongGuesses = 0;
int correctGuesses = 0;
char guessedLetters[MAX_WORD_LENGTH];
memset(guessedLetters, 0, MAX_WORD_LENGTH); // Initialize guessedLetters with zeros
printf("Word: %s\n", guessedWord);
// Main game loop
while (wrongGuesses < MAX_WRONG_GUESSES && correctGuesses < wordLength)
{
printf("Enter a letter: ");
char guess;
scanf(" %c", &guess);
guess = tolower(guess); // Convert the guessed letter to lowercase
if (isalpha(guess))
{
if (!strchr(guessedLetters, guess))
{ // Check if the letter was already guessed
guessedLetters[strlen(guessedLetters)] = guess; // Add the letter to guessed letters
if (checkGuess(g_word, guess, guessedWord))
{
correctGuesses++;
printf("Correct guess!\n");
}
else
{
wrongGuesses++;
printf("Wrong guess! You have %d guesses left.\n", MAX_WRONG_GUESSES - wrongGuesses);
}
printf("Guessed letters: %s\n", guessedLetters);
printf("Word: %s\n", guessedWord);
}
else
{
printf("You already guessed that letter. Try again.\n");
}
}
else
{
printf("Invalid input. Please enter a letter.\n");
}
}
// Check if the player has won or lost
if (correctGuesses == wordLength)
{
printf("Congratulations! You guessed the word: %s\n", g_word);
}
else
{
// Display the hangman art if the player loses
printf(" ___\n");
printf(" |/ |\n");
printf(" | |\n");
printf(" | O\n");
printf(" | /|\\\n");
printf(" | / \\\n");
printf(" |\n");
printf(" /|\\_\n");
printf(" | \\_\n");
printf("Sorry, you're out of guesses. The word was: %s\n", g_word);
}
}Editor is loading...
Leave a Comment