I am trying to write the logic for the Hangman game. I want to be able to enter a word and to get a random word from a list.
The part in which that I choose a random word works great without a problem. The problem starts when I enter a word. No matter if I am guessing the correct char it will still count as a strike. What is the explanation for why this is happening?
int main()
{
int option=5, randomword;
bool loop = true;
// here you choose if you want a random word or to enter one by yourself.
while (loop)
{
printf("choose an option:\n0 to enter a word.\n1 for the computer to choose a word for you.\n");
scanf("%d", &option);
// start the loop over again if the value that got scan is not 0 or 1;
if (option == 0 || option == 1)loop = false;
}
char EnterdWord[128];
// if you choose to enter a word it will scan it and enter it to a string. I am using fgets so you can enter space
if (option == 0)
{
printf("enter a word:\n");
getchar();
fgets(EnterdWord , 128, stdin);
}else // if you want a random word, this will make a random number, so later it will choose a random word from a list.
{
srand(time(NULL));
randomword = rand() % 20 ;
}
// random words list
char RanWord[][20] = {"dog", "cat", "sky", "orange", "apple", "movie", "code", "number" ,"ship" ,"google" ,"sushi" , "shower",
"super hero", "job interview" ,"computer" ,"video games" ,"pizza" ,"cell phone", "charger", "bottle"};
char *TempWord = malloc(1024 * sizeof(char));
// remove the \n from the scan in fgets.
if (*EnterdWord && EnterdWord[strlen(EnterdWord)-1] == '\n')EnterdWord[strlen(EnterdWord)-1] = 0;
// give to the string TempWord the word based on the option u choose
if (option == 0) strcpy(TempWord, EnterdWord);
if (option == 1) strcpy(TempWord, RanWord[randomword]);
//this is the string that you are going to scan your right guess.
char *NiceWord = malloc(1024 * sizeof(char));
if (NiceWord == NULL) return 0;
// copy temp word to nice word
strcpy(NiceWord, TempWord);
// finding the length of the word for the loop below (I know I can use while != \0)
int lengh = strlen(TempWord);
int i=0;
// this part is for aesthetics. I could have just changed all the characters to space.
while(i < lengh)
{
if (TempWord[i] != ' ')NiceWord[i]='_';
if (TempWord[i] == ' ')NiceWord[i]=' ';
i++;
}
loop = true;
char guess;
// bool verb to check for strikes.
bool CheckStrike = true;
int y, strikes=0;
// print so you can see how many characters the word is.
printf("\n%s\n", NiceWord);
while (loop == true)
{
y=0;
//scan your guess.
printf("enter your guess:\n");
getchar();
scanf("%c", &guess);
// reset the strike checker in case you where right for the last guess
CheckStrike = true;
// check if your guess is correct or not.
while(TempWord[y] != '\0')
{
if ( TempWord[y] == guess)
{
NiceWord[y] = guess;
CheckStrike = false;
}
y++;
}
// if your guess was false, add one strike
if (CheckStrike == true)strikes++;
printf("you have %d strikes left\n", 10-strikes);
printf("%s\n", NiceWord);
// check if you lost
if (strikes == 10)
{
printf("the word was: %s\nyou lost :( ", TempWord);
loop = false;
}
// check if you won
if (strcmp (NiceWord, TempWord) == 0)
{
printf("\n\n\n\nu won :) with %d strikes left!\n\n\n\n\n", 10-strikes);
loop = false;
}
}
return 0;
}