Thread: please help guys!

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    please help guys!

    by the way, i really admidre good c programmers becuase i have just started a few weeks ago, and it is really tough, i have never had to think about things so much in my life, and i was top of my class in school. Congratulations to you. Anyway, I really need help please. Im writing a hangman game, I know I know you heard it all before, but if you could please help me. I have tried to add tags for most important parts. I need a 2 player game where you select level of difficulty then enter a word, then the other guy tried to guess the word. He has to guess in the correct number of goes or he looses. What i want really is a few little things cos i got most of it. I want * to be displayed for the letters the player hasnt guessed and if possible a picture of the man being hanged as the player gets wrong guesses. It has to be C not C++. Thanks a lot, also if possible a function to allow the player to have a guess at the whole word in one go if he thinks he knows the word. Here is what I have.

    Code:
    #include <stdio.h>        /*This is where standard C functions are defined*/
    #include <conio.h>        /*This is where functions such as Getch are defined*/
    #include <string.h>       /*This is where the string handling functions are defined*/
    
    int mainMenu();            /*holds functions for the main menu*/
    int inpt_word();           /*variable that holds the word entered by players*/
    int gs_word();             /*holds functions whilst player is guessing the letters*/
    int win();                 /*variable if player guesses word correct*/
    int loose();               /*variable if player looses game*/
    
    int lives;                 /*variable that holds number of remaining lives*/
    int max_chars;             /*varibale that holds the max ammount of characters in word*/
    char word[9];              /*variable holds the word which is to be guessed*/
    
    int main(void)
    {
    	mainMenu();
    
       getch();                /*Waits for player to enter any character to continue to the next screen*/
       return 0;
    }
    
    mainMenu(void)
    {
    	int option;             /*variable that holds the number for which case is to be used*/
    
       clrscr();
    
    	printf("\nHangman : Main Menu\n\n");
    	printf("Select difficulty\n\n");
    	printf("1 : Easy - 9 Lives,Max 9 Letter-count\n");
       printf("2 : Medium - 8 Lives,Max 7 Letter count\n");
       printf("3 : Hard - 7 Lives,Max 8 Letter-count\n\n");
    
     	printf("Enter option : ");  /*The player has to enter a level of difficulty here*/
       scanf("%d",&option);        /*This stores the level entered*/
    
       switch(option)
       {
       	case 1 : lives = 9; max_chars =9; inpt_word(); break;   /*The boundaries of case1*/
          case 2 : lives = 8; max_chars =7; inpt_word();break;    /*The boundaries of case2*/
          case 3 : lives = 7; max_chars =8; inpt_word();break;    /*The boundaries of case3*/
          default : printf("Incorrect Input"); break;            /*What happens if an incorrect value is entered*/
       }
    
       return 0;
    }
    
    int inpt_word(void)
    {
    	clrscr();
    
       printf("\nHangman : Word Input\n\n");
       
       printf("Enter Word (maximum %d letters,it is case-sensetive) : ",max_chars);
       scanf("%s",&word);
    	printf("\nWord = %s\n",word);
    
       printf("Press any key to continue...");
    
       getch();
    
       gs_word();
    
       return 0;               /*The value zero is returned becuase the function has to value to return*/
    }
    
    int gs_word(void)
    {
       char letter;            /*The current letter guessed by the player*/
       char wordGuess[9];      
       int counter;            /*Stores the number of of * to display*/
       int arrayDest;
       int wordLength;         /*Number of characters is the word being guessed*/
       int charsMatched;       /*The number of characters matched*/
       int previousMatched;
    
       charsMatched = 0;
       counter = 0;
       wordLength = strlen(word);
    
       if(counter < wordLength)
       {
       	wordGuess[counter]='*';
    
          counter++;
       }
    
       while(lives > 0)
       {
    		clrscr();
    
          arrayDest = 0;
    		previousMatched = charsMatched;
    
       	printf("\nHangman : Word Guessing\n\n");
    
          printf("Lives remaining : %d\n\n",lives);
    
          printf("Word : %s\n\n",wordGuess);
    
       	printf("Enter a letter : ");
          scanf("%c",&letter);
       	scanf("%c",&letter);
    
          while(arrayDest < max_chars)
          {
       		if(letter==word[arrayDest])
             {
                wordGuess[arrayDest]=(letter);
    
                charsMatched++;
          	}
    
             arrayDest++;
          }
    
          if(charsMatched == previousMatched)
          {
           	lives--;
          }
    
          if(charsMatched == wordLength)
          {
          	win();
          }
       }
    
       loose();
    
       return 0;
    }
    
    win(void)
    {
    	clrscr();
    
    	printf("\nHangman\n\n");
    
    	printf("You Win!");
    
       getch();
    
       return 0;
    }
    
    loose(void)
    {
    	clrscr();
    
    	printf("\nHangman\n\n");
    
       printf("Word = %s",word);
    
    	printf("You Loose!\n\n");
    	printf("tard");
    
       getch();
    
       return 0;
    }
    Thank you

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You are on the right track, but I don't know where you want to go after mMainMenu() is run.
    On your question as to what word to run. If they enter more than maxcharacters then the extra characters will go into the buffer. Very badLook here

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You didn't really pose a question, but I'll make a few points:
    1) Lose the global variables.
    2) See the first word in the above sentence? Shouldn't you at least spell correctly in a game of Hang Man:
    Code:
    int loose();               /*variable if player looses game*/
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    ok thanks for the adive, the only serious thing i need now is How do I give and option after the player has either won or lost, to press N for a new game?? pls could you show me the wxact come and how i should do this? thanks. Also about you point with the player entering more than Max_char, i have put an IF statement there so if the word enter is more than Max_char then i say in a printf statement, so would it be a good idea to load the screen where the user enters the word to be guessed again so he can enter a word which is within the max_char length?? If its a good idea, how would i do this?? this is the same sort of thing as asking the player if he wants to play again i think, is it??? You see, im new and it some problems sort of "hurt my brain" haha. thanks

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main( void )
    {
        char choice = 0;
    
        while( choice != 'N' && choice != 'n' )
        {
            mainMenu( );
    
            printf("Play again? [Y/N]: ");
            scanf("%c", &choice ); /* since you're already using scanf... */
        }
        return 0;
    }
    Something like that would work. Naturally this doesn't do any error checking or the like. I'll leave that as an exercise to the reader.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Thanks a lot, it works great except for 1 little problem. Could you please give me one last help. The program only ask if I want to play again after I have lost, it doesnt appear when the player has won, I think its because of the way it works its way down the code in order so the program isnt terminated till it works it way through the Loose part. So how do I get it to ask if i want to play again after the player has won?? Thanks you very very much.

    Code:
    #include <stdio.h>        /*This is where standard C functions are defined*/
    #include <conio.h>        /*This is where functions such as Getch are defined*/
    #include <string.h>       /*This is where the string handling functions are defined*/
    
    int mainMenu();            /*holds functions for the main menu*/
    int inpt_word();           /*variable that holds the word entered by players*/
    int gs_word();             /*holds functions whilst player is guessing the letters*/
    int win();                 /*variable if player guesses word correct*/
    int loose();               /*variable if player looses game*/
    int wordLength;
    int lives;                 /*variable that holds number of remaining lives*/
    int max_chars;             /*varibale that holds the max ammount of characters in word*/
    char word[9];              /*variable holds the word which is to be guessed*/
    
    int main(void)
    {
    char choice=0;
    
    while( choice!= 'N' &&choice!='n')
    
    	{
    	mainMenu();
    
       printf("Play again? [Y/N]: ");
       scanf("%c", &choice );
    
       getch();                /*Waits for player to enter any character to continue to the next screen*/
       }
       return 0;
    }
    
    mainMenu(void)
    {
    	int option;             /*variable that holds the number for which case is to be used*/
    
       clrscr();               /*Clears the screen*/
    
    	printf("\nHangman : Main Menu\n\n");
    	printf("Select difficulty\n\n");
    	printf("1 : Easy - 9 Lives,Max 9 Letter-count\n");
       printf("2 : Medium - 8 Lives,Max 7 Letter count\n");
       printf("3 : Hard - 7 Lives,Max 8 Letter-count\n\n");
    
     	printf("Enter option : ");  /*The player has to enter a level of difficulty here*/
       scanf("%d",&option);        /*This stores the level entered*/
    
       switch(option)
       {
       	case 1 : lives = 9; max_chars =9; inpt_word(); break;   /*The boundaries of case1*/
          case 2 : lives = 8; max_chars =7; inpt_word();break;    /*The boundaries of case2*/
          case 3 : lives = 7; max_chars =8; inpt_word();break;    /*The boundaries of case3*/
          default : printf("Incorrect Input"); break;            /*What happens if an incorrect value is entered*/
       }
    
       return 0;
    }
    
    int inpt_word(void)
    {
    	clrscr();
    
       printf("\nHangman : Word Input\n\n");
    
       printf("Enter Word (maximum %d letters,it is case-sensetive) : ",max_chars);  /*The word to be guessed is entered here*/
       scanf("%s",&word);
       wordLength = strlen(word);
       if (wordLength > max_chars)
       {
       printf("You have entered too many characters");
       }
    
    	printf("\nWord = %s\n",word);     /*The word entered is displayed here for the person to check if enter correctly*/
    	printf("Press any key to continue...");
    
       getch();
    
       gs_word();
    
       return 0;               /*The value zero is returned becuase the function has to value to return*/
    }
    
    int gs_word(void)
    {
       char letter;            /*The current letter guessed by the player*/
       char wordGuess[9];      /*The word that is to be guessed*/
       int counter;            /*Helps to work out number of * to display*/
       int array_pos;          /*Gives a position to each of the characters in the entered word*/
       int wordLength;         /*Number of characters is the word being guessed*/
       int mch_char;           /*The number of characters matched*/
       int pvs_mch;    /*Has the character entered been entered before*/
    
       mch_char = 0;
       counter = 0;
       wordLength = strlen(word);     /*Calculates length of word being guessed*/
    
       if(counter < wordLength)
       {
       	wordGuess[counter]='*';     /*A * is displayed if the IF statement is still true*/
    
          counter++;
       }
    
       while(lives > 0)        /*Run the loop whilst the player has lives left*/
       {
    		clrscr();
    
          array_pos = 0;
    		pvs_mch = mch_char;
    		printf("\nHangman : Word Guessing\n\n");
    		printf("Lives remaining : %d\n\n",lives);
    		printf("Word : %s\n\n",wordGuess);
    		printf("Enter a letter : ");
          scanf("%c",&letter);
    
    
          while(array_pos < max_chars)     /*If all the characters have not been revealed, the loop still runs*/
          {
       		if(letter==word[array_pos])   /*It the letter entered is in the word, place the letter in the same destination as in the array*/
             {
                wordGuess[array_pos]=(letter);
    				mch_char++;                /*Number of matched characters goes up bu 1*/
          	}
    
             array_pos++;
          }
    
          if(mch_char == pvs_mch)
          {
           	lives--;
          }
    
          if(mch_char == wordLength)     /*If player has matched same number of characters as word length then they Win*/
          {
          	win();
          }
       }
    
       loose();
    
       return 0;
    }
    
    win(void)
    {
    	int foreground,background;      /*Variables for the colour of text and background*/
    
       for(foreground=0;foreground<=6;foreground++)   /*The colour of text changes each time the loop runs*/
       {
       	for(background=0;background<=6;background++)  /*The colour of background changes each time the loop runs*/
          {
          	textcolor(foreground);
             textbackground(background);
             cprintf("YOU WIN!");
          }
          
       }
       getch();
    
       return 0;
    }
    
    loose(void)
    {
    	clrscr();
    
    	printf("\nHangman : Loose\n\n");
    
       printf("The word you were looking for was : %s",word);
    
    	printf("Bad Luck, You Loose!\n\n");
    
       getch();
    
       return 0;
    }
    Also, what do you think about the structure of my programming?? is it ok or poor??

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Your loop continues after a win until lives = 0, then a lose message is given.
    Originally posted by Owain1602
    Code:
       while(lives > 0)        /*Run the loop whilst the player has lives left*/
       {
    		clrscr();
    
          array_pos = 0;
    		pvs_mch = mch_char;
    		printf("\nHangman : Word Guessing\n\n");
    		printf("Lives remaining : %d\n\n",lives);
    		printf("Word : %s\n\n",wordGuess);
    		printf("Enter a letter : ");
          scanf("%c",&letter);
    
    
          while(array_pos < max_chars)     /*If all the characters have not been revealed, the loop still runs*/
          {
       		if(letter==word[array_pos])   /*It the letter entered is in the word, place the letter in the same destination as in the array*/
             {
                wordGuess[array_pos]=(letter);
    				mch_char++;                /*Number of matched characters goes up bu 1*/
          	}
    
             array_pos++;
          }
    
          if(mch_char == pvs_mch)
          {
           	lives--;
          }
    
          if(mch_char == wordLength)     /*If player has matched same number of characters as word length then they Win*/
          {
          	win();
    Don't you want to exit the while loop on a win instead of running the next life?
          }
       }
    
       loose();
    Since this is outside the lives loop, you will *always* execute this function, even on a win
    Also, what do you think about the structure of my programming?? is it ok or poor??
    Your formatting is getting in the way of seeing the structure. You need to decide to used spaces OR tabs, not both. I'd suggest removing all tabs and indent with spaces. Just look at the post. Ragged indenting makes it hard to read.

    Also, your lines are too long. It is OK to break lines in two so you don't have 200 chars on a line. You can even put the comments on the line above if you need to. Notice how far you have to scroll horizontally just to read your comments? That's not good IMHO.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Also, what do you think about the structure of my programming?? is it ok or poor??
    The intendenation in your code is messed up and annoying. I think you should keep the general number of comments, but maybe scrunch them down a little, or atleast space the comments closer to the code, but yet so they also are all aligned correctly indentation-wise(every comment for that block of code starts in the same column).

    For getting help purposes, it takes a vetran programmer a tad longer to thrift through unorganized code. With it being organized good, they generally know where to go in your code a bit quicker.

    I was a little, "...ehhh..ahh, get it off me!" while reading your code.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 05:47 PM
  2. How long have you guys been working with C/C++??
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 08-01-2003, 03:41 PM
  3. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM
  4. hello guys
    By lupi in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:42 AM
  5. hello guys
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 01:00 AM