Need help with c program in eclipse Hangman The game of hangman is a classic pap
ID: 3569973 • Letter: N
Question
Need help with c program in eclipse
Hangman
The game of hangman is a classic paper and pencil game which requires 2 or more players. One player (the executioner), Draws the gallows and selects a word. He puts down a series of underlines to indicate the length of the word.
The other player/s in turn try to guess the word by suggesting a letter.
If the guess is correct the executioner writes the letter over the appropriate underlines. If the guess is wrong, the executioner adds the letter to a list of wrong guesses, and adds a
Explanation / Answer
#include <stdio.h>
char incorrectletterguessed[20];
char word [100];
char correctletterguessed [100];
char stars [100];
int isLetterInWord ( char a, char* word );
int isLetterInWord ( char a, char* word )
{
int i = 0;
while ( word [ i ] != '' )
{
if ( word [ i ] == a )
return 1;
i++;
}
return 0;
}
int showGuessed ( char*, char*);
int showGuessed ( char* word, char* correctletterguessed)
{
/*
printf
(
"showGuessed ( %s, %s ) ",
word,
correctletterguessed
);
*/
/*
* return 1 if printing complete word, didn't print any star
* return 0 if printing a star
* stars : ***********
* word : linuxmint
* correctletterguessed : li
* result : print li****i**
*/
int i = 0;
int length = 0;
while ( word [ i ] != '' )
{
stars [ i ] = '_';
i++;
length++;
}
stars [ i ] = '';
//printf ( "stars before : %s ", stars );
i= 0;
while ( correctletterguessed [ i ] != '' )
{
//printf ("checking %c ", correctletterguessed [ i ]);
int j=0;
while ( word [ j ] != '' )
{
if ( word [ j ] == correctletterguessed [ i ] )
//printf ("[%c]", word [ j ] );
stars [ j ] = correctletterguessed [ i ];
j++;
}
i++;
}
//printf ( "current word : %s ", stars );
i = 0;
while ( stars [ i ] != '' )
{
if (stars [ i ] == '_' )
return 0;
i++;
}
return 1;
}
void showMistakeStatus (int);
void showMistakeStatus (int mistake)
{
if (mistake == 0)
{
printf (" +------+ ");
printf (" | | ");
printf (" | ");
printf (" | ");
printf (" | ");
printf (" | ");
printf (" ============ ");
printf (" The Word : %s ",stars);
printf (" Letters Tried : %s ",incorrectletterguessed);
}
else if (mistake == 1)
{
printf (" +------+ ");
printf (" | | ");
printf (" | o ");
printf (" | ");
printf (" | ");
printf (" | ");
printf (" ============ ");
printf (" The Word : %s ",stars);
printf (" Letters Tried : %s ",incorrectletterguessed);
}
else if (mistake == 2)
{
printf (" +------+ ");
printf (" | | ");
printf (" | o ");
printf (" | | ");
printf (" | ");
printf (" | ");
printf (" ============ ");
printf (" The Word : %s ",stars);
printf (" Letters Tried : %s ",incorrectletterguessed);
}
else if (mistake == 3)
{
printf (" +------+ ");
printf (" | | ");
printf (" | o ");
printf (" | /| ");
printf (" | ");
printf (" | ");
printf (" ============ ");
printf (" The Word : %s ",stars);
printf (" Letters Tried : %s ",incorrectletterguessed);
}
else if (mistake == 4)
{
printf (" +------+ ");
printf (" | | ");
printf (" | o ");
printf (" | /|\ ");
printf (" | ");
printf (" | ");
printf (" ============ ");
printf (" The Word : %s ",stars);
printf (" Letters Tried : %s ",incorrectletterguessed);
}
else if (mistake == 5)
{
printf (" +------+ ");
printf (" | | ");
printf (" | o ");
printf (" | /|\ ");
printf (" | / ");
printf (" | ");
printf (" ============ ");
printf (" The Word : %s ",stars);
printf (" Letters Tried : %s ",incorrectletterguessed);
}
else if (mistake == 6)
{
printf (" +------+ ");
printf (" | | ");
printf (" | o ");
printf (" | /|\ ");
printf (" | / \ ");
printf (" | ");
printf (" ============ ");
printf (" The Word : %s ",stars);
printf (" Letters Tried : %s ",incorrectletterguessed);
}
}
int main ()
{
char letter = '';
while(1){
printf ("Do you want to play hangman game?");
scanf(" %c",&letter);
if ((letter=='y')||(letter=='Y')){
incorrectletterguessed[0]='';
word[0]='';
correctletterguessed[0]='';
stars[0]='';
printf ("enter a word to be guessed : ");
scanf ("%s", word );
getchar ();
int correctletterguessed_ctr = 0;
int mistake = 0;
int mistakel=0;
char letter = '';
int allStarsGone = 0;
int i;
for(i=0;word [i] != '' ;i++)
stars [i] = '_';
stars [i] = '';
while ( allStarsGone == 0 && mistake < 6 )
{
printf ("enter a letter : ");
scanf ( " %c", &letter );
if
(
! isLetterInWord ( letter, correctletterguessed ) &&
isLetterInWord ( letter, word )
)
{
correctletterguessed [ correctletterguessed_ctr ] =
letter;
correctletterguessed_ctr++;
correctletterguessed [ correctletterguessed_ctr ] =
'';
allStarsGone =
showGuessed ( word, correctletterguessed );
showMistakeStatus ( mistake );
}
else
{
incorrectletterguessed[mistakel++]=letter;
incorrectletterguessed[mistakel++]=' ';
incorrectletterguessed[mistakel]='';
mistake++;
showMistakeStatus ( mistake );
}
}
if ( mistake >= 6 )
printf ("game over answer! You are hung! ", word );
else
printf ("congratulation ");
}
else {
break;
}
}
return 0;
}