C语言-猜数游戏

目录

1. 随机数的生成

1.1 rand函数

1.2 srand函数

1.3 time函数

1.4 设置随机数的范围

2. 猜数游戏的实现

参考代码1:

参考代码2:


掌握了前面博客学习的这些知识,我们就可以写一些稍微有趣的代码了,比如:
写⼀个猜数字游戏

游戏要求:

  1. 电脑自动生成1~100的随机数
  2. 在玩家猜数字过程中,根据玩家猜测的数据大小给出 大了或者小了 的提示反馈,直到猜对游戏结束。

1. 随机数的生成

要想完成猜数字游戏,首先得产生随机数,那怎么产生随机数呢?

1.1 rand函数

C语言提供了一个函数叫rand,这个函数是可以生成随机数的,函数原型如下所示:

int rand(void);

rand函数会返回一个伪随机数,这个随机数的范围是在0 ~ RAND_MAX之间,这个RAND_MAX 的大小是依赖编译器上实现的,但是大部分编译器是32767。

rand函数是使用需要一个头文件是: stdlib.h

那我们就测试一下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;
}

运行两次结果如下:

57483ec8aac942e18921bd7402aacb3e.pnga257aaf80a82415ba13f598eb626d473.png

我们可以看到虽然一次运行中产生的5个数字是相对随机的,但是下⼀次运行程序生成的结果和上一次一模一样,这就说明有点问题。
如果再深入了解⼀下,我们就不难发现,其实rand函数生成的随机数是伪随机的,伪随机数不是真正的随机数,是通过某种算法生成的随机数真正的随机数的是无法预测下一个值是多少的

而rand函数是对一个叫 “种子” 的基准值进行运算生成的随机数。
之所以前面每次运行程序产生的随机数序列是⼀样的,那是因为rand函数生成随机数的默认种子是1。
如果要生成不同的随机数,就要让种子是变化的。

接下来我们介绍另一个函数:srand。

1.2 srand函数

C语言中有提供了一种函数是:srand,用来初始化随机数的生成器的,srand的原型如下:

viod srand(unsigned int seed);

程序中在调用 rand 函数之前先调用 srand 函数,通过 srand 函数的参数 seed 来设置rand函数生成随机数的时候的种子,只要种子在变化,每次生成的随机数序列也就变化起来了。
那也就是说给srand的种子是如果是随机的,rand就能生成随机数;在生成随机数的时候又需要⼀个随机数,这就矛盾了。

这时候就该用到了 time (时间)这个函数。

1.3 time函数

在程序中我们一般是使用程序运行的时间作为种子的,因为时间时刻在发生变化的。
在C语言中有⼀个函数叫 time ,就可以获得这个时间,time函数原型如下:

time_t time (time_t * timer);

time 函数会返回当前的日历时间,其实返回的是1970年1⽉1日0时0分0秒到现在程序运行时间之间的差值,单位是秒。返回的类型是time_t类型的,time_t 类型本质上其实就是32位或者64位的整型类型。

time函数的参数 timer 如果是非NULL的指针的话,函数也会将这个返回的差值放在timer指向的内存中带回去。
如果 timer 是NULL,就只返回这个时间的差值。time函数返回的这个时间差也被叫做:时间戳。
time函数的时候需要包含头文件:time.h

//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函数返回时间戳,这里没有接收返回值

那我们就可以让生成随机数的代码改写成:

#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());
		printf("%d\n", rand());
		printf("%d\n", rand());
		printf("%d\n", rand());
		printf("%d\n", rand());
	return 0;
}

现在我们运行,看看生成的随机数是否还是伪随机数。

46ba6bba171c4fdbb8fd955e3462843c.png7b672397db84459a83436557066673b0.png

注意:

  1. 每次运行的结果不一定相同
  2. srand函数是不需要频繁调用的,一次运行的程序调用一次就够了。

1.4 设置随机数的范围

如果我们要生成0 ~ 99之间的随机数,方法如下:

rand() % 100; 
//余数范围是0 ~ 99

如果要生成1 ~ 100的随机数,方法如下:

rand() % 100 + 1; 
//%100的余数是0 ~ 99,0 ~ 99的数字 + 1,范围就是1 ~ 100

如果要生成100 ~ 900的随机数,方法如下:

100 + rand()%(900-100+1);
//余数的范围是0~800,加上 100 之后就是100 ~ 900

如此类推,我们可以得到,如果要生成a ~ b的随机数,方法如下:

a + rand()%(b - a + 1);

2. 猜数游戏的实现

参考代码1:

#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("***********************\n");
	printf("****** 1. play ******\n");
	printf("****** 0. exit ******\n");
	printf("***********************\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("游戏结束\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

参考代码2:

还可以加上猜数字的次数限制,如果5次猜不出来,就算失败.

#include <stdlib.h>
#include <time.h>
void game()
{
	int r = rand() % 100 + 1;
	int guess = 0;
	int count = 5;//设置可以猜数的次数
	while (1)
	{
		printf("\n你还有%d次机会\n",count);//提示玩家还有的次数
		count--;//每猜一次减少一次机会
		printf("请猜数字>:");
		scanf("%d", &guess);
		if (guess < r)
		{
			printf("猜小了\n");
		}
		else if (guess > r)
		{
			printf("猜大了\n");
		}
		else
		{
			printf("恭喜你,猜对了\n");
			break;
		}
	}
	if (count == 0)//当次数为0了,宣告游戏失败
	{
		printf("你失败了,正确的值是%d\n",r);
	}
}

void menu()
{
	printf("***********************\n");
	printf("****** 1. play ******\n");
	printf("****** 0. exit ******\n");
	printf("***********************\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("游戏结束\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'撒野奔跑.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值