C语言小游戏多次循环偶尔几次会出bug,求解?
  • 编了一个猜单词的吊小人小游戏,每次运行时开始几个猜起来都没问题,循环十次左右之后就会出现bug(原本猜之前会输出一行下划线,出bug时就只会出来一个)。
  • bug循环结束后可能恢复正常,再循环几次后又会出现bug。
  • 求大佬帮忙看一看,实在是不明白为什么啊QAQ
  • 代码如下:
  • (谢谢大佬!)
#include <stdio.h.>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define draw1 printf("________\n|   |\n|\n|\n|\n|\n|______\n|      |____\n|___________|\n")
#define draw2 printf("________\n|   |\n|   o\n|\n|\n|\n|______\n|      |____\n|___________|\n")
#define draw3 printf("________\n|   |\n|   o\n|  /\n|\n|\n|______\n|      |____\n|___________|\n")
#define draw4 printf("________\n|   |\n|   o\n|  /|\n|\n|\n|______\n|      |____\n|___________|\n")
#define draw5 printf("________\n|   |\n|   o\n|  /|\n|   |\n|\n|______\n|      |____\n|___________|\n")
#define draw6 printf("________\n|   |\n|   o\n|  /|\\   \n|   |\n|\n|______\n|      |____\n|___________|\n")
#define draw7 printf("________\n|   |\n|   o\n|  /|\\   \n|   |\n|  /\n|______\n|      |____\n|___________|\n")
#define draw8 printf("________\n|   |\n|   o\n|  /|\\  \n|   |\n|  / \\   \n|______\n|      |____\n|___________|\n")
void puta(char a[100],int n)
{
    int m;
    for(m=0;m<n;m++)
        printf("%c ",a[m]);
    printf("\n");
}
void draw(char a[100],int n,int i)
{
    switch(i)
                {
                case 0: draw1; break;
                case 1: draw2; break;
                case 2: draw3; break;
                case 3: draw4; break;   
                case 4: draw5; break;
                case 5: draw6; break;
                case 6: draw7; break;
                }
            printf("待填单词:    ");
            puta(a,n);
}
void yn(int b,char c,char a[100])
{
    int d,n;
    n=0;
    for(d=0;d<b;d++)
    {
        if (a[d]==c)
            n=1;
    }
    if(n==1)
        printf("您已输入过%c(猜中),请重新输入:",c);
    else
        printf("您已输入过%c(未中),请重新输入:",c);
}
void main()
{
    int b,c,d,e,f,g,h,i,j,k,l,p,t;
    char q='y',r;
    char a1[][15]={"university","computer","language","homework","economics","mathematics","textbook","hamburger","chocolate","dumpling"};
    char a2[10][100]={'\0'};
    char a3[100]={'\0'};
    char a4[100]={'\0'};
    char y[100]={'\0'};
    e=0,i=0,k=0,h=0;
    srand((unsigned)time(0)); 
    b=rand()%10;
    c=strlen(a1[b]);
    while(q=='y')
    {
    printf("开始游戏...\n");
    draw1;
    printf("\n待猜单词:    ");
    for(d=0;d<c;d++)
        a3[d]='_';
    puta(a3,c);
    printf("\n第1次字母猜测:");
    do{
        do
        {
            scanf("%s",a2[e]);
            if((strlen(a2[e])==1)&&(a2[e][0]>='a')&&(a2[e][0]<='z'))
            {
                f=a2[e][0];
                for(l=0,p=0;l<c;l++)
                {
                    if(f==a4[l])
                        p=1;
                }
                if(p==1)
                {
                    yn(c,f,a1[b]);
                }
                else
                {
                    a4[e]=f;
                    break;
                }
            }
            else
                printf("第%d次字母猜测:",e+1);
        }
        while(1);
        h=0;
        for(g=0;g<c;g++)
        {
            if(a1[b][g]==f)
            {
                a3[g]=f;
                h=1;
            }
            else
                ;
        }
        if(h==1)
            draw(a3,c,i);
        else
        {
            i++;
            if(i<7)
                draw(a3,c,i);
            else
            {
                draw8;
                printf("You Lost! The man is HANGED.\n真实单词:");
                puta (a1[b],c);
                printf("你的猜测序列:");
                puta (a4,e+1);
                break;
            }
        }
        k=0;
        for(j=0;j<c;j++)
        {
            if(a3[j]=='_')  
                k=1;
        }
        if(k==0)
        {
            printf("You Win!\n");
            printf("你的猜测序列:");
            puta (a4,e+1);
            break;
        }
        e++;
        printf("第%d次字母猜测:",e+1);
    }
    while(1);
    do
    {
        printf("想再玩一次吗?(y/n)\n");
        scanf(" %c%c",&q,&r);
    }
    while((r!='\n')|((q!='y')&&(q!='n')));
    if(q=='n')
        break;
    for(t=0;t<10;t++)
        memset(a2[t],0,sizeof(a2[t]));
    memset(a3,0,sizeof(a3)); 
    memset(a4,0,sizeof(a4)); 
    memset(y,0,sizeof(y)); 
    e=0,i=0,k=0,h=0,t=0;
    srand((unsigned)time(0)); 
    b=rand()%10;
    c=strlen(a1[b]);
    }
    system ("pause");
}

1个回答

qq_43889776
qq_43889776 谢谢,可是这个和我的问题差别还挺大的?
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