#include<conio.h> This is a hangman game. Can you help me make 8 functions out o
ID: 3713817 • Letter: #
Question
#include<conio.h>
This is a hangman game. Can you help me make 8 functions out of this also make us it compiles good.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<dos.h>
//#define MAXCHAR=100
char inputdata();
char userdec();
int main()
{
FILE *fp;
int MAXCHAR=100,i,len,wrongTry=6,matchFound=0,counter=0,position=0,winner;
char gussed[100],ch,progress[100],letters[100],guess[100],tempWord[100];
char alphabetFromUser,dec;
char* filename = "test3.txt";
clrscr();
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Could not open file %s",filename);
return 1;
}
while (fgets(gussed, MAXCHAR, fp) != NULL)
//printf("%s",gussed);
fclose(fp);
fflush(stdin);
len=0;
len=strlen(gussed);
//while ( gussed[i]!= NULL )
//{
// len=len+1;
//}
x:
printf(" The word has %d alphabets ",len); /**tell the user how many alphabets the word has**/
for(i = 0 ; i < len ; i++)
{
progress[i]='*';
}
for(i = 0 ; i < len ; i++)
{
printf(" ");
printf("%c",progress[i]); /**Show the Word With n(length of the original word) number of underscores (_)**/
}
while(wrongTry != 0) /**while loop for exiting the program when no try left**/
{
matchFound = 0;
alphabetFromUser=inputdata(); /**function for get alphabet from user**/
if(alphabetFromUser < 'a' || alphabetFromUser >'z') /**In case player gives input other than 'a' to 'z' the console will ask again**/
{
system("cls");
printf(" Wrong input TRY AGAIN ");
matchFound = 2;
}
fflush(stdin);
if (matchFound != 2)
{
for(counter=0;counter<len;counter++) /**dfor loop to check whether player input alphabet exists or not in the word**/
{
if(alphabetFromUser==gussed[counter])
{
matchFound = 1;
}//end of if()
}//end of for()
if(matchFound == 0) /**in case of wrong guess**/
{
printf(" :( You have %d tries left ",--wrongTry);
getch();
//showHangman(wrongTry);
//getchar();
}//end of if()
else
{
for(counter = 0; counter < len; counter++)
{
matchFound = 0;
if(alphabetFromUser == gussed[counter])
{
position = counter ;
matchFound = 1;
}//end of if
if(matchFound == 1)
{
for(i = 0 ; i < len ; i++)
{
if( i == position)
{
progress[i] = alphabetFromUser; /**Put the alphabet at right position**/
}
else if( progress[i] >= 'a' && progress[i] <= 'z' ) /** If the position already occupied
by same alphabet then no need to fill again EASY!! and continue **/
{
continue;
}
else
{
progress[i] = '*'; /** Put a blank at not guessed alphabet position **/
}
}
tempWord[position] = alphabetFromUser; /**put the alphabet in another char array to check with the original word**/
tempWord[len] = ''; /**put the NULL character at the end of the temp string**/
winner = strcmp(tempWord,gussed); /**upon True comparison it will return 0**/
if(winner == 0) /**if the player guessed the whole word right then he/she is the WINNER**/
{
printf(" YAHOO!!!!! You are the WINNER !!!!!");
printf(" The Word was %s ",gussed);
printf(" EASY HUH??? ");
dec=userdec();
if(dec=='y')
goto x;
else
return 0;
}//end of inner if
}//end of outer if
}//end of for loop
}//end of else
}// end of if(matchFound != 2) condition
printf(" ");
for(i = 0 ; i < len ; i++)
{
printf(" ");
printf("%c",progress[i]); /**Show the original Word With blanks and right Input alphabet**/
}
getch();
}//end of while loop
if(wrongTry <= 0) /**if the player can not guess the whole word in 5 chaces**/
{
printf(" The Word was %s ",gussed);
printf(" Better luck next!!!");
}
getch();
return 0;
}
char inputdata()
{
char c;
printf(" enter any alphabet from a to z and please use small case!!");
printf(" Enter HERE ==> ");
fflush(stdin);
scanf("%c",&c);
return c;
}
char userdec()
{
char c;
printf("Do you want to play again?y/n");
scanf("%c",&c);
return c;
}
Explanation / Answer
#include<stdio.h> /*Header file declaration*/
#include<string.h> /*<string.h> for strcmp();,strlen(); functions use*/
#include<stdlib.h>
void showHangman(int);
int main(void)
{
char hangmanWord[100], tempWord[100]; /**hangmanWord[] array for the original word and tempWord[] array to get the alphabet from user and compare it with original word**/
char hangmanOutput[100]; /**This array will show the remaining blanks and correct inputs**/
int wrongTry = 6 , matchFound = 0; /**player will get 5 chance, so we use wrongTry as chance counter**/
/**matchFound to search the alphabet, if the alphabet from user does not exist
in the original word it will remain 0, upon finding the word, matchFound will
be set as 1**/
int counter = 0 , position = 0, winner, length , i;
char alphabetFromUser;
system("cls"); /**for clearing the screen**/
printf(" Enter any word in small case and hit >>ENTER<<");
printf(" Enter HERE ==> ");
scanf("%s",hangmanWord); /**get the string from opponent**/
printf(" Now give the COMPUTER to your friend and see if he/she can CRACK it!!!");
printf(" HIT >>ENTER<<");
getchar(); /**hold the computer screen**/
length = strlen(hangmanWord); /**get the length of the word**/
system("cls");
printf(" !!!!!!!!!!!!!!!!!!!Welcome to the HANGMAN GAME!!!!!!!!!!!!!!!!! "); /**Brief description of the game**/
printf(" You will get 5 chances to guess the right word");
printf(" So help the Man and get...set...GO..!!");
getchar();
printf(" HIT >>ENTER<< ");
getchar();
system("cls");
printf(" ||===== "); /**show the HANGMAN**/
printf(" || | ");
printf(" || ");
printf(" || ");
printf(" || ");
printf(" || ");
printf(" The word has %d alphabets ",length); /**tell the user how many alphabets the word has**/
for( i = 0; i < length ; i++)
{
hangmanOutput[i] = '_';
hangmanOutput[length] = '';
}
for(i = 0 ; i < length ; i++)
{
printf(" ");
printf("%c",hangmanOutput[i]); /**Show the Word With n(length of the original word) number of underscores (_)**/
}
while(wrongTry != 0) /**while loop for exiting the program when no try left**/
{
matchFound = 0;
printf(" enter any alphabet from a to z and please use small case!!");
printf(" Enter HERE ==> ");
fflush(stdin);
scanf("%c",&alphabetFromUser); /**get alphabet from user**/
if(alphabetFromUser < 'a' || alphabetFromUser > 'z') /**In case player gives input other than 'a' to 'z' the console will ask again**/
{
system("cls");
printf(" Wrong input TRY AGAIN ");
matchFound = 2;
}
fflush(stdin);
if (matchFound != 2)
{
for(counter=0;counter<length;counter++) /**for loop to check whether player input alphabet exists or not in the word**/
{
if(alphabetFromUser==hangmanWord[counter])
{
matchFound = 1;
}//end of if()
}//end of for()
if(matchFound == 0) /**in case of wrong guess**/
{
printf(" :( You have %d tries left ",--wrongTry);
getchar();
showHangman(wrongTry);
getchar();
}//end of if()
else
{
for(counter = 0; counter < length; counter++)
{
matchFound = 0;
if(alphabetFromUser == hangmanWord[counter])
{
position = counter ;
matchFound = 1;
}//end of if
if(matchFound == 1)
{
for(i = 0 ; i < length ; i++)
{
if( i == position)
{
hangmanOutput[i] = alphabetFromUser; /**Put the alphabet at right position**/
}
else if( hangmanOutput[i] >= 'a' && hangmanOutput[i] <= 'z' ) /** If the position already occupied
by same alphabet then no need to
fill again EASY!! and continue */
{
continue;
}
else
{
hangmanOutput[i] = '_'; /** Put a blank at not guessed alphabet position **/
}
}
tempWord[position] = alphabetFromUser; /**put the alphabet in another char array to check with the original word**/
tempWord[length] = ''; /**put the NULL character at the end of the temp string**/
winner = strcmp(tempWord,hangmanWord); /**upon True comparison it will return 0**/
if(winner == 0) /**if the player guessed the whole word right then he/she is the WINNER**/
{
printf(" YAHOO!!!!! You are the WINNER !!!!!");
printf(" The Word was %s ",hangmanWord);
printf(" EASY HUH??? ");
getchar();
return 0;
}//end of inner if
}//end of outer if
}//end of for loop
}//end of else
}// end of if(matchFound != 2) condition
printf(" ");
for(i = 0 ; i < length ; i++)
{
printf(" ");
printf("%c",hangmanOutput[i]); /**Show the original Word With blanks and right Input alphabet**/
}
getchar();
}//end of while loop
if(wrongTry <= 0) /**if the player can not guess the whole word in 5 chaces**/
{
printf(" The Word was %s ",hangmanWord);
printf(" The man is dead you IDIOT!!!!!");
printf(" Better luck next!!!");
}
getchar();
return 0;
}//end of main();
void showHangman(int choice) /**This function show the hangman after each wrong try**/
{
switch(choice)
{
case 0:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || | ");
printf(" || / %c",'\');
printf(" || ");
break;
case 1:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || | ");
printf(" || %c",'\');
printf(" || ");
break;
case 2:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || | ");
printf(" || ");
printf(" || ");
break;
case 3:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || ");
printf(" || ");
printf(" || ");
break;
case 4:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO ",'\');
printf(" || ");
printf(" || ");
printf(" || ");
break;
case 5:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || O ");
printf(" || ");
printf(" || ");
printf(" || ");
break;
}//end of switch-case
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.