C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 25th, 2012, 09:37 AM
Kamikageyami Kamikageyami is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 16 Kamikageyami User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 52 m 53 sec
Reputation 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()

Reply With Quote
  #2  
Old October 25th, 2012, 10:06 AM
Kamikageyami Kamikageyami is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 16 Kamikageyami User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 52 m 53 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #3  
Old October 25th, 2012, 10:14 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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.

Reply With Quote
  #4  
Old October 25th, 2012, 11:19 AM
Kamikageyami Kamikageyami is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 16 Kamikageyami User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 52 m 53 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #5  
Old October 25th, 2012, 12:14 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,359 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 43 m 13 sec
Reputation Power: 383
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!

Reply With Quote
  #6  
Old October 25th, 2012, 05:08 PM
Kamikageyami Kamikageyami is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 16 Kamikageyami User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 52 m 53 sec
Reputation Power: 0
Quote:
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 ?

Reply With Quote
  #7  
Old October 25th, 2012, 08:28 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,359 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 43 m 13 sec
Reputation Power: 383
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)

Reply With Quote
  #8  
Old October 25th, 2012, 10:47 PM
Kamikageyami Kamikageyami is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 16 Kamikageyami User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 52 m 53 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need advice on small Hangman game.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap