Barry Schwarz wrote:
> On 21 Nov 2005 17:25:25 -0800, "tigrfire" <> wrote:
>
> >So I'm trying to write a hangman game and the output is coming out a
> >little strange. Here's my code thus far:
> >
> >
> >#include <stdio.h>
> >#include <string.h>
> >#include <stdlib.h>
> >#include <time.h>
> >
> >int hangman()
>
> If the function does not accept arguments, state it specifically:
> int hangman(void)
>
> >{
> > char input;
> > char temp[10];
>
> tempt is uninitialized. It also needs to be at least 12 long to hold
> "publication".
>
> > char word[][20] = {"literature","scholar", "enormous", "influence",
> >"publication", "pioneer", "reshape", "catalyst", "leader", "member",
> >"final", "phonograph", "executed",
> > "oldest", "people", "requiring", "screwdriver", "buckshot",
> >"different", "striking"};
> >
> > int k = rand()%20;
>
> The 20 here is based on the first dimension of word, not the second.
> If you ever change the number of strings in the initialization clause,
> this will be wrong. You might want to compute the number of entries
> in word with code like
> int ent = sizeof word/sizeof *word;
>
> You never seed the rand function.
>
> > int i = 0;
> > int r = 0;
> >
> > char b[]= "____________";
> >
> > int length = strlen(word[k]);
> > int turn = length, dummy = 0, decide = 0, z = 0;
> >
> > for(i=0; i < length; i++)
> > {
> > printf("%c", b[i]);
> > }
> >
> > while(turn != 0)
> > {
> > if (strcmp(temp,word[k]) == 0)
>
> This invokes undefined behavior because temp is indeterminate.
>
> > {
> > printf("You have guessed");
> > }
> >
> > printf("\nTurns left: ");
> > printf("%d", turn);
>
> If you want insure the output appears to the user, terminate it with
> \n.
>
> >
> > printf("\nEnter a letter to guess: ");
> > scanf("%c", &input);
>
> Here is the problem you ask about. You read one character. However,
> you have to type two: the character being guessed and the ENTER key.
> The ENTER keys sits in the input buffer until you execute this
> statement again and then it gets processed. Neither scanf nor
> getchar() are particularly user friendly in this regard. Recommend
> using fgets.
>
> >
> > for(r = 0; r < length; r++)
> > {
>
> The single easiest thing you can do to make your programs more
> readable is adopt a consistent style of indenting, preferably at least
> three characters.
>
> > if(input == word[k][r] && b[r] == '_')
>
> Do you really expect the single character in input to equal all the
> characters in word[k]?
>
> > {
> > decide = 1;
> > turn = length - dummy;
> > }
> > 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];
>
> Undefined behavior when word[k] has 11 characters or more.
>
> > temp[length] = '\0';
> >
> > printf("%c", b[r]);
> > }
> > z++;
> > }
> > }
> >
> > if(strcmp(temp, word[k]) != 0)
> > {
> > printf("Sorry, no more turns left. The secret word was %s.",
> >word[k]);
> > }
> > else
> > {
> > printf("Congratulations!\nYou guessed the secret word: %s",
> >word[k]);
> > }
> >
> > return 0;
> >}
> >
> >
> >
> >int main()
> >{
> > char response;
> >
> >do
> > {
> > hangman();
> >
> > printf("Do you want to continue (y or n): ");
> > scanf("%c", &response);
> > }
> > while(response == 'y');
> >
> > return 0;
> >}
> >
> >This produces an output similar to this:
> >_________
> >Turns left: 9
> >Enter a letter to guess: b
> >
> >Turns left: 8
> >Enter a letter to guess:
> >Turns left: 7
> >Enter a letter to guess: e
> >ee
> >Turns left: 7
> >Enter a letter to guess:
> >Turns left: 6
> >Enter a letter to guess:
> >
> >(note: the output would have continued but I suspended it)
> >
> >Here's my problem, I am trying to get the program to read the strlen
> >and output one * for each 'hidden' letter of the word, that doesn't
> >seem so bad. My real problem is that I want those asteriscks to stay
> >there and get replaced by the correct letter when guessed. Also, I
> >don't really know why it subtracts two turns away each time or
> >sometimes none at all. Any help?
>
>
> <<Remove the del for email>>
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:
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**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.