Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void errorPrint(int wrong);
char userGuess(char guess[], int guessed);
int getWord(char wordToGuess[], int strLength);
int main ()
{
int wrong=1, i, n=0, guessed=0, strLength=0, j=0, r=0;
char guess[27], wordToGuess[500], complWord[50];
while(wrong<8 && strcmp(wordToGuess, complWord) != 0){
strLength=getWord(wordToGuess, strLength);
guessed=userGuess(guess, guessed);
for(i=0;i<strLength;i++){
if (wordToGuess[i]==guess[n] && i<strLength){
complWord[i]=wordToGuess[i];
printf("You got the letter %c correct!\n", wordToGuess[i]);
n++;
userGuess(guess, guessed);
printf("You have %d letters left to guess!\n", (strLength-1)-n);
continue;
}if(i==strLength-1){
errorPrint(wrong++);
userGuess(guess, guessed);
r++;
break;
}
}
}
}
char userGuess(char guess[], int guessed)
{
printf("Choose a letter you wish to guess:\n");
scanf("%c", &guess[guessed]);
guessed++;
return (int)guessed;
}
int getWord(char wordToGuess[], int strLength)
{
FILE *fp = fopen("word.txt", "r");
char c[1500]="/0";
int pos = 0, n=0, k=0, len=0;
k=rand()% ((32+1)-1)+1;
if (fp != NULL) {
while (fgets(c, 1500, fp) != NULL) {
if (sscanf(c, "%[^,],%n", wordToGuess, &n) != 1)
continue;
printf("%s:\n", wordToGuess);
pos += n;
while (sscanf(c+pos, "%15[^,\n]%*c%n", wordToGuess, &n) == 1) {
printf("\t%s\n", wordToGuess[n]);
pos += n;
}
printf("\%s\n", wordToGuess);
pos+=n;
}
}
strLength=strlen(wordToGuess);
fclose(fp);
return (int)strLength;
}
void errorPrint(int wrong)
{
switch (wrong){
case 0:
break;
case 1:
printf("You have guessed %d wrong\n", wrong);
printf(" _______\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("_________|\n");
break;
case 2:
printf("You have guessed %d wrong\n", wrong);
printf(" _______\n");
printf(" | |\n");
printf(" 0 |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("_________|\n");
break;
case 3:
printf("You have guessed %d wrong\n", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("| | |\n");
printf(" |\n");
printf(" |\n");
printf("_________|\n");
break;
case 4:
printf("You have guessed %d wrong\n", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("//| | |\n");
printf(" |\n");
printf(" |\n");
printf("___________|\n");
break;
case 5:
printf("You have guessed %d wrong\n", wrong);
printf(" _______\n");
printf(" | |\n");
printf(" 0 |\n");
printf("//| |\\\\ |\n");
printf(" |\n");
printf(" |\n");
printf("___________|\n");
break;
case 6:
printf("You have guessed %d wrong\n", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("//| |\\\\ |\n");
printf(" || |\n");
printf(" |\n");
printf("____________|\n");
break;
case 7:
printf("You have guessed %d wrong, You Lose\n", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("//| |\\\\ |\n");
printf(" || || |\n");
printf(" |\n");
printf("____________|\n");
break;
}
return;
}