Thread: Hangman game problems

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Hangman game problems

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define SIZE 30
    #define MAXGUESS 6
    void OpenFile(char word_to_guess[]);
    void MatchArray(char word_to_guess[],char word_in_progress[], int *length, char guessed_letters[]);
    char GetGuess();
    void CheckGuess(char word_to_guess[], char word_in_progress[],char guessed_letters[], int USEDGUESS, char guessed, int *length);
    void Instructions();
    int StrCmp(char word_to_guess[], char word_in_progress[], int *result);
    int main()
    {
    char word_to_guess[SIZE], word_in_progress[SIZE], guessed_letters[SIZE], guessed='a';
    int length=0, USEDGUESS=0, result=0;
    
    		Instructions();
    		OpenFile(word_to_guess);
    		MatchArray(word_to_guess, word_in_progress, &length, guessed_letters);
    		do
    		{
    		guessed=GetGuess(guessed,guessed_letters);
    		CheckGuess(word_to_guess,word_in_progress,guessed_letters,USEDGUESS, guessed,&length);
    		result=StrCmp(word_to_guess,word_in_progress,&result);
    		}while(result != 1);
    }
    
    void MatchArray(char word_to_guess[],char word_in_progress[], int *length, char guessed_letters[])
    {
    	int k;
    
    	*length = strlen(word_to_guess);
    	printf("\n%d\n", &length);
    for(k=0; k<SIZE; k++)
    {
    	word_in_progress[k]=0;
    }
    
    for(k=0; k<*length; k++)
    {
    	word_in_progress[k]='*';
    }
    for(k=0; k<SIZE; k++)
    {
    guessed_letters[k]=0;
    }
    printf("%s\n", word_in_progress);
    }
    
    char GetGuess(char guessed, char guessed_letters[])
    {
    	int k=0;
    	char guess='a';
    	printf("\n\nPlease input a letter to guess\n");
    	scanf("%c", &guess);
    	guessed=tolower(guess); // – takes the original letter and returns the lowercase equivalent
    	printf("%c\n", guessed);
    	return guessed;
    	guessed_letters[k]=guessed;
    	k++;
    	printf("%s", guessed_letters);
    
    }
    void CheckGuess(char word_to_guess[], char word_in_progress[],char guessed_letters[], int USEDGUESS, char guessed, int *length)
    {
    
    char *position;
    int j, i=0,k=0, correct=0;
    
    
    
    		printf("%s\n", word_to_guess);
    		printf("\n\n%c\n\n", guessed);
    		position=strchr(word_to_guess, guessed);
    		printf("%d\n", position-word_to_guess+0);
    		if(position != NULL)
    		{
    		word_in_progress[position-word_to_guess+0]=guessed;
    		printf("%s\n", word_in_progress);
    		printf("%s\n", guessed_letters);
    		//guessed_letters[k]=guessed;
    		printf("%s\n", guessed_letters);
    		}
    
    }
    void Instructions()
    {
    	printf("Welcome to the express HangMan Game\n");
    	printf("You will be guessing a random word selected from a secret text file\n");
    	printf("You have a max of 6 incorrect guesses before your game ends\n");
    	printf("If you manage to correctly guess the word before your 6 guesses are up you win!\n");
    	printf("Have fun, try to enjoy yourself!\n");
    }
    //void ContentsOfArray
    void OpenFile(char word_to_guess[])
    {
    FILE *inptr;
    inptr= fopen("words.txt","r"); //Words.txt opened and read for use
    fscanf(inptr,"%s", word_to_guess);//get word from file
    printf("%s", word_to_guess);
    }
    
    int StrCmp(char word_to_guess[], char word_in_progress[], int *result)
    {
    int equal=0;
    	
    	equal = strcmp(word_to_guess, word_in_progress);
    	
    	if(equal == 0)
    	{
    		return *result=1;
    	}
    	if(equal != 0)
    	{
    		return *result=0;
    	}
    
    
    }


    Ok basically I am wracking my brain on trying to figure out where to go from here in order to properly display my guessed_letters array, and add the guessed letters each time the user inputs a guess, and to keep the entire program running while the user doesnt use the global max incorrect guesses of 6 and the word is not guessed correctly. So far this will run, read the word from words.txt, and if you input the correct word it will terminate properly. My assignment requires that at all times the user inputted guesses show, incorrect or correct along with the *'d word of what they are trying to guess.

    Any sort of insight will be highly appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could have an array - I'll call it used[], which has 26 elements in it 0-25.

    It's a boolean thing - every number that is guessed, gets a one put into the used element which corresponds to it's letter value (where A = 0, through Z = 25).

    now showing the used letters is easy

    For each new game, initialize the used array to all zero's. Then

    In pseudo code:
    Code:
    for 0 to 25
      if(used[index] == 1)
        print the letter corresponding to that index - it's been used already.
    end of for
    You get the letter's corresponding number by first, making the letter uppercase (lower is fine also, but this example is uppercase), and then subtracting A from the letter:

    corresponding number = Letter - 'A'

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    I understand the logic behind that. However I guess my main issue lies in that I cannot for the life of me figure out how I should structure my functions so that I can continue to take input until they are out of guesses, or the word is guessed. Either way the guessed letters have to be displayed everytime a guess is made, but I can't get that if I can't figure out how to structure the rest of my functions >.>

    I think I am just overthinking alot of this and it's not helping me at all..been very frustrated -.-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game problem
    By piradie in forum C Programming
    Replies: 9
    Last Post: 12-30-2008, 04:29 PM
  2. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM