   Hangman Ver 2
  Hi I would just like some feedback on the second version of this game that I coded based on the feedback I got first time around. This can be viewed at http://www.gamedev.net/community/forums/topic.asp?topic_id=199836.The main improvements in this version is that the program is broken down into more functions and that the words are selected from a text file rather than from an array of strings.Language: C IDE: Dev-C++ 4.9.8.4One of the major critiques of my code the first time round was that I had too much going on in my main function. The questions therefore are:1. I have tried breaking the program down into more functions and I wound like to know how successful people think this has been achieved in my program?2. While breaking the program down I have been tempted to use global variables so that I would not have to pass in so many parameters to certain functions. Considering how small the program is do you think this is a good idea?  #include <stdio.h>#include <stdlib.h>//constants#define MAXSTRING 20#define LIVES 5//function prototypesvoid flush();void initialise(char *, char *);void generateword(char *);int gamemain(char *, char *, char, int *);void output(int);int main(int argc, char *argv[]){   char word[MAXSTRING] = {0};//holds the word that player must match  char word2[MAXSTRING] = {0};//the word that the player sees    char guess;//holds the letter that the player guessed  char play;//holds player response for playing again(y or n)int lives = LIVES;//holds the number of lives that the player has left  int stat;//determines whether a round has been finished    srand((unsigned)time(NULL));//seed the random function    initialise(word, word2);    printf("Hangman\n");  printf("=======\n\n");    //start gameloop  do  { printf("%s\tlives left: %d\n\n", word2, lives);//output//input printf("Enter a letter: ");    scanf("%c", &guess);    flush();   stat  = gamemain(word, word2, guess, &lives);   output(lives);//graphics output   if(stat == 0)    { printf("Play again(y/n): "); scanf("%c", &play); flush(); if(play == 'y') {   lives = LIVES;//set lives back to normal    initialise(word, word2); }    }    //printf("%s\n", word2);}while(play != 'n');system("PAUSE");  return 0;}//function for carraige return problemvoid flush(){  char ch;  while((ch = getchar()) != '\n');}//function to set up the gamevoid initialise(char *word, char *word2){  int i;   generateword(word);  strcpy(word2, word);    //create the blanks in the word  for(i=0; i<strlen(word); i++)  {    word2[i] = '*';  }}//function to randomly generate a wordvoid generateword(char *word){  FILE *fPtr;  char str[MAXSTRING];  int num=0, rnum;  if((fPtr=fopen("words.txt", "r")) == NULL)    printf("File not found\n");  else  {    while(fgets(str, MAXSTRING, fPtr) != NULL)    { num++;    }  }    rnum = rand() % num;    rewind(fPtr);    int i=0;    do  {    if(i == rnum)    { fscanf(fPtr, "%s", word); break;    }    i++;  }while(fgets(str, MAXSTRING, fPtr) != NULL);    fclose(fPtr);}//function to check whether the letter was found or not and to see whether a round is finishedint gamemain(char *word, char *word2, char guess, int *lives){   int i;//looping variable    int status;//flag for determining whether a letter has been found in the word(0 = found or 1 = not found)    int finish=1; for(i=0; i<strlen(word); i++)    { if(guess == word[i]) {   status = 0;   break; } else   status = 1;    }   switch(status)    { case 0:  for(i=0; i<strlen(word); i++) {   if(guess == word[i])word2[i] = guess; } break; case 1:   (*lives)--; break;    }   //check to see if the player has guessed all the letters    if(strcmp(word, word2) == 0)    { printf("Congratulations, you have won\n"); printf("word is %s\n", word); printf("word2 is %s\n", word2); finish = 0;    }   //check to see if the player has run out of lives    if((*lives) == 0)    { printf("Sorry you lost\n"); printf("word was %s\n", word); printf("word2 is %s\n", word2); finish = 0;    }    return finish;}void output(int lives){  switch(lives)  {    case 0: printf("=======\n");  printf("|=\n");  printf("|o\n");  printf("|    /|\\\n");  printf("|    / \\\n");  printf("|\n");  printf("|======\n");  printf("|========\n");  printf("|==========\n");  break;    case 1: printf("=======\n");  printf("|=\n");  printf("|o\n");  printf("|    /|\\\n");  printf("|    / \n");  printf("|\n");  printf("|======\n");  printf("|========\n");  printf("|==========\n");  break;    case 2: printf("=======\n");  printf("|=\n");  printf("|o\n");  printf("|    /|\\\n");  printf("|\n");  printf("|\n");  printf("|======\n");  printf("|========\n");  printf("|==========\n");  break;    case 3: printf("=======\n");  printf("|=\n");  printf("|o\n");  printf("||\n");  printf("|\n");  printf("|\n");  printf("|======\n");  printf("|========\n");  printf("|==========\n");  break;    case 4: printf("=======\n");  printf("|=\n");  printf("|o\n");  printf("|\n");  printf("|\n");  printf("|\n");  printf("|======\n");  printf("|========\n");  printf("|==========\n");  break;    case 5: printf("=======\n");  printf("|=\n");  printf("|\n");  printf("|\n");  printf("|\n");  printf("|\n");  printf("|======\n");  printf("|========\n");  printf("|==========\n");  break;  }}[\source]How about them apples?[edited by - popcorn on February 17, 2004 6:48:29 AM]
  bump 