C语言猜数字小游戏智能版

不仅有对错,还有评分系统,根据回答的次数给予合适的反馈。此代码适合linux和安卓系统,如果windows想用得删除\033[xxm之类的ANSI打印转义字符,换成windows对应的清屏、切换颜色函数

#include
#include 
#include 
int count(int x,int y)
//折中法计算最保守猜中的次数
{
    int amount=y-x+1,i=0;
    for(; amount>0; i++)
        amount/=2;
    return i;
}
int game()
{
    int guess,randnum,normalCount,x,y,i=0;
    int sameFlag=0,effectiveCount=0;
    int preGuess[16]= {0};
    printf("\033[36m输入两个整数确定范围:\n");
    scanf("%d%d",&x,&y);
    while ( getchar() != '\n' );
    if (x>y)
        //交换
    {
        int tmp=x;
        x=y;
        y=tmp;
    }
    int clearLowBound=x,clearHighBound=y;
    //把猜过数后确定的新范围存进这两个数里面
    randnum=rand()%(y-x+1)+x;
    printf("请猜一个%d~%d的数:\n",x,y);
    normalCount=count(x,y);
    do
    {
        i++,sameFlag=0;
        //i是猜数字的次数,f是有效猜数字的次数
        scanf("%d",&guess);
        while (getchar()!= '\n' );
        if(guess>y||guessclearHighBound) //如果猜得超出比先前确定的范围
                printf("反其道而行 \t");
            else {
                effectiveCount++;
                preGuess[effectiveCount-1]=guess;
                //如果不是超出界限的情况就把先前输入的数存进数组里面去
                if(guess=normalCount)
                        printf("有点笨啊现在还猜不中,猜大点吧 ");
                    else
                        printf("猜大一点 ");
                }
                else if(guess>randnum) {
                    clearHighBound=guess;
                    if(i>=normalCount)
                        printf("有点笨啊现在还猜不中,猜小点吧 ");
                    else
                        printf("猜小一点 ");
                }
            }

        }
    }
    while (guess!=randnum);
    if(i -1.5; y -= 0.16f)
        {
            usleep(30000);
            for (float x = -1.5f; x < 1.5; x += 0.088f)
            {
                float a = x * x + y * y - 1;
                if(a*a*a-x*x*y*y*y<=0)
                    printf("\033[34m*");
                else
                    printf(" ");
            }
            printf("\n");
        }
        printf("\033[16;1H\033[33m\033[?25h太牛B了回答了%d次就正确了,你是欧皇吗?",i);
    }
    else if(i5)
        printf("\033[35m运气不太好,下次加油吧");
    else
        printf("\033[31m你这个也是没谁了用了%d次啊,太菜了吧",i);
    printf("\n你还想继续玩吗?回复1继续\n");
    int ret;
    scanf("%d",&ret);
    printf("\033[2J\033[1;1H");
    return ret;


}
int main()
{
    srand(time(0));
    printf("\033[36m按回车键输入,两个数之间用空格隔开\n");
    while(game()==1);
    return 0;
}

你可能感兴趣的:(c语言)