/***************************************************************************
* 功能: *
* 猜数字小游戏,分为3个难度等级, *
* 难度1为1~100之间猜 *
* 难度2为1~1000之间猜 *
* 难度3为1~10000之间猜 *
* 统计猜的次数 *
*说明: *
* 根据用户选择的难度等级,随机产生一个相应范围内的数字 *
* 用户输入猜的数字验证是否和随机产生的相等 *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc,char *argv[])
{
int mysteryNumber = 0;
int guessNumber = 0;
int max = 100,min = 1;
int count = 1;
int difficultyLevel = 0;
printf("please input 1~3 for Difficulty Level\n");
scanf("%d",&difficultyLevel);
switch(difficultyLevel)
{
case 1:
{
min = 1;
max = 100;
break;
}
case 2:
{
min = 1;
max = 1000;
break;
}
case 3:
{
min = 1;
max = 10000;
break;
}
default:
printf("input error,please input 1~3 for Difficulty Level\n");
return -1;
break;
}
srand(time(NULL));
mysteryNumber = (rand() % (max - min + 1)) + min;
do
{
switch(difficultyLevel)
{
case 1:
{
printf("input 1~100 a number\n");
scanf("%d",&guessNumber);
break;
}
case 2:
{
printf("input 1~1000 a number\n");
scanf("%d",&guessNumber);
break;
}
case 3:
{
printf("input 1~10000 a number\n");
scanf("%d",&guessNumber);
break;
}
default:
break;
}
if(mysteryNumber > guessNumber)
{
++count;
printf("input number too small\n");
}
else if(mysteryNumber < guessNumber)
{
++count;
printf("input number too large\n");
}
else
{
printf("very good,you guessed correct\n");
printf("you totally guess %d times\n",count);
}
}while(mysteryNumber != guessNumber);
return 0;
}