#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0

    Need advice on small Hangman game.


    I'm very new to C, sorry if I make you cringe at how inefficient my coding is, but I'm just trying to make a small hangman game. It does work, but when the player answers yes to "Do you want to try again?" the game does loop back to the start, but nothing works properly. Can anyone help me with this ?

    It is actually indented, but when I put it into this post it removed the indents, and I don't have time to fix it.

    Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <time.h>
        #include <string.h>
         
        #define WORD_DATABASE 20
        #define WORD_DATABASE_LETTERS 28
         
        enum Status {CONTINUE, END, RUN};
         
        int main( void )
        {
         
        enum Status gameStatus;
        gameStatus=RUN;
         
        while(gameStatus==RUN) //So the game can loop again if the player wants to continue.
        {
        srand( time( NULL ) );
        int lives, i, total_guesses, noLetterCheck, word_generate;
        char word_database[WORD_DATABASE][WORD_DATABASE_LETTERS] = {"d e f i n e", "s e t t l e m e n t", "l i t t e r", "o p e n i n g", "b a c k y a r d", "c o a s t", "p o p u l a t i o n", "b r a i n s t o r m i n g", "s k y l i n e", "j a z z", "s e a s o n s", "s w e e t", "a n t i b i o t i c", "p l a y f u l", "s t o c k p i l e", "p i l l a r", "b i o l u m i n e s c e n t", "r e l i a b l e", "j a c k e t", "f r o s t"};
        char word_hidden[WORD_DATABASE][WORD_DATABASE_LETTERS] = {"_ _ _ _ _ _", "_ _ _ _ _ _ _ _ _ _", "_ _ _ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _ _ _ _ _", "_ _ _ _ _", "_ _ _ _ _ _ _ _ _ _", "_ _ _ _ _ _ _ _ _ _ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _ _", "_ _ _ _ _ _ _ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _ _ _ _ _ _", "_ _ _ _ _ _", "_ _ _ _ _ _ _ _ _ _ _ _ _ _", "_ _ _ _ _ _ _ _", "_ _ _ _ _ _", "_ _ _ _ _"};
        char word_letterGuess[2];
        char word_letterUsed[27][2] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
        char empty, playAgain;
         
        empty = ' ';
        noLetterCheck=0;
         
        word_generate= 0 + ( rand() % 19 ); //Choose word from database
         
         
        gameStatus=CONTINUE;
         
         
        lives=6; //Initialise lives
         
        total_guesses=0;
         
        i=0;
         
        printf("////////////////////////////////////////////////////////////////////////////////"
        "/ /"
        "/ HANGMAN /"
        "/ Type 0 to end the game. /"
        "/ /"
        "////////////////////////////////////////////////////////////////////////////////");
         
        while(gameStatus==CONTINUE)
        {
        if(lives>0)//To stop game from continuing if lives = 0.
        {
        noLetterCheck=0;
        printf("\n\n%30cWord: %s\n%36cLives:%d", empty, word_hidden[word_generate], empty, lives);
        if(total_guesses >= 1)
        {
        printf("\n%14cLetters guessed: ", empty);
        for(i=0;i<total_guesses;i++)
        {
        printf("%c, ", word_letterUsed[i][0]);
        }//end for
        }//end if
        else
        {
        total_guesses=0;
        }
        printf("\n%15cGuess a letter>", empty);
        scanf("%c", &word_letterGuess[0]);
        getchar();
         
        word_letterUsed[total_guesses][0]=word_letterGuess[0];
         
        total_guesses++;
        }//end if
         
        if(lives>0)
        {
        if(word_letterGuess[0] == '0')
        {
        printf("\n%35cGame over.\n\n", empty);
        getchar();
        printf("////////////////////////////////////////////////////////////////////////////////"
        "/ /"
        "/ Thanks for playing ! /"
        "/ /"
        "////////////////////////////////////////////////////////////////////////////////");
        getchar();
        return 0;
        }//end if
        for(i=0;i<strlen(word_database[word_generate]);i++)//Checking word for matches to guessed letter and revealing letters in hidden word.
        {
        if(word_database[word_generate][i]==word_letterGuess[0])
        {
        word_hidden[word_generate][i]=word_letterGuess[0];
        }//end if
        else//Counter to check if no letters match te guessed letter.
        {
        noLetterCheck++;
        }
         
        if(noLetterCheck == strlen(word_database[word_generate]))//Action to execute if no letters match the guessed letter.
        {
        lives--;
        }
        }//end for
        for(i=1;i<=strlen(word_database[word_generate]);i++)//Checking if word is complete.
        {
        if(word_hidden[word_generate][i]==word_database[word_generate][i])
        {
        if(i==strlen(word_database[word_generate]))
        {
        printf("\n%29cYou guessed the word !", empty);
        printf("\n%29cThe word was: ", empty);
        for(i=0;i<strlen(word_database[word_generate]);i=(i+2))//Printing word without spaces.
        {
        printf("%c", word_database[word_generate][i]);
        }//end for
        getchar();
        printf("\n%25cWould you like to try again ?\n%37cY/N:", empty, empty);
        playAgain=getchar();
         
        switch(playAgain)//Prompt user to decide to play again or not.
        {
        case 'Y':
        case 'y':
        gameStatus=RUN;
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        break;
         
        case 'N':
        case 'n':
        gameStatus=END;
        break;
         
        default:
        printf("\n%32cInvalid choice.\n%38c>", empty, empty);
        break;
        }//end playAgain switch
        }//end if
        }//end if
        else//If word is not complete.
        {
        break;
        }//end else
        }//end for (checking if word is complete)
        }//end if lives > 0
         
        else//If no lives remain.
        {
        gameStatus=END;
        printf("\n%16cNo lives remaining, you did not guess the word.", empty);
        printf("\n%21cThe word was: ", empty);
        for(i=0;i<strlen(word_database[word_generate]);i=(i+2))
        {
        printf("%c", word_database[word_generate][i]);
        }//end for
        getchar();
        printf("\n%25cWould you like to try again ?\n%37cY/N:", empty, empty);
        playAgain=getchar();
         
        switch(playAgain)
        {
        case 'Y':
        case 'y':
        gameStatus=RUN;
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");//To clear the screen for next game.
        break;
         
        case 'N':
        case 'n':
        gameStatus=END;
        break;
         
        default:
        printf("\n%32cInvalid choice.\n%38c>", empty, empty);
        break;
        }//end playAgain switch
        }//end else lives = 0
        }//end while
         
        if(gameStatus==END)
        {
        printf("\n%35cGame over.\n\n", empty);
        getchar();
        printf("////////////////////////////////////////////////////////////////////////////////"
        "/ /"
        "/ Thanks for playing ! /"
        "/ /"
        "////////////////////////////////////////////////////////////////////////////////");
        getchar();
        return 0;
         
        }//end if
        }//end while RUN
        }//end main()
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by Kamikageyami
    Here's a link to my code:
    snipt(dot)org/vgiac0

    I'm very new to C, sorry if I make you cringe at how inefficient my coding is, but I'm just trying to make a small hangman game. It does work, but when the player answers yes to "Do you want to try again?" the game does loop back to the start, but nothing works properly. Can anyone help me with this ?
    If anyone wants to actually try it to see what happens just let me know and I'll post a download link.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    By the description you provided (I haven't looked at the code: you may want to post it here rather than off site) I imagine your problem is with scanf()/getchar().

    Notice that when you press a command, for example "yes", you finish the command with an ENTER. The scanf("%s") ignores that ENTER, but getchar() reads it.

    I suggest you always use fgets() for user input.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by bdb
    By the description you provided (I haven't looked at the code: you may want to post it here rather than off site) I imagine your problem is with scanf()/getchar().

    Notice that when you press a command, for example "yes", you finish the command with an ENTER. The scanf("%s") ignores that ENTER, but getchar() reads it.

    I suggest you always use fgets() for user input.
    Thanks for the reply !

    I was using getchar(), but it still looped back to the start of the game. I'll try again with fgets() instead and see how it works. Thanks again.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,839
    Rep Power
    480
    indenting code takes little time (seconds, not hours) if you know how.

    In emacs:
    mark the region and type C-M-\
    (hold down control and alt keys then tap backslash)

    With a c pretty printer:
    $ bcpp <ugly.c >legible.c


    seed the rng just once. A computer could play your game 1000 times per second. Each game would be the same.


    bdb identified the problem. You need to suck up input through new line character following
    Code:
    	      printf("\n%25cWould you like to try again ?\n%37cY/N:", empty, empty);
    	      playAgain=getchar();
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by b49P23TIvg
    indenting code takes little time (seconds, not hours) if you know how.

    In emacs:
    mark the region and type C-M-\
    (hold down control and alt keys then tap backslash)

    With a c pretty printer:
    $ bcpp <ugly.c >legible.c


    seed the rng just once. A computer could play your game 1000 times per second. Each game would be the same.


    bdb identified the problem. You need to suck up input through new line character following
    Code:
    	      printf("\n%25cWould you like to try again ?\n%37cY/N:", empty, empty);
    	      playAgain=getchar();
    I don't understand what you mean o:
    I need to suck up input ?
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,839
    Rep Power
    480
    Code:
    printf("\n%25cWould you like to try again ?\n%37cY/N:", empty, empty);
    playAgain=getchar();
    while('\n' != getchar())   /* insert this line here */
      ; /* and this line here and your program will work a little better */
    better still, use fgets to read input and then sscanf or whatever to parse the input. (bdb's idea)
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    printf("\n%25cWould you like to try again ?\n%37cY/N:", empty, empty);
    playAgain=getchar();
    while('\n' != getchar())   /* insert this line here */
      ; /* and this line here and your program will work a little better */
    better still, use fgets to read input and then sscanf or whatever to parse the input. (bdb's idea)
    Thanks so much guys, it's working perfectly now.

IMN logo majestic logo threadwatch logo seochat tools logo