hangman

This is a discussion on hangman within the C Programming forums, part of the General Programming Boards category; I have to create a basic hangman game for a class using two arrays. What I am struggling with is ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    hangman

    I have to create a basic hangman game for a class using two arrays. What I am struggling with is when the user inputs the letters and gets them right i dont know how to output the letters hes has right in the mystery word

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I can only offer suggestions based on knowing what you have already coded. So be a good sport and post some code for me, por favor.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char word[] = {'j','a','r','i','d'};
    	char let,i;
    	
    	for(i=0; i<10; i++){
    		printf("Guess a letter: ");
    		scanf("%c", &let);
    		if(let == 'j')
    			printf("j****");
    		else if(let == 'a')
    			printf("*..........*");
    		else if(let == 'r')
    			printf("**r**");
    		else if(let == 'i')
    			printf("***i*");
    		else if(let == 'd')
    			printf("****d");
    		else
    			printf("Guess again");
    		}
    		
    		system("pause");
    	}
    This is what i have but it is not anything near correct, i'm just not sure how to go about doing this.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Check this out. What if you had another array of chars that was all filled up with zeros, right. Now lets say your player pics the char, 'j'. Now what if you made a function that compares what the user put in with all of the leters in the target string, as well as what is in that second zeroed out memory buffer.

    Ok, now it finds that you have never guessed 'j' before, but it finds that 'j' does exist in the target string. So it places a j in the answer buffer in the first index of the array.

    Does this make sense? Its how I would go about this.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Yes that somewhat makes sense and that is how i believe it is supposed to be done but i am having trouble putting that into code

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok, well I can show you how to do what I mean. Just don't go around thinking this means I am doing homework for you.

    Example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char word[] = {'j','a','r','i','d'};
    	char *shadow;
    	char let,i, shadow_length;
    	
    	shadow_length = sizeof word / sizeof word[0];
    	shadow = malloc(shadow_length + 1);
    	shadow[shadow_length] = 0;
    
    	for(i = 0; i < shadow_length; ++i)
    		shadow[i] = '*';
    
    
    	for(i=0; i<10; i++){
    		printf("Guess a letter: ");
    		scanf("&#37;c", &let);
    		if(let == 'j')
    			printf("j****");
    		else if(let == 'a')
    			printf("*..........*");
    		else if(let == 'r')
    			printf("**r**");
    		else if(let == 'i')
    			printf("***i*");
    		else if(let == 'd')
    			printf("****d");
    		else
    			printf("Guess again");
    		}
    		
    		system("pause");
    	}
    From here on out its all you buddy.
    Last edited by master5001; 10-24-2008 at 05:03 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Haha thanks for that but we haven't learned about the string library so i'm not really sure what that does.
    Also after each correct letter is guessed it needs to properly show which ones were guessed, i.e. if j and a were guessed it would display j..........*
    Last edited by jstrike21; 10-24-2008 at 05:03 PM.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You may not want to use memset() though. It doesn't account for spaces, but since I am just trying to get you off to a start, its a direction to move in. I have to go though so I will leave you in tabstops' capable hands.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Help - Code included
    By darren78 in forum C Programming
    Replies: 3
    Last Post: 02-17-2009, 09:35 AM
  2. Hangman Game Troubles
    By emerica240 in forum C Programming
    Replies: 9
    Last Post: 11-26-2008, 01:39 AM
  3. Hangman, Help me with the thinking
    By Livijn in forum C# Programming
    Replies: 14
    Last Post: 02-09-2008, 03:16 PM
  4. Help doing Hangman...
    By Kreative in forum C Programming
    Replies: 11
    Last Post: 08-18-2002, 10:22 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21