简体   繁体   中英

Program stops working after typing a character and I don't know how

I'm making a Hangman game for a school requirement. After running the program, it suddenly stops working after guessing a word. It's not finished yet since it's not working here's what I'm trying to accomplish: Hangman is a word guessing game in which the player is told how many letters are in the word. The player must discover the word by guessing letters one at a time. Each correctly guessed letter is added to the word. On each wrong guess, a body part is added to a picture of a hanged man. The player is allowed 5 mistakes, corresponding to hangman's head, body, left arm, right arm, left leg. At the 6th mistake the right leg is drawn, and the game is over. If a letter is repeated more than once, the repeating occurrences are always considered as an error, even if the first time was correct. Once the hanged man is complete, the player loses the game.

A hangman will look like this after 0 to 6 errors. There should be no whitespace at the end of lines.
    +--+    +--+    +--+    +--+    +--+    +--+    +--+
    |           |   o       |   o       |   o       |   o       |   o       |   o
    |           |           |   |       |  /|       |  /|\      |  /|\      |  /|\
    |\      |\          |\          |\      |\      |\ /        |\ / \

  Your task is to Implement the game of Hangman. For this problem, you need to have an array holding ten different words for the player to choose from. You will be required to use rand() function under <stdlib.h> to choose between those ten words. The chosen word will then be guessed by the player.


    Sample Run.

    H A N G M A N
          +---+
          |
          |
          |\
         ===
    Missed letters: 
    Mystery word: _ _ _
    Guess a letter.
    a

    H A N G M A N
          +---+
          |
          |
          |\
         ===
    Missed letters:
    Mystery word: _ a _
    Guess a letter.
    o






   H A N G M A N
          +---+
          |    o
          |
          |\
         ===
    Missed letters: o
    Mystery word: _ a _
    Guess a letter.
    r

    H A N G M A N
          +---+
          |    o
          |    |
          |\
         ===
    Missed letters: o r
    Mystery word: _ a _
    Guess a letter.
    t

    H A N G M A N
          +---+
          |    o
          |    |
          |\
         ===
    Missed letters: o r
    Mystery word: _ a t
    Guess a letter.
    a

    H A N G M A N
          +---+
          |    o
          |   /|
          |\
         ===
    You have already guessed that letter. Choose again.
    Guess a letter.
    c



    Yes! The secret word is "cat"! You have won!
    Do you want to play again? (yes or no)
    no

here's my code:

        #include <stdio.h>
            #include <stdlib.h>
            #include <time.h>
            #include <ctype.h>
            #include <string.h>


            char word[3][10] = {"gabb", "mnl", "josh"};
            char parts[6] = {" "};
            char mwords[6] = {" "};
            char blanks[10];
            int life = 0;
            int len;
            char guess;

            void body(int);
            void generate(int);
            int compare(int);

            int main(){
                int res;
                char ans[3];

                int gen;

                srand(time(0));
                gen = rand() % 3;

                for(int i = 0; i < strlen(word[gen]); i++){
                    blanks[i] = '_';
                }

                do{
                    while(life < 6){
                        body(life);
                        generate(gen);
                        res = compare(gen);
                        if(res == 1){
                            printf("Yes! The secret word is \"%s\"! You have won!");
                            break;
                        }
                    }
                    printf("Do you want to play again? (yes or no): ");
                    scanf(" %s", ans);
                }while(strcmp (ans, "yes") == 0);
            }

this prints the body of hangman depending on the number of lives

            void body(int n){

                char guess;

                switch(n){
                case 6:
                    parts[5] = '\\';
                case 5:
                    parts[4] = '/';
                case 4:
                    parts[3] = '\\';
                case 3:
                    parts[2] = '/';
                case 2:
                    parts[1] = '|';
                case 1:
                    parts[0] = 'O';
                }

                printf("H A N G M A N\n");
                printf("\t+---+\n");
                printf("\t|   %c\n", parts[0]);
                printf("\t|  %c%c%c\n",parts[2], parts[1], parts[3]);
                printf("\t|\\ %c %c\n", parts[4], parts[5]);
                printf("\t===\n");

            }

this generates the "guessing area"

            void generate(int a){

                len = strlen(word[a]);


                printf("Missed words: ");
                    for(int i = 0; i < 6; i++){
                        if(mwords == '\0')
                            break;
                        else
                            printf("%c",mwords[i]);
                    }
                printf("\n");

                printf("Mystery word: ");
                    for(int i = 0;i < len; i++){
                        printf("%c ", blanks[i]);
                    }
                    printf("\n");

I don't know if the problem lies here

                printf("Guess a letter: ");
                scanf("%c", guess);

                for (int i = 0; i < len; i++){
                    if(word[a][i] == guess)
                        blanks[i] = guess;
            }
                for(int i = 0; i < len;i++){
                    if(word[a][i] == guess)
                        break;
                    else
                        life++;
                }

            }

