写一个猜数字游戏:
1.电脑自动生成1到100的随机数。
2.玩家猜数字,猜数字的过程中,根据猜出的数字的大小给出大了或小了的反馈,直到猜对,游戏结束。
一、随机数的生成
要想实现猜数字游戏,首先要生成随机数,那么怎么生成随机数呢?
1.rand()函数
C语言提供了rand()
函数,这个函数是可以生成随机数的,函数原型如下:
int rand(void);
rand()
函数的使用是需要包含头文件<stdlib.h>
的。rand()
函数会返回一个伪随机数,这个伪随机数的范围是0~RAND_MAX
,RAND_MAX的大小是依赖编译器实现的,但是大部分编译器上都是32767。
先测试一下rand()
函数,调用五次试试,产生5个随机数:
#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;
}
第一次运行程序的结果:
第二次运行程序的结果:
我们发现前面两次运行程序产生的随机数序列是一模一样的,这是因为rand()
函数生成的随机数是伪随机数,伪随机数不是真正的随机数,它是通过某种算法生成的随机数。真正的随机数是预测不到下一个具体的值的。rand()
函数是以一个叫做“种子”的基准值进行运算生成随机值的函数。之所以前面两次运行程序生成的随机数序列是一样的,那是因为rand()
函数生成随机数的“种子”默认是1。要想让每次生成的随机数是不一样的,就要让种子发生变化。
2.srand()函数
C语言提供了srand()
函数,是初始化随机数的生成器(种子),函数原型如下:
void srand(unsigned int seed);
程序中在调用rand()
函数之前先调用srand()
函数,通过srand()
函数的参数seed
来设置rand()
函数生成随机数时的种子,只要种子在发生变化,每次生成的随机数序列也都不一样了。也就是说如果给srand()
的种子是随机的,那么rand()
就能生成随机数,生成随机数的同时又需要一个随机数,这就矛盾了。其实种子可以不是随机的,只要让种子是发生变化的就可以。
例如:
种子是1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand(1);
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
种子变为2:
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand(2);
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
两次产生的随机数序列是不一样的。
3.time()函数
一般会使用运行程序的时间作为种子,因为时间是时时刻刻在发生变化的。
C语言提供了time()
函数,这个函数会获取时间,time()
函数使用的时候需要包含头文件<time.h>
,函数原型如下:
time_t time(time_t* timer);
time()
函数返回的是当前的日历时间,其实返回的是从1970年1月1日0时0分0秒到现在运行程序的时间之间的差值,单位是秒。time()
函数的返回值类型是time_t
类型,time_t
类型就是32位或64位的整型类型。
如果time()
函数的参数timer
是非NULL
的指针,那么time()
函数会将返回的时间的差值放在timer
指向的内存中带回去;
如果time()
函数的参数timer
是NULL
的指针,那么time()
函数会返回这个时间的差值,这个时间差值也被叫做:时间戳。
//VS2022上time_t类型的说明
#ifndef _CRT_NO_TIME_T
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t;
#else
typedef __time64_t time_t;
#endif
#endif
typedef long __time32_t;
typedef __int64 __time64_t;
如果只是让time()
函数返回时间戳,可以这样写:
time(NULL);//调用time函数返回时间戳,这里没有接收返回值。返回值类型为time_t类型
那就可以把生成随机数的代码写成这样:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
srand((unsigned int)time(NULL));
//若不想使用time()函数的参数,就传递NULL就行了
//把time()函数的返回值设置为种子
//因为srand()函数的参数是unsigned int类型的,所以对time()函数的返回值强制类型转换
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
srand()
函数不需要频繁调用,一次运行程序中调用一次就可以。
第一次运行程序的结果:
第二次运行程序的结果:
注: 截图只是当时运行程序的结果,大家的结果可能与此不同。
4.设置随机数的范围
若想生成0~99的随机数,如下:
rand() % 100;//余数是0到99
若想生成1~100的随机数,如下:
rand() % 100 + 1;//%100的余数是0到99,加一后就是1到100
若想生成100~200的随机数,如下:
100 + rand() % (200 - 100 + 1);//余数是0到100,加一百后就是100到200
所以若想生成a到b的随机数,如下:
a + rand() % (b - a + 1);
二、猜数字游戏的实现
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
while (1)
{
printf("请输入你猜的数字:");
scanf("%d", &guess);
if (guess < r)
printf("猜小了\n");
else if (guess > r)
printf("猜大了\n");
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
void menu()
{
printf("****1.play****\n");
printf("****0.exit****\n");
printf("**************\n");
printf("**************\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请输入数字:");
scanf("%d", &input);
switch (input)
{
case 0:
printf("游戏结束\n");
break;
case 1:
printf("游戏开始\n");
game();
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
还可以加上次数的限制,如果5次猜不出来,就算失败:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
int count = 5;
while (count)
{
printf("你还有%d次猜数字的机会\n", count);
printf("请输入你猜的数字:");
scanf("%d", &guess);
if (guess < r)
printf("猜小了\n");
else if (guess > r)
printf("猜大了\n");
else
{
printf("恭喜你,猜对了\n");
break;
}
count--;
}
if (count == 0)
printf("你五次机会用光了,正确值为%d\n", r);
}
void menu()
{
printf("****1.play****\n");
printf("****0.exit****\n");
printf("**************\n");
printf("**************\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请输入数字:");
scanf("%d", &input);
switch (input)
{
case 0:
printf("游戏结束\n");
break;
case 1:
printf("游戏开始\n");
game();
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}