Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAXSIZE 30
#define MAXWORDS 1000
#define MAXINCORRECT 5
char getGuess(int usedletters[26]);
int readWords(char allWords[][MAXSIZE], char word[MAXSIZE], FILE *fin, int n);
int doGuess(char word[MAXSIZE], char blankword[MAXSIZE], char guess[MAXSIZE], int length);
int solved(char word[MAXSIZE], char blankword[MAXSIZE]);
void printblank(int length, char blankword[MAXSIZE]);
int main(void)
{
srand(time(NULL));
char allWords[MAXWORDS][MAXSIZE];
char filename[MAXSIZE];
char word[MAXSIZE];
char blankword[MAXSIZE];
char guessletter[MAXSIZE];
char choice[16];
int usedletters[26];
int numWords = 0;
int wordlength = 0;
int attempts = 0;
int goodmove = 0;
int usedalready = 0;
printf("What file stores the puzzle words?\n"); // Ask the user for the input file
scanf("%s", filename);
printf("\n");
printf("Would you like to play hangman (yes,no)?\n");
scanf("%s", choice);
printf("\n");
while(strcmp(choice, "no") != 0)
{
attempts = 0; // Reset the number of attempts for each new game
int i; // Loop index
FILE* fin;
fin = fopen(filename, "r");
if(fin == (NULL))
{
printf("\nEnter a valid filename.\n\n");
break;
}
fscanf(fin, "%d", &numWords);
wordlength = readWords(allWords, word, fin, numWords); // Read all the words (also select a random word), and set wordlength
// to what is returned by the readWords function
fclose(fin); // Close the file
for(i = 0; i < wordlength; i++)
{
blankword[i] = '_'; // Initialize the "blank" word string
}
for(i = 0; i < 26; i++)
{
usedletters[i] = 0;
}
while(attempts != MAXINCORRECT)
{
printblank(wordlength, blankword); // Print the "blank" word the length of the real word
guessletter[0] = getGuess(usedletters); // Get the user's guess
goodmove = doGuess(word, blankword, guessletter, wordlength); // Find out if the letter was found in the real word
if(solved(word,blankword))
{
printf("\nCongratulations! You got the correct word, %s.\n", word); // Tell the user if they have solved the puzzle
break;
}
if(goodmove != 0 && usedletters[(int)(guessletter[0] - 'A')] >= 2) // If the letter has been guessed already and is in the puzzle, it counts as a miss
{
printf("\nSorry, you have guessed that letter already.\n");
printf("Now it counts as a miss.\n");
attempts++;
usedalready = 1;
}
else
{
usedalready = 0;
}
if(goodmove == 0) // If the letter the user guesses is not in the real word, subtract 1 from the number of attempts left
{
printf("\nSorry, that letter is NOT in the puzzle.\n");
attempts++;
}
else if(usedalready == 0 && goodmove != 0 && !solved(word,blankword))
{
printf("\nCongratulations, you guessed a letter in the puzzle!\n"); // Tell the user they guessed a letter in the puzzle
}
if(attempts != MAXINCORRECT)
{
printf("\nYou currently have %d incorrect guesses.\n", attempts); // Tell the user their number of incorrect guesses
}
}
if(attempts == MAXINCORRECT)
{
printf("\nSorry, you have made %d incorrect guesses, you lose. The word was %s.\n", MAXINCORRECT, word); // If the user has 0 attempts left, they lose and the real word is printed out
}
printf("\nWould you like to play hangman (yes,no)?\n"); // Asks the user to play again
scanf("%s", choice);
printf("\n");
}
printf("Thanks for playing!\n\n");
system("PAUSE");
return 0;
}
int readWords(char allWords[][MAXSIZE], char word[MAXSIZE], FILE *fin, int n)
{
int i; // Loop index
for (i=0; i < n; i++) // Loop through the file
{
fscanf(fin, "%s", allWords[i]); // Scan every word in the file
}
int randomword = 0; // The number for the random word
int length = 0; // The length of the word
randomword = rand() % n; // Makes randomword a random number less than the number of words
strcpy(word, allWords[randomword]); // Copies to random word to the puzzle word
length = strlen(word); // Determines the length of the random word
return length; // Returns the length of the random word
}
// Pre-conditions: needs the length of the "blank" word and the string of the blank word
// Post-conditions: prints out the "blank" word
void printblank(int length, char blankword[MAXSIZE])
{
int i = 0; // Loop index
printf("Here is your puzzle:\n");
for(i = 0; i < length; i++)
{
printf("%c ", blankword[i]); // Print out the "blank" word
}
printf("\n");
}
// Pre-conditions: none
// Post-conditions: returns the letter the used guessed and adds 1 to the number of times this letter has been used
char getGuess(int usedletters[26])
{
char guess[0];
printf("\nPlease enter your guess.\n"); // Ask the user for their guess
scanf(" %c", &guess[0]);
usedletters[(int)(guess[0] - 'A')]++; // Adds one to the number of uses for each letter
return guess[0];
}
int doGuess(char word[MAXSIZE], char blankword[MAXSIZE], char guess[MAXSIZE], int length)
{
int i = 0; // Loop indices
int j = 0;
int good = 0; //
for(i = 0; i < length; i++)
{ // If the letter they guess is in the real word, swap it for any "blank" letters and return a value > 0
if(guess[0] == word[i])
{
blankword[i] = guess[0];
good++;
}
}
return good;
}
int solved(char word[MAXSIZE], char blankword[MAXSIZE])
{
if(strcmp(word,blankword) == 0)
{
return 1;
}
else
{
return 0;
}
}
Good luck lol. A sample .txt file for it looks like this: