不仅有对错,还有评分系统,根据回答的次数给予合适的反馈。此代码适合linux和安卓系统,如果windows想用得删除\033[xxm之类的ANSI打印转义字符,换成windows对应的清屏、切换颜色函数
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
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||guess<x)printf("超出范围了,请认真输入");else{for(int i=0; i<effectiveCount; i++)//寻找有没有与之前猜过的数相同if (guess==preGuess[i])sameFlag=1;if(sameFlag==1)printf("之前都输入过了,请输入别的");else if(guess<clearLowBound||guess>clearHighBound) //如果猜得超出比先前确定的范围printf("反其道而行🤣 \t");else {effectiveCount++;preGuess[effectiveCount-1]=guess;//如果不是超出界限的情况就把先前输入的数存进数组里面去if(guess<randnum) {clearLowBound=guess;if(i>=normalCount)printf("有点笨啊现在还猜不中😂,猜大点吧 ");elseprintf("猜大一点 ");}else if(guess>randnum) {clearHighBound=guess;if(i>=normalCount)printf("有点笨啊现在还猜不中😂,猜小点吧 ");elseprintf("猜小一点 ");}}}}while (guess!=randnum);if(i<normalCount-1){printf("\033[?25l\033[2J\033[2;1H");//画爱心for (float y = 1.18f; y > -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*");elseprintf(" ");}printf("\n");}printf("\033[16;1H\033[33m\033[?25h太牛B了回答了%d次就正确了,你是欧皇吗?",i);}else if(i<normalCount)printf("\033[32m恭喜你用了%d次就答对了!",i);else if(i==normalCount)printf("还算正常,用了%d次答对",i);else if(i==normalCount+1&&normalCount>5)printf("\033[35m运气不太好,下次加油吧");elseprintf("\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;
}