Code:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define MAXNUMINCORRECT 5
#define MAXWORDS 1000
#define MAXSIZE 30
int readWords(char allWords[][MAXSIZE],FILE *fin);
int loadWord(char allWords[][MAXSIZE], char word[MAXSIZE], int numWords);
void printBlank(char emptyword[MAXSIZE], int word_len);
char getMove(int letters_used[26]);
int doMove(int len, char emptyword[MAXSIZE], char word[MAXSIZE], char attempt);
int gameSolved(char emptyword[MAXSIZE], char word[MAXSIZE]);
void drawHangman(int tries);
int main(void)
{
char filename[MAXSIZE];
int numWords =0;
char choice[8];
char allWords[MAXWORDS][MAXSIZE];
int valid_move;
int letters_used[26];
char emptyword[MAXSIZE];
int already_used;
int tries;
int number_words;
char letter_guess;
int word_len;
char word[MAXSIZE];
int length_of_word = 0;
char guessed_letter;
char attempt;
int lengthWord;
// Seed the random generator
srand(time(0));
// Ask the user for the input file.
printf("What file stores the puzzle words?\n\n");
scanf("%s", filename);
FILE* fin;
fin = fopen(filename, "r");
if(fin == (0))
{
printf("Please enter a valid filename. (\"%s\") Press Enter to exit \n\n", filename);
system("PAUSE");
return 0;
}
//Ask the user if they would like to play the game
printf("1.) Would you like to play hangman? (yea/no)\n\n");
scanf("%s ", choice);
numWords = readWords(allWords, fin);
fclose(fin);
do{
tries = 0;
int j;
int k;
lengthWord = loadWord(allWords, word, numWords);
for(j = 0; j < 26; j++)
{
letters_used[j];
}
for(k = 0; k < lengthWord; k++)
{
emptyword[k] = '_';
}
while(tries != MAXNUMINCORRECT)
{
guessed_letter = getMove(letters_used);
printBlank(emptyword, word_len);
valid_move = doMove(word_len, emptyword, word, attempt);
if(gameSolved(word, emptyword))
{
printf("Congratulations, You got the correct word, %s.\n");
break;
}
if(valid_move != 0 && letters_used[(int)(guessed_letter - 'A')] >= 2) // If the letter has been guessed already and is in the puzzle, it counts as a miss
{
printf("Sorry, you have guessed that letter already.\n");
printf("Now it counts as a miss.\n");
tries++;
already_used = 1;
}
else
{
already_used= 0;
}
if(valid_move == 0) // If the letter the user guesses is not in the real word, subtract 1 from the number of attempts left
{
printf("Sorry, that letter is NOT in the puzzle.\n");
tries++;
}
else if(already_used == 0 && valid_move != 0 && !gameSolved(word,emptyword))
{
printf("Congratulations, you guessed a letter in the puzzle!\n");
}
if(tries != MAXNUMINCORRECT)
{
printf("You currently have %d incorrect guesses.\n", tries);
drawHangman(tries);
}
if(tries == MAXNUMINCORRECT)
{
drawHangman(tries);
}
}
}while(strcmp(choice, "no") != 0)
system("PAUSE");
return 0;
}
// Pre-conditions: allWords must be of size numWords, at least, fin must
// point to a file with n words, with only uppercase letters
// ready to read from the first word.
// Post-conditions: The numWords stored in the file pointed to by fin will
// be stored in the array allWords.
int readWords(char all_words[][MAX_SIZE], FILE *fin)
{
int numWords = 0, i;
fscanf(fin, "%d", &numWords);
if(i == EOF || i != 1)
return 0;
for (i=0; i < numWords; i++) // Loop through the file
{
fscanf(fin, "%s", allWords[i]); // Scan every word in the file
}
return numWords;
int loadWord(char allWords[][MAXSIZE], char word[MAXSIZE], int numWords)
{
int get_word;
int len;
get_word = rand() % numWords;
strcpy(word, allWords[get_word]);
len = strlen(word);
return len;
}
void printBlank(char emptyword[MAXSIZE], int word_len)
{
int j;
printf("Here is your puzzle:\n");
for(j = 0; j < word_len; j++)
{
printf("%c ", emptyword[j]);
}
printf("\n");
}
char getMove(int letters_used[26])
{
char attempt, up_attempt;
printf("Please enter your guess.\n");
scanf("%c", &attempt);
attempt = toupper(attempt);
return attempt;
}
int doMove(int len, char emptyword[MAXSIZE], char word[MAXSIZE], char attempt)
{
int valid;
int j;
for(j = 0; j < len; j++)
{
if(word[j] == attempt)
{
valid++;
attempt = emptyword[j];
}
}
return valid;
}
void drawHangman(int tries)
{
if(tries == MAXNUMINCORRECT)
{
char word;
printf("Sorry, you have made %d incorrect guesses, you lose. The word was %s.\n", MAXNUMINCORRECT, word);
printf("+----+ \n");
printf("| | \n");
printf("| O \n");
printf("| /|\\ \n");
printf("| / \\ \n");
printf("| Your're Dead \n");
printf("=============\n\n");
}
else if(tries == 4)
{
printf("+----+ \n");
printf("| | \n");
printf("| O \n");
printf("| /|\\ \n");
printf("| \\ \n");
printf("| \n");
printf("=============\n\n");
}
else if(tries == 3)
{
printf("+----+ \n");
printf("| | \n");
printf("| O \n");
printf("| /|\\ \n");
printf("| \n");
printf("| \n");
printf("=============\n\n");
}
else if(tries == 2)
{
printf("+----+ \n");
printf("| | \n");
printf("| O \n");
printf("| /| \n");
printf("| \n");
printf("| \n");
printf("=============\n\n");
}
else if(tries == 1)
{
printf("+----+ \n");
printf("| | \n");
printf("| O \n");
printf("| | \n");
printf("| \n");
printf("| \n");
printf("=============\n\n");
}
else if(tries == 0)
{
printf("+----+ \n");
printf("| | \n");
printf("| \n");
printf("| \n");
printf("| \n");
printf("| \n");
printf("=============\n\n");
}
}
int gameSolved(char emptyword[MAXSIZE], char word[MAXSIZE])
{
if(strcmp(word,emptyword) != 0)
{
return 0;
}
else
{
return 1;
}
}