目录
1. 游戏要求
- 电脑自动生成 1~100 之间的随机数
- 玩家猜数字,猜数字过程中,根据玩家猜测数据的大小给出大了或小了的提示,有五次机会,猜对则游戏结束;五次机会用光,游戏失败。
2. 随机数生成
要想完成猜数字游戏,首先得产生随机数,那怎么产生随机数呢?
2.1 rand
C语言提供了一个函数叫 rand,这个函数可以生成随机数,rand 函数原型如下:
int rand (void);
rand 函数的使用需要包含一个头文件:stdlib.h
rand 函数会返回一个伪随机数,这个随机数的范围是在 0 ~ RAND_MAX 之间,RAND_MAX 的大小依赖编译器实现,大部分编译器上是32767。
测试一下 rand 函数,产生五个随机数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
运行之后你会发现第一次产生的5个数字的确是随机的,但是下一次运行程序生成的结果和上一次一模一样,这就说明有问题。
其实,rand 函数生成的随机数是伪随机的,是通过某种算法生成的随机数,并不是真正的随机数。rand 函数是对一个叫“种子”的基准值进行运算生成的随机数。之所以前面产生的随机数一样,那是因为 rand 函数生成随机数的默认种子是1.如果要生成真正的苏技术,就要让种子变化。
2.2 srand
C语言中还提供了 srand,用于初始化随机数生成器,srand 的原型如下:
void srand (unsigned int seed);
程序中在调用 rand 函数之前先调用 srand 函数,通过 srand 函数的参数 seed 来设置 rand 函数生成随机数的时候的种子,只要种子在变化,每次生成的随机数也就变化起来了。
但是问题来了,给 srand 的种子需要是随机的,rand 才能生成随机数,那这个给种子的随机数又由谁来给呢?在生成随机数的时候有需要一个随机数,这就很矛盾了。下面可能会有答案。
2.3 time
在程序中我们一般使用程序的运行时间作为种子,因为时间是时刻在变化的。
C语言中有一个函数叫 time,可以获得这个时间,time 函数原型如下:
time_t time (time_t* timer);
使用 time 函数的时候需要包含头文件:time.h
time 函数会返回当前的日历时间,其实返回的是1970年1月1日0时0分0秒到现在程序运行时间的值,单位是秒。返回类型是 time_t 类型的,time_t 类型本质上是32位或64位的整型类型。
time 函数的参数 timer 如果是非 NULL 的指针的话,函数也会将这个返回的插值放在 timer 指向的内存中带回去。
如果 timer 是 NULL,就只返回这个时间的值。timer 函数返回的这个时间差也被叫做:时间戳
time(NULL);//调用time函数返回时间戳,这里没有接收返回值
所以最终我们生成随机数的代码改写成如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
//使⽤time函数的返回值设置种⼦
//因为srand的参数是unsigned int类型,我们将time函数的返回值强制类型转换
srand((unsigned int)time(NULL));
printf("%d\n", rand()%100);//生成0~99之间的随机数
printf("%d\n", rand()%100+1);//生成1~100之间的随机数
printf("%d\n", 100 + rand()%(200-100+1));//生成100~200之间的随机数
printf("%d\n", a + rand()%(b-a+1));//生成a~b之间的随机数
return 0;
}
3. 菜单生成
程序开始时,需要给一个简单的菜单界面:
void menu()
{
printf("******************\n");
printf("*****1.play*******\n");
printf("*****0.exit*******\n");
printf("******************\n");
}
4. 猜数字游戏实现
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
int count = 5;
while (count) {
printf("%d chance left\n", count);
printf("请猜数字:");
scanf("%d", &guess);
if (guess > r)
{
printf("大了!\n");
}
else if (guess < r)
{
printf("小了!\n");
}
else
{
printf("u r right!\n");
break;
}
count--;
}
if (count == 0)
{
printf("you lose!\n");
}
}
5. 整体游戏实现
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("******************\n");
printf("*****1.play*******\n");
printf("*****0.exit*******\n");
printf("******************\n");
}
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
int count = 5;
while (count) {
printf("%d chance left\n", count);
printf("请猜数字:");
scanf("%d", &guess);
if (guess > r)
{
printf("big!\n");
}
else if (guess < r)
{
printf("small!\n");
}
else
{
printf("u r right!\n");
break;
}
count--;
}
if (count == 0)
{
printf("you lose!\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("game over");
break;
default:
printf("wrong!");
break;
}
} while (input);
return 0;
}