Untitled

 avatar
unknown
plain_text
a year ago
7.0 kB
15
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<stdbool.h>
#include <ctype.h>
#include <time.h>
#include <windows.h>
#define maximum_try 10 // a user can maximum try 10th time


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);                                                         //is a function used to suspend the execution of a program for a specified number of seconds.
        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 10 mistakes are allowed.               | \n");
    printf("            | All alphabet are in lower case.                |\n");
    printf("            | All words are very popular.                    | \n");
    printf("            | If you enjoy continue, otherwise close it.     |\n");
    printf("            ..................................................\n");
    printf("\n\n");
    return 1;
}


int func_checking(const char *selectword, char guess, char *g_w)
{
    bool flag=false;                                 // bool function can hold two possible values: true or false.
    for (int i = 0;selectword[i] != '\0';i++)
    {
        if(selectword[i] == guess)
        { g_w[i] = selectword[i];  flag = true; }
    }
    return flag;
}


int main()
{
    design_part(1);                                                   // make a function that show the HANGMAN
    
    //define string array 
    const char *Sports[] = {"golf","boxing","hockey","snake","xare","rugby","wrestling"}; 
    const char *Colors[] = {"red","blue","orange","black","white","azure","cyan"};
    const char *Food[]   = {"quiche","burger","spagheti","yogurt","mango","Fig"," Grape"};

    printf("                    Click ENTER to start the game : _");
    getchar();                                                       // take any kind of character without distortion
    system("cls");                                                  // clear the previous window

    printf("\n\n                ----------------------------------\n");
    printf("                |    Please! Choose any option  |\n");
    printf("                ----------------------------------\n");
    printf("                |           1.Sport              |\n");
    printf("                |           2.Color              |\n");
    printf("                |           3.Food               |\n");
    printf("                ---------------------------------\n");

    int x;
    for (;1;)
    {
        printf("Choice your option -> ");
        scanf("%d", &x);
        if (x >= 1 && x <= 4)
        {
            system("cls");                                              //for clearing the screen
            for (int i = 1; i <= 25; i++)
            {
                Sleep(50);printf("\r");                                 // sleep() is a function used to suspend the execution of a program for a specified number of seconds
                for (int j = 1; j <= i; j++)
                {printf(".");}                                          // create loading types style -> .....
            }
            system("cls");break;
        }
        else {printf("Invalid!!! Please type between 1 to 4\n");}
    }

    const char *g_word;
    switch (x)                                                        // check the user choice option, either it valid or not
    {
    case 1:
        srand(time(NULL));                                           // srand() and time(NULL) are used together to initialize the random number generator with a seed based on the current tim 
        g_word = Sports[rand() % 7];
        break;

    case 2:
        srand(time(NULL));
        g_word = Colors[rand() % 7];
        break;

    case 3:
        srand(time(NULL));
        g_word = Food[rand() % 7];
        break;

    default:
        printf("Default case is Matched.");
        break;
    }

    int s_word = strlen(g_word);                                  //strlen() is a function, used to find the size of a word
    printf("SIZE OF WORD -> %d\n\n", s_word);

    char g_w[20]={0},g_char[20]={0};
    g_w[s_word] = '\0'; 
    int w = 0,r = 0;
    
    do{
        printf("Enter a letter: ");
        char guess;
        scanf(" %c", &guess);
        if (!strchr(g_char,tolower(guess)))                      // strchr() is a function in C is used to find the first occurrence of a specified character in a string.
        {                                                  
            g_char[strlen(g_char)] = tolower(guess);             // tolower() function is used to convert a given character to its lowercase equivalent
            if (func_checking(g_word,tolower(guess),g_w))        // func_checking function check the character that is given by a user
            {
                r++;
                printf("Correct guess!\n");
            }
            else
            {
                w++;
                printf("Wrong guess! You have %d guesses left.\n", maximum_try - w); //in case of wrong guess
            }
            printf("Guessed letters: %s\n", g_w);
        }
        else
        {
            printf("You already guessed that letter. Try again.\n");  //If the position already occupied by same alphabet then no need to fill again EASY!! and continue 
        }
    }
    while (r<s_word && w<10);                           // loop for exiting the program when no try left

    if (r == s_word)                                    // if the player guessed the whole word right then he/she is the WINNER
    {
        printf("\nYAHOO!!!!! You are the WINNER\n");
        printf("\n");
        printf("\n");
        printf("      -------+\n");
        printf("      |      | \n ");
        printf("     |     \\o/ \n");
        printf("      |      |  \n");
        printf("      |     / \\ \n\n\n");
        printf("The word is - > %s\n", g_word);
    }
    else
    {
        printf("\n The Word was -> %s ",g_word);
        printf("\n The man is dead you IDIOT!!");
	    printf("\n Better luck next!!");

    }
    return 0;
}

Editor is loading...
Leave a Comment