#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>

#define WORDS 10
#define WORDLEN 40
#define CHANCE 6

bool srand_called = false;


int xrand(int x){
    if (!srand_called) {
        srand(time(NULL) << 10);
        srand_called = true;
    }
    return rand() % x;
}

char* decode(char* code){
int x = ((strlen(code) - 3) / 3) + 2;
char* decode = malloc(x);
char* free = decode;
char* letter = code;
for(int i = *code; i!= '\0'; i = *(++code)){
if((code - letter + 2) % 3 == 1){
    *(decode++) = i - (letter - code + 1) - x;
    }
}
*decode = '\0';
return free;
}

void print(int wrong, char* body){
printf("\tOOPS!! You missed wrong word: %d\n", wrong);
switch(wrong){
case 6: body[6] = '\\'; break;
case 5: body[5] = '/'; break;
case 4: body[4] = '\\'; break;
case 3: body[3] = '|'; break;
case 2: body[2] = '/'; break;
case 1: body[1] = ')', body[0] = '('; break;
default: break;
}

printf("\t _________\n"
           "\t|         |\n"
           "\t|        %c %c\n"
           "\t|        %c%c%c\n"
           "\t|        %c %c\n"
           "\t|             \n"
           "\t|             ", body[0], body[1], body[2],
           body[3], body[4], body[5], body[6]);

}

void printletters(char* guess, int length){
printf("\t");
for(int i = 0; i < length; i++){
printf("%c ", guess[i]);
}
printf("\n\n");
}


int main(void){

printf("\n\t Hellooo, Welcome to my hangman!!!");
printf("\n\n\tRules: ");

printf("\n\n\t Rules : ");
    printf("\n\t -You can make max of 6 mistakes after which you will loose the game");
    printf("\n\t - Lower case letters only");

char val[WORDS][WORDLEN] = {"N~mqOlJ^tZletXodeYgs","gCnDIfFQe^CdP^^B{hZpeLA^hv","7urtrtwQv{dt`>^}FaR]i]XUug^GI",
                                    "aSwfXsxOsWAlXScVQmjAWJG","cruD=idduvUdr=gmcauCmg]","BQt`zncypFVjvIaTl]u=_?Aa}F",
                                    "iLvkKdT`yu~mWj[^gcO|","jSiLyzJ=vPmnv^`N]^>ViAC^z_","xo|RqqhO|nNstjmzfiuoiFfhwtdh~",
                                    "OHkttvxdp|[nnW]Drgaomdq"};
char *body = malloc(CHANCE +1);

int x = xrand(WORDS);
char *letter = decode(val[x]);
int length = strlen(letter);
char *guess = malloc(length);
char wrongword[CHANCE];

memset(body, ' ', CHANCE+1);
memset(guess, '_', length);

char g;
bool find;
char *win;

int wrong = 0;
setvbuf(stdin, NULL, _IONBF, 0);

while(wrong < CHANCE && win != NULL){
find = false;
printf("\n\n");
print(wrong, body);
printf("\n");
printf("\tWrong letters: ");
if(wrong == 0){
printf("None \n");
}
for(int i = 0; i < wrong; i++){
printf("%c", wrongword[i]);
}
printf("\n");
printletters(guess, length);
printf("\tGive an alphabet in lower case : ");
do {
scanf("%c",&g);
} while ( getchar() != '\n' );
for(int i = 0; i < length; i++){

if(letter[i] == g) {
    find = true;
    guess[i] = g;
}
}
if(!find){
wrongword[wrong] = g;
wrong++;
}
win = strchr(guess, '_');
}

if(win = NULL){
printf("\n");
printletters(guess, length);
printf("\n Congrats YOU HAVE WONN YOU ARE THE SMARTEST OF ALLLLLL");
}

else {
printf("\n");
print(wrong, body);
printf("Im sorry BUT YOU LOOOSEEE BOOOOOO!!!!!!!!");
}

free(body);
free(letter);
return EXIT_SUCCESS;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.