this function compares two strings

            int compare(int comp){
                if(strcmp (word[comp], blanks) == 0)
                    return 0;
                else
                    return 1;
            }

There are many errors in your code and in the logic of your program:

char ans[3]; //should be ans[4] to be able to read the word yes for example

the funtion void body(int n) in the body of switch(n) in the different case your never use break for separe each case. Solution:

switch(n){
    case 6: parts[5] = '\\'; break;
    case 5: parts[4] = '/';  break;
    case 4: parts[3] = '\\'; break;
    case 3: parts[2] = '/';  break;
    case 2: parts[1] = '|';  break;
    case 1: parts[0] = 'O';  break;
}

other difficult in your code logic is:

if(res == 1){
    printf("Yes! The secret word is \"%s\"! You have won!\n");
    break
}

When res == 1 the words are not the same, also in

printf("Yes; The secret word is \"%s\"! You have won!\n");

generate a warning message because you not add the vale for %s . The solution is:

if(res == 0){
   printf("Yes! The secret word is \"%s\"! You have won!\n", word[gen]);
   break;
}

But the main problem in your code logis is that your never catch the letter from the user and update the state of game. The solution may be to add a function to do this after the function generate(gen) for this functionality for example:

void readLetterUpdateLife(int gen){
    bool find = false;
    char letter;
    scanf(" %s", &letter);
    for(int i=0; i<strlen(word[gen]); ++i){
        if(word[gen][i] == letter){
            blanks[i] = letter;
            find = true;
        }
    }
    if(!find) mwords[life++] = letter;
}

and finaly if you want pay again the game you should be reset all variables that your using for the game, example:

printf("Do you want to play again? (yes or no): ");
scanf(" %s", ans);
if(strcmp (ans, "yes") == 0){
    life = 0;             //Reset the life
    srand(time(0));       //Select a new seed for rand
    gen = rand() % 3;     //Reset the word
    for(int i = 0; i < strlen(word[gen]); i++){
        blanks[i] = '_';  //Clear blanks
    }
    strcpy(mwords,"     "); //Reset wrong letters
    strcpy(parts,"     ");  //Reset parts
}

based on all the changes that I propose the final program is:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>

char word[3][10] = {"gabb", "mnl", "josh"};
char parts[6] = {"    "};
char mwords[6] = {"    "};
char blanks[10];
int life = 0;
int len;
char guess;

void body(int);
void generate(int);
int compare(int);
void readLetterUpdateLife(int);

int main(){
    int res;
    char ans[4];
    int gen;
    srand(time(0));
    gen = rand() % 3;
    for(int i = 0; i < strlen(word[gen]); i++){
        blanks[i] = '_';
    }
    do{
        while(life < 6){
            body(life);
            generate(gen);
            readLetterUpdateLife(gen);
            res = compare(gen);
            if(res == 0){
                printf("Yes! The secret word is \"%s\"! You have won!\n", word[gen]);
                break;
            }
        }
        printf("Do you want to play again? (yes or no): ");
        scanf(" %s", ans);
        if(strcmp (ans, "yes") == 0){
            life = 0;             //Reset the life
            srand(time(0));       //Select a new seed for rand
            gen = rand() % 3;     //Reset the word
            for(int i = 0; i < strlen(word[gen]); i++){
                blanks[i] = '_';  //Clear blanks
            }
            strcpy(mwords,"     "); //Reset wrong letters
            strcpy(parts,"     ");  //Reset parts
        }
    }while(strcmp (ans, "yes") == 0);
}

void body(int n){
    char guess;
    switch(n){
        case 6: parts[5] = '\\'; break;
        case 5: parts[4] = '/';  break;
        case 4: parts[3] = '\\'; break;
        case 3: parts[2] = '/';  break;
        case 2: parts[1] = '|';  break;
        case 1: parts[0] = 'O';  break;
    }
    printf("H A N G M A N\n");
    printf("\t+---+\n");
    printf("\t|   %c\n", parts[0]);
    printf("\t|  %c%c%c\n",parts[2], parts[1], parts[3]);
    printf("\t|\\ %c %c\n", parts[4], parts[5]);
    printf("\t===\n");
}

void generate(int a){
    len = strlen(word[a]);
    printf("Missed words: ");
    for(int i = 0; i < 6; i++){
        if(mwords[i] == '\0')
            break;
        else
            printf("%c",mwords[i]);
    }
    printf("\n");
    printf("Mystery word: ");
    for(int i = 0;i < len; i++){
        printf("%c ", blanks[i]);
    }
    printf("\n");
}

int compare(int comp){
    if(strcmp (word[comp], blanks) == 0)
        return 0;
    else
        return 1;
}

void readLetterUpdateLife(int gen){
    bool find=false;
    char letter;
    scanf(" %c", &letter);
    for(int i=0; i<strlen(word[gen]); ++i){
        if(word[gen][i] == letter){
            blanks[i] = letter;
            find = true;
        }
    }
    if(!find) mwords[life++] = letter;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM