-
-
Save saroj22322/aa2f0849f33736395544c2d341ab3722 to your computer and use it in GitHub Desktop.
/** | |
* @autor Saroj Tripathi | |
* @name Hangman | |
* https://www.onlinegdb.com/online_c_compiler . Just copy this source code and test it on this online compiler. | |
*/ | |
#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 i_rnd(int i) { | |
if (!srand_called) { | |
srand(time(NULL) << 10); | |
srand_called = true; | |
} | |
return rand() % i; | |
} | |
char* decrypt(char* code) { | |
int hash = ((strlen(code) - 3) / 3) + 2; | |
char* decrypt = malloc(hash); | |
char* toFree = decrypt; | |
char* word = code; | |
for (int ch = *code; ch != '\0'; ch = *(++code)) | |
{ | |
if((code - word + 2) % 3 == 1){ | |
*(decrypt++) = ch - (word - code + 1) - hash; | |
} | |
} | |
*decrypt = '\0'; | |
return toFree; | |
} | |
void printBody(int mistakes, char* body) { | |
printf("\tMistakes :%d\n", mistakes); | |
switch(mistakes) { | |
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 printWord(char* guess, int len) { | |
printf("\t"); | |
for (int i = 0; i < len; ++i) | |
{ | |
printf("%c ", guess[i]); | |
} | |
printf("\n\n"); | |
} | |
int main() { | |
printf("\n\t Be aware you can be hanged!!."); | |
printf("\n\n\t Rules : "); | |
printf("\n\t - Maximum 6 mistakes are allowed."); | |
printf("\n\t - All alphabet are in lower case."); | |
printf("\n\t - All words are name of very popular Websites. eg. Google"); | |
printf("\n\t - If you enjoy continue, otherwise close it."); | |
printf("\n\t Syntax : Alphabet"); | |
printf("\n\t Example : a \n\n"); | |
char values[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 id = i_rnd(WORDS); | |
char *word = decrypt(values[id]); | |
int len = strlen(word); | |
char *guessed = malloc(len); | |
char falseWord[CHANCE]; | |
memset(body, ' ', CHANCE+1); | |
memset(guessed, '_', len); | |
char guess; | |
bool found; | |
char* win; | |
int mistakes = 0; | |
setvbuf(stdin, NULL, _IONBF, 0); | |
do { | |
found = false; | |
printf("\n\n"); | |
printBody(mistakes, body); | |
printf("\n\n"); | |
printf("\tFalse Letters : "); | |
if(mistakes == 0) printf("None\n"); | |
for (int i = 0; i < mistakes; ++i) | |
{ | |
printf("%c", falseWord[i]); | |
} | |
printf("\n\n"); | |
printWord(guessed, len); | |
printf("\tGive me a alphabet in lower case : "); | |
do {scanf("%c",&guess);} while ( getchar() != '\n' ); | |
for (int i = 0; i < len; ++i) | |
{ | |
if(word[i] == guess) { | |
found = true; | |
guessed[i] = guess; | |
} | |
} | |
if(!found) { | |
falseWord[mistakes] = guess; | |
mistakes += 1; | |
} | |
win = strchr(guessed, '_'); | |
}while(mistakes < CHANCE && win != NULL); | |
if(win == NULL) { | |
printf("\n"); | |
printWord(guessed, len); | |
printf("\n\tCongrats! You have won : %s\n\n", word); | |
} else { | |
printf("\n"); | |
printBody(mistakes, body); | |
printf("\n\n\tBetter try next time. Word was %s\n\n", word); | |
} | |
free(body); | |
free(word); | |
free(guessed); | |
return EXIT_SUCCESS; | |
} |
char values[WORDS][WORDLEN] = {"NmqOlJ^tZletXodeYgs","gCnDIfFQe^CdP^^B{hZpeLA^hv","7urtrtwQv{dt",>^}FaR]i]XUug^GI", "aSwfXsxOsWAlXScVQmjAWJG","cruD=idduvUdr=gmcauCmg]","BQt
zncypFVjvIaTl]u=?Aa}F",
"iLvkKdTyu~mWj[^gcO|","jSiLyzJ=vPmnv^
N]^>ViAC^z","xo|RqqhO|nNstjmzfiuoiFfhwtdh
"OHkttvxdp|[nnW]Drgaomdq"};
What is this???
pls help!!!!
i tried executing it in vs code but it gives me many errors. can someone help me with it
Hi soul-warrior1692 can I get more information about the error message you are getting. The code is checked and runs well. You can quickly try it here : https://www.onlinegdb.com/online_c_compiler . Just copy the source code and test it on this online compiler.
char values[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", "iLvkKdTyu~mWj[^gcO|","jSiLyzJ=vPmnv^
N]^>ViAC^z","xo|RqqhO|nNstjmzfiuoiFfhwtdh
"OHkttvxdp|[nnW]Drgaomdq"};
What is this???
pls help!!!!
Hi soul-warrior1692, they are the encrypted names of websites. These are websites what the user has to guess during the game. To hide it ( yes there is no DB integrated to get data ) from end user, the name of websites are encrypted using a function from this https://github.com/dhpradeep/100DaysCodeChallenge/blob/saroj/challenges/day04/encryptDecrypt.c project and can be decrypted using the function in the source code that is : decrypt(char* code).
In lines 141 and 142 seems to have a problem for me because the code cannot execute for me.
Hi 1-2cloud, can i get errors you are receiving. Cuz the code is working for me, in Linux and Windows with C99 / C11 Standard.
thx for the reply on my email, just wanted to know one more thing, how will I get to know which encryption is for which website name, as I want to remove some from my code?
thx for the reply on my email, just wanted to know one more thing, how will I get to know which encryption is for which website name, as I want to remove some from my code?
You can use the decrypt function from the source code.
I have written a small program to show you the example. Run it in online complier example https://www.onlinegdb.com/online_c_compiler or in your IDE to get the values listed. The program is :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WORDS 10
#define WORDLEN 40
char* decrypt(char* code) {
int hash = ((strlen(code) - 3) / 3) + 2;
char* decrypt = malloc(hash);
char* toFree = decrypt;
char* word = code;
for (int ch = *code; ch != '\0'; ch = *(++code))
{
if((code - word + 2) % 3 == 1){
*(decrypt++) = ch - (word - code + 1) - hash;
}
}
*decrypt = '\0';
return toFree;
}
int main()
{
char values[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 *word;
for(int i = 0; i < WORDS; ++i) {
word = decrypt(values[i]);
printf("Decrypted word ( Index %d ) : %s \n", i, word);
}
free(word);
return 0;
}
thank you so much. Great work sir. you r really very helpful😀
can you please explain this part of the code to me in brief?
bool srand_called = false;
int i_rnd(int i) {
if (!srand_called) {
srand(time(NULL) << 10);
srand_called = true;
}
return rand() % i;
}
char* decrypt(char* code) {
int hash = ((strlen(code) - 3) / 3) + 2;
char* decrypt = malloc(hash);//malloc is memory allocation. hash stores data.
char* toFree = decrypt;
char* word = code;
for (int ch = *code; ch != '\0'; ch = *(++code))
{
if((code - word + 2) % 3 == 1){
*(decrypt++) = ch - (word - code + 1) - hash;
}
}
*decrypt = '\0';
return toFree;
}
Can you write a code that creates a chatbot for us using C++
Im using Dev c and im getting an error on line 31 when trying to compile
Code works just fine in vs code, thanks for making my life A LOT easier at uni :>>
i tried executing it in vs code but it gives me many errors. can someone help me with it