/*头文件引用*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*主函数*/
int main()
{
/*定义程序需要的变量*/
int number; //系统随机生成的随机数
int low=1; //随机数的最小值
int high=1000; //随机数的最大值
int guess_number; //玩家猜测的数字
int count_guess=0; //玩家猜测的次数
int max_count=10; //允许猜测的最大次数
/*生成指定范围内的随机数*/
srand(time(0)); //设置时间种子使得每次运行程序都能得到随机值
number=rand()%(high-low)+1; //调用rand函数生成随机数
/*开始游戏*/
while(1) //开始死循环,当猜中时或猜10次还没有猜中才能退出
{
count_guess++; //猜测次数加一
printf("please enter the number you guess between %d to %d : ",low,high);
scanf("%d",&guess_number);
/*对玩家猜测的数字和系统随机生成的数字进行比较,并作出相应的处理*/
if(guess_number>number) //猜的数字大了
{
printf("the number you guess is bigger !\n");
}
else if(guess_number<number) //猜测的数字小了
{
printf("the number you guess is smaller !\n");
}
else //猜中了
{
printf("congratulations , you are right , the count of guess is %d .\n",count_guess);
break; //猜对了,退出死循环
}
/*还没有猜中,判断猜测次数是否已经是最大次数次了,超过则游戏失败,退出死循环*/
if(count_guess==max_count)
{
printf("the count of guess is more than %d , you are fail , the right number is %d !\n",max_count,number);
break;
}
}
return 0;
}