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??