如何在Hangman游戏C中使用数组

时间:2015-12-05 08:59:22

标签: c

不知道如何进一步解决。

 #include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 #include<string.h>

 void main()   
 {

    printf( "                  Welcome To Hangman!:)           \n");
    printf( " All the Best (y)   \n");
    printf( "You can play it 3 time \n");
    printf( "You have 6 chances for wrong guesses\n");

    int x=0;
    while (x<3)
    {
        char word[]="pakistan";
        /*
        string[1]="india";
        string[2]="america";
        string[3]="aferica";
        string[4]="spain";
        string[5]="china";
        int wrong = 1;
        char word=rand()%5;
        */
        int wrong=0;
        int new=0;
        char so_far[strlen(word)];
        int I;
        for(I=0; I<strlen(word); I++)
        {
            so_far[I] = '_';
        }


        while(wrong<=7 && strcmp(so_far, word)!=0)
        {
            printf("so far the word is: %c\n", so_far);
            char guess;
            printf("enter your guess: ");
            scanf("%c", &guess);
            //guess=guess.upper();

            int i=0;
            //if (new==1)
        //  {


            for(i=0;i<strlen(word);i++)
                    {
                        if (word[i]==guess)

                //  if (guess==word[i])

                        //new+=guess;
                        so_far[i]=guess;
                        new=1;

                //  else

                    //  new+=so_far[i];
                }


        //  so_far[i]=new;
        //  }
            //else
            if(new!=1)
            {
                printf("\nsorry '%c' is not in the word\n",guess);
                printf("\nyour wrong guesses are: %d\n",wrong);
                if (wrong ==1)
                    {
                            printf("\n      +------- \n");
                            printf("       |   |  \n");
                            printf("       |   O  \n");
                            printf("       |    \n");
                            printf("       |    \n");
                            printf("       |    \n");
                            printf("       |    \n");
                            printf("       |    \n");
                            printf("       =============\n");
                }
                if( wrong==2)
                {
                        printf("\n       +------- \n");
                        printf("       |   |  \n");
                        printf("       |   O  \n");
                        printf("       |   | \n");
                        printf("       |   | \n");
                        printf("       |    \n");
                        printf("       |    \n");
                        printf("       |    \n");
                        printf("       =============\n");
                }
            if (wrong==3)
               {
                    printf("\n       +------- \n");
                    printf("       |    |  \n");
                    printf("       |    O  \n");
                    printf("       |    |____   \n");
                    printf("       |    | \n");
                    printf("       |    |  \n");
                    printf("       |    \n");
                    printf("       |    \n");
                    printf("       =============\n");
            }
            if( wrong==4)
                {
                    printf("\n       +------- \n");
                    printf("       |    |  \n");
                    printf("       |    O  \n");
                    printf("       |____|____   \n");
                    printf("       |    | \n");
                    printf("       |    |  \n");
                    printf("       |    \n");
                    printf("       |    \n");
                    printf("       =============\n");
            }
            if( wrong==5)
                {
                printf("\n         +-------\n");
                printf("         |     |\n");
                printf("         |  ___O___\n");
                printf("         |     |\n");
                printf("         |     | \n");
                printf("         |      |\n");
                printf("         |      |  \n");  
                printf("       ==============\n");

                }
            if (wrong==6)
                {
                printf("\n         +-------\n");
                printf("         |     |\n");
                printf("         |  ___O___\n");
                printf("         |     |\n");
                printf("         |     | \n");
                printf("         |   |   |\n");
                printf("         |   |   |  \n");  
                printf("       ==============\n");
            }
            //wrong+=1;
            }
        wrong+=1;
        }
        if (wrong==7)
        {
            printf("you lose and your hanged :(\n");

        }
        else
        {
            printf("you guessd it. Good\n");
        }
        printf("the word is: %c\n",word);



        x++;
        printf("play once again\n");
    }
    printf("game over\n");
    getch();

}

else语句无法正常工作且循环错误&lt; 7而不是打印一次,打印两次。请帮忙

2 个答案:

答案 0 :(得分:0)

# include <stdio.h>
# include <conio.h>
#include <stdlib.h>
#include  <string.h>
void main()

{

printf( "                  Welcome To Hangman!:)           \n");
printf( " All the Best (y)   \n");
printf( "You can play it 3 time \n");
printf( "You have 6 chances for wrong guesses\n");

int x=0;

    int static ne=0;
while (x<3)
{
    char word[]="pakistan";
    /*
    string[1]="india";
    string[2]="america";
    string[3]="africa";
    string[4]="spain";
    string[5]="china";
    int wrong = 1;
    char word=rand()%5;
    */
    int wrong=0;

    char so_far[strlen(word)];
    int I;
    for(I=0; I<strlen(word); I++)
    {
        so_far[I] = '_';
    }

      char guess;
    while(wrong<7 && strcmp(so_far, word)!=0)
    {

            int i;
        printf("so far the word is: %s\n", so_far);

        printf("enter your guess: ");
        scanf(" %c", &guess); //main problem was here as you have not provided any space before %c it was skipping once and calling it twice


        for(i=0;i<strlen(word);i++)
                {
                    if (word[i]==guess )
                    {
                    so_far[i]=guess;
                    ne=1;// variable to check whether any character matched or not
                    }
                }
            if(ne==0)//if not matched then enter here
        {   system("cls");
            printf("\nsorry %c is not in the word\n",guess);
            printf("\nyour wrong guesses are: %d\n",wrong+1);

        wrong+=1;
            }

        ne=0;

    }
        if (wrong==7)
    {
        printf("you lose and your hanged :(\n");

    }
    else
    {
        printf("you guessd it. Good\n");
        printf("the word is: %s\n",word);

    }

    x++;
    printf("play once again\n");
}
printf("game over\n");
getch();
}

为了更好地理解有和没有空格的scanf,请遵循以下答案What does space in scanf mean?

答案 1 :(得分:-1)

当用户猜对了(程序进入其他部分)时,你应该打破。如果没有,它将再次循环并打印两次或更多次。