|
Response Details:
please rate - thanks
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_GUESSES 5
#define MAX_INCORRECT_GUESSES 9
#define MAX_WORD_LENGTH 15
#define MAX_WORDS 50
int read_words(char[],char[][MAX_WORD_LENGTH]);
void get_word(char[][MAX_WORD_LENGTH],char[],int);
int get_random(int);
void print_guessed_letters(char[],int);
int good_guess(char[],char);
void printword(char[]);
char getletter();
int main()
{
FILE *output;
int i,count,used=0,guesses=0,good;
char filename[20];
char WordArray[MAX_WORDS][MAX_WORD_LENGTH];
char Word[MAX_WORD_LENGTH];
char word_so_far[MAX_WORD_LENGTH];
char lettersUsed[26];
char again='y',guess;
srand(23); //"seeds" the random number generator
while(toupper(again)=='Y')
{
printf("This is the game of Hangman\n\n");
printf("I will choose a word, and you will enter characters to\n");
printf("try to guess the characters in my word without making\n");
printf("more than nine incorrect guesses. If you guess all the letter\n");
printf("in my word before making nine incorrect guesses, you win!\n\n");
printf("What file would you like to use for data input: \n");
scanf("%s",&filename);
printf("\n");
printf("We will be using the file %s for our word choices today.\n\n");
count=read_words(filename,WordArray);
if(count==-1)
return 1;
get_word(WordArray,Word,count);
printf("Your file had %d words\n\n", count);
// printf("Your *hangman secret word* is: %s\n\n",Word);
printf("What file name would you like to use for saving the chosen word?: \n");
scanf("%s",&filename);
printf("\n");
while( (fgetc( stdin )) != '\n' );
/* printf("The chosen word has been saved!\n\n");
printf("That's all for now.\n");
printf("Goodbye!\n");
output= fopen(filename,"w");
if(output==NULL)
{
printf("Error opening Output File!\n");
getchar();
return 0;
}
fprintf(output,"%s\n",Word);
fclose(output);
*/
used=0;
guesses=0;
count=0;
for(i=0;i<26;i++)
lettersUsed[i]=' ';
for(i=0;i<strlen(Word);i++)
word_so_far[i]='-';
word_so_far[i]='\0';
printf("\n\nYour word: ");
printword(word_so_far);
while(guesses<9&&count<strlen(Word))
{guess=getletter();
lettersUsed[used]=guess;
used++;
good=good_guess(Word,guess);
if(good==1)
{for(i=0;i<strlen(Word);i++)
if(toupper(Word[i])==toupper(guess))
{word_so_far[i]=toupper(guess);
count++;
}
}
else
guesses++;
print_guessed_letters(lettersUsed,used);
printf("\n\nHere is your word so far: ");
printword(word_so_far);
}
if(guesses==9)
printf("Sorry! You lost\n");
else
printf("Congratulations! You win!\n");
printf("The word was: ");
printword(Word);
printf("another game?(Y/N) ");
scanf("%c",&again);
}
getchar();
system("pause");
}
char getletter()
{char letter;
printf("Enter a new letter: ");
scanf("%c",&letter);
while( (fgetc( stdin )) != '\n' );
return letter;
}
void printword(char w[])
{int i;
for(i=0;i<strlen(w);i++)
printf("%c",w[i]);
printf("\n\n");
}
int good_guess(char w[],char let)
{int i;
for(i=0;i<strlen(w);i++)
if(toupper(let)==toupper(w[i]))
{printf("Good guess! My word contains your letter!\n");
return 1;
}
printf("Sorry! That letter is not in the chosen word!\n");
return 0;
}
void print_guessed_letters(char letters[],int n)
{int i;
printf("The letters you have guessed so far are: ");
for(i=0;i<n;i++)
{putchar(letters[i]);
printf(" ");
}
printf("\n");
}
void get_word(char a[][MAX_WORD_LENGTH],char w[],int count)
{
int n,i;
n=get_random(count);
for(i=0;i<MAX_WORD_LENGTH;i++)
w[i]=a[n][i];
return;
}
int get_random(int num_words)
{
return(rand() % num_words);
}
int read_words(char filename[],char a[][MAX_WORD_LENGTH])
{
int i,j,count=0,k;
FILE *input;
input= fopen(filename,"r");
if(input==NULL)
{
printf("Error opening Input File!\n");
getchar();
return -1;
}
i=0;
while(i<MAX_WORDS)
{
j=0;
k=0;
while(j<MAX_WORD_LENGTH)
{
a[i][j]=fgetc(input);
k=1;
if(a[i][j]==EOF)
k=0;
if(a[i][j]==' '||k==0||a[i][j]=='\n')
{
a[i][j]='\0';
if(j!=0)
count++;
j=MAX_WORDS;
}
j++;
if(k==0)
i=MAX_WORDS;
}
i++;
}
fclose(input);
return count;
}
|