need help using arrays ( simple hangman game)

This is a discussion on need help using arrays ( simple hangman game) within the C Programming forums, part of the General Programming Boards category; heeey guys I need your help to create a code. I totally forget how to use arrays and I need ...

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

    need help using arrays ( simple hangman game)

    heeey guys I need your help to create a code. I totally forget how to use arrays
    and I need to use two arrays for a hangman game one holds the characters of the secret word
    and the other one holds the characters of the current status.

    for example : if the secret word is " book"
    and the user guessed "o" "this is the 1st try"
    the output should look like this "-oo-"

    however when the user guess another letter let's say G "2nd try"
    the output should still be "-oo-"

    also, the user have only 8 tries

    anyone can help me

    that what I got so far
    and I know it's not right at all


    Code:
    #include <stdio.h>
    
    int main()
    {
    	char word[] = {'b','o','k'};
    
    	char j;
    	int i;
    	
    	
    	for(i=1; i<=8; i++)	
    	{
    		printf("Guess a letter: ");
    		scanf("%c", &j);
    		if(j == 'b')
    			printf("b---");
    		else if(j == 'o')
    			printf("-oo-");
    		else if(j == 'k')
    			printf("---k");
    		else 
    			printf("Guess again");
    		}
    
    	return(0); 
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       char word[] = {"book"};
       char gotIt[40] =  { ' ' };	//spaces
       char j;
       int guess, i, len;
    
       for(guess = 0; guess < 8; guess++)  {
           printf("\nGuess a letter: ");
           scanf(" %c", &j);
           len = strlen(word);
           gotIt[len] = '\0';
    
           for(i=0; i<len; i++)	
           {
    	   if(j == word[i] && j != gotit[i])  {
                  gotIt[i] = word[i];
                  printf("%s", gotIt);
                  if((strcmp(word, gotIt) == 0)	
                     printf("You have won! Congratulations ");
               }
    	   else 
    	      printf("Guess again, there are no %c's \n", j);
       }
    
          printf("\n\n\t\t       Press enter when ready");
          getchar();  //holds the console window open
          return(0); 
    }
    I haven't compiled the above, but it should give you a starting point


    For an upgraded version, I suggest:

    1) Download a small file of common words, from the net (Google "word list").

    2) Use a random number to choose the special word for each game, from that list.

    3) You need a text figure of a guy appearing more and more complete, with every
    failed guess. After 8 (or so) guesses, if all the letters have not been correctly guessed, his last body part appears and he gets hanged.

    That's the fun of hangman, right there. Otherwise, it's like "Wheel of Fortune" - boring.
    Last edited by Adak; 03-04-2009 at 04:05 AM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    WOW

    thanks a lot but u take it so far. all I need for this program is to work with only on secret word without any string.
    as basic as possible
    and I need the program to display a congratulatory message along with the number of guesses the user took " when the user discovers the secret word by the 8th guess. "
    otherwise displaying a consolation message and end the game.


    thanks again and I really appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i have to write simple car racing game?
    By iskelet in forum Game Programming
    Replies: 1
    Last Post: 04-01-2009, 04:34 PM
  2. Simple Game Engine Programming
    By AoA in forum Tech Board
    Replies: 1
    Last Post: 05-26-2006, 12:50 AM
  3. A simple game using RNG
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 10:05 AM
  4. Help with a simple game
    By jjj93421 in forum Game Programming
    Replies: 1
    Last Post: 03-28-2004, 02:52 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM

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