I've worked a bit more on the program since a week ago, and I think
I've made some significant progress, but there're still a few things I
either don't understand how to do, or am doing wrong. With that said,
here's the new source:
Code:
int hangman()
{
char input;
char temp[12];
char word[][20] = {"literature","scholar", "enormous", "influence",
"publication", "pioneer", "reshape", "catalyst", "leader", "member",
"final", "phonograph", "executed",
"oldest", "people", "requiring", "screwdriver", "buckshot",
"different", "striking"};
srand((unsigned)time(NULL)); /* Provide a seed to rand() */
int k = (rand() % 20);
int i = 0;
int r = 0;
char b[]= "*******";
int length = strlen(word[k]);
int turn = 7;
int dummy = 0;
int decide = 0;
printf("\nSorry, no more turns left. The secret word was %s.\n",
word[k]);
for(i = 0; i < length; i++)
{
printf("%c", b[i]);
}
while(turn != 0)
{
printf("\nTurns left: ");
printf("%d", turn);
printf("\nEnter a letter to guess: ");
input = getchar();
while(getchar() != '\n');
for(r = 0; r < length; r++)
{
if(input == word[k][r] && b[r] == '*')
{
decide = 1;
turn = length;
}
else
{
decide = 0;
}
}
if(decide == 0)
{
dummy++;
turn = length - dummy;
}
for(r = 0; r < length; r++)
{
if(input == word[k][r])
{
b[r] = word[k][r];
temp[r] = word[k][r];
temp[length] = '\0';
printf("%c", b[r]);
}
}
}
if(strcmp(temp, word[k]) != 0)
{
printf("\nSorry, no more turns left. The secret word was %s.\n",
word[k]);
}
else
{
printf("\nCongratulations!\nYou guessed the secret word: %s\n",
word[k]);
}
return 0;
}
int main()
{
printf(" Welcome to HANGMAN\n\n");
printf("You will be asked to guess the computer's secret word.\n");
printf("The word will be displayed as a number of *'s. Every
time\n");
printf("you guess a letter correctly, that letter will be shown in
its\n");
printf("correct position in the word. If you guess incorrectly,
the\n");
printf("number of tries you have left will be decremented. You
will be\n");
printf("given a maximum of 7 incorrect guesses.\n\n");
hangman();
return 0;
}
So, my issue, is that I need to display the current status of the
player's guesses in a format like ***.. (where * = strlen) and have it
update per guess by replacing. So if someone guesses a and it's in the
word twice it'd be like *..........a.. and so on. I also don't know what I'm
doing wrong with the guess number. If someone guesses right I don't
want guess number to change, but if wrong, just subtract one..this
isn't working either.