Q BgQuestion:

      
Novice
Karma Points: 25
Respect (94%):
posted by  10 on 11/3/2009 12:38:46 AM  |  status: Closed  |  Earned Karma: 25

c language

Course Textbook Chapter Problem Needs by
N/A N/A N/A N/A 11/11/2009 at 11:00:00 AM
Question Details:
Must work in C environment

Program Description:
Using the program earlier, write a "hangman game" program in which the user attempts to guess all the letters in a "mystery word" chosen from the data file provided by the user. The user should be allowed to play as many game as he wishes before quitting. For each game, the program should randomly select a new word from the array. The user should be shown the secret word as a series of dashes. The number of dashes should correspond to the number of letters in the word. (If the word is "hello" the program would print out: -----)

The program should request that the user enter guesses as to the letters in the secret word. After each guess, a list of all letters that the user has guessed so far should be printed out, followed by the latest version of the "word so far" with the correctly guessed letters in place of the dashes. If the user makes nine incorrect guesses before guessing all the letters contained in the word, then print out a "You Lose" message together with the chosen word. If the user guesses all the correct letters in the chosen word, print the word with a "You Win!" message. Whether the user wins or loses, the program should allow him to play again (see the sample execution below)

Specifications:
Program should have at least 2 functions in addition to previous hangman exercise.

1) a function of type int called good_guess that checks to see whether the most recent letter input by the user is a letter contained in the mystery word. If so, the function returns 1. Otherwise, the function returns 0.

2) a function called print_guessed_letters that will print to the screen all letters previously guessed during this game, including both correct and incorrect letters. (This reminds the user which letters are used up and which ones are remaining.)

3) initialize a cstring called "word_so_far" with dashes for each letter of the chosen word, and a null terminator at the end to show where the word ends. (Hint: you will need to use the function strlen to determine the length of the word)

4) You will also need to modify the function get_random() which you wrote earlier, so that it will not choose the same "random" word each time it is called. To do this, you need to "seed" the random number outside the function which returns the random array element. Also, you should "seed" the random number generator with the system time, rather than with a constant, in order to have different words chosen each time the game is played.

5) The program may assume that the user will enter valid guesses (i.e. uppercase or lowercase alphabet letters). You need not check for type errors, such as the user entering a numeric value instead of an alphabetical one.

Sample Execution:
---------------------------------
Enter the hangman word file name: hangman.dat
---------------------------------
This is a fun word game hangman

Here is your word so far: --------

Enter a new letter: R
Sorry! That letter is not in the chosen word!
The letters you have guessed so far are: R
Here is your word so far: --------

Enter a new letter: s
Good guess! My word contains your letter!
The letters you have guessed so far are: R
Here is your word so far: S--S----

Enter a new letter: z
Sorry! That letter is not in the chosen word!
The letters you have guessed so far are: R Z
Here is your word so far: S--S----

Enter a new letter: E
Good guess! My word contains your letter!
The letters you have guessed so far are: R Z
Here is your word so far: S--S---E

Enter a new letter: i
Good guess! My word contains your letter!
The letters you have guessed so far are: R Z
Here is your word so far: S--S-I-E

Enter a new letter: t
Good guess! My word contains your letter!
The letters you have guessed so far are: R Z
Here is your word so far: S--STI-E

Enter a new letter: o
Good guess! My word contains your letter!
The letters you have guessed so far are: R Z
Here is your word so far: SO-STI-E

Enter a new letter: L
Good guess! My word contains your letter!
The letters you have guessed so far are: R Z
Here is your word so far: SOLSTI-E

Enter a new letter: c
Good guess! My word contains your letter!
The letters you have guessed so far are: R Z
Congratulations! You win!
The word was: SOLSTICE

Play again?? (y/n) y







Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification
Oracle
Karma Points: 31,937
posted by rapunzel on 11/4/2009 9:32:20 PM  |  status: Live
Asker's Rating: Lifesaver   
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;
}




Note to all members 1. Please 1 question per post 2. Show respect to your fellow members by rating all answers. 3. When rating remember that the first answer is not necessarily the best answer. 4. When answering questions, explain what you are doing, so that the asker will learn, don't just give a meaningless number. 5. Your answers should be your work. Don't copy from another member, this is Karma abuse and possible disciplinary actions against you. and GOOD LUCK to you all!!
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »