Hangman Game - Part 2

This is a discussion on Hangman Game - Part 2 within the C Programming forums, part of the General Programming Boards category; I've worked a bit more on the program since a week ago, and I think I've made some significant progress, ...

  1. #1
    C Programming Newbie
    Join Date
    Nov 2005
    Posts
    12

    Hangman Game - Part 2

    I've worked a bit more on the program since a week ago, and I think
    I've made some significant progress, but there're still a few things I
    either don't understand how to do, or am doing wrong. With that said,
    here's the new source:
    Code:
    int hangman()
    {
      char input;
      char temp[12];
      char word[][20] = {"literature","scholar", "enormous", "influence",
    "publication", "pioneer", "reshape", "catalyst", "leader", "member",
    "final", "phonograph", "executed",
                         "oldest", "people", "requiring", "screwdriver", "buckshot",
    "different", "striking"};
    
      srand((unsigned)time(NULL)); /* Provide a seed to rand() */
      int k = (rand() % 20);
      int i = 0;
      int r = 0;
    
      char b[]= "*******";
    
      int length = strlen(word[k]);
      int turn = 7;
      int dummy = 0;
      int decide = 0;
    
      printf("\nSorry, no more turns left. The secret word was %s.\n",
    word[k]);
    
      for(i = 0; i < length; i++)
        {
          printf("%c", b[i]);
        }
    
      while(turn != 0)
        {
          printf("\nTurns left:  ");
          printf("%d", turn);
    
          printf("\nEnter a letter to guess:  ");
          input = getchar();
          while(getchar() != '\n');
    
          for(r = 0; r < length; r++)
            {
              if(input == word[k][r] && b[r] == '*')
                {
                  decide = 1;
                  turn = length;
                }
              else
                {
                  decide = 0;
                }
            }
    
          if(decide == 0)
            {
              dummy++;
              turn = length - dummy;
            }
    
          for(r = 0; r < length; r++)
            {
              if(input == word[k][r])
                {
                  b[r] = word[k][r];
                  temp[r] = word[k][r];
                  temp[length] = '\0';
    
                  printf("%c", b[r]);
                }
            }
        }
    
      if(strcmp(temp, word[k]) != 0)
        {
          printf("\nSorry, no more turns left. The secret word was %s.\n",
    word[k]);
        }
      else
        {
          printf("\nCongratulations!\nYou guessed the secret word:  %s\n",
    word[k]);
        }
    
      return 0;
    
    }
    
    int main()
    {
    
        printf("                      Welcome to HANGMAN\n\n");
    
        printf("You will be asked to guess the computer's secret word.\n");
        printf("The word will be displayed as a number of *'s.  Every
    time\n");
        printf("you guess a letter correctly, that letter will be shown in
    its\n");
        printf("correct position in the word.  If you guess incorrectly,
    the\n");
        printf("number of tries you have left will be decremented.  You
    will be\n");
        printf("given a maximum of 7 incorrect guesses.\n\n");
    
        hangman();
    
     return 0;
    
    }
    So, my issue, is that I need to display the current status of the
    player's guesses in a format like ***.. (where * = strlen) and have it
    update per guess by replacing. So if someone guesses a and it's in the
    word twice it'd be like *..........a.. and so on. I also don't know what I'm
    doing wrong with the guess number. If someone guesses right I don't
    want guess number to change, but if wrong, just subtract one..this
    isn't working either.
    --
    growl.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Before I get into answering your questions, I'm gonna go over a bad programming technique that is common among new programmers.

    A program should never look like this:

    Code:
    void game();
    
    int main() {
        printf("This is my game. \n");
        game();
    
        return 0;
        }
    
    void game() {
        // The entire program
        // is written here
    
       return;
       }
    Functions are not meant to handle the entire program. They're meant to handle your calculations, algorithms, and stuff that's repeated alot. Unless you're planning to add multiple games to this program and have main as a menu to access these games, there is no reason to have the hold game in a function. Another peculiar thing you did with your function was make it an int and call it in the middle of nowhere in your program, which basically makes your return value fly off into the abyss. If you happened to be thinking, "Well, main has a return value that means nothing to me", then you should probably know that the operating system uses(sometimes) that return value to check if the program exited successfully. If you wanted to do the same with your function, you'd need to put the return value in an if statement or something similar.

    Now onto handling your questions...

    Aside from the obvious difficulties of updating different parts of the screen, this is not a very difficult problem. Simply have two strings. One is the correct word and one is the same strlen() of that word just filled with '*'. Basically it's like this. You get your random word, you get the string length of that word and you fill in your starred character array. Then, as all stars you output that to the screen. When your player guesses, you check your string and get the arrays indexes that have that letter. Then with that information, you go to your star string, and replace those same indexes with the guess. Then you reprint this to the screen.

    As for the guess number, I'm not quite sure what you're doing in your code with the dummy variable and setting the integer turn to the length, but that wouldn't word. You should have two simple cases, one if it's right, one if it's wrong. If it's right, you do as I said and replace the stars with the indexes, if it's wrong you reduce the number of turns, do whatever you need to do to your graphical hangman(if you have one), and you output the guess into the wrong letters area.
    Last edited by SlyMaelstrom; 11-28-2005 at 03:25 AM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help using arrays ( simple hangman game)
    By skymoha in forum C Programming
    Replies: 2
    Last Post: 03-04-2009, 04:52 AM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 04:18 PM
  3. !!!Urgent Help on a group project!!!!!
    By AmeenR in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 09:22 PM
  4. New game: Hangman!
    By abrege in forum Game Programming
    Replies: 6
    Last Post: 12-05-2002, 02:05 PM
  5. hangman game, copied staight from book.. 31 errors?!?!?!?!
    By Blizzarddog in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2002, 01:20 AM

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