自学内容网 自学内容网

【C语言】猜数字小游戏

😂个人主页: 起名字真南
🤣个人专栏:【数据结构初阶】 【C语言】 【C++】

请添加图片描述

前言:我们学习完前面的循环以后可以写一个猜数字小游戏

1 随机数的生成

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

1.1 rand

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

int rand(void

rand函数会生成一个伪随机数,这个随机数的范围是在0~RAND_MAX之间,这个RAND_MAX的大小是依赖编译器上实现的,但是大部分编译器是32767.
rand函数的使用需要包含<stdlib.h>的头文件

#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函数生成随机数是依靠一个种子作为基准值来计算并生成随机数,而之所以运行生成的随机数都是一样的是因为默认情况下默认种子是1。

1.2 srand

C语言中又提供了一个函数叫做srand,用来初始化随机数的生成器,代码如下

void srand (unsigned int seed);

程序中在调用rand函数之前要先调用srand函数,通过srand函数的参数来改变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());

srand(2);

printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}

结果展示:
在这里插入图片描述
可以看到将种子改为2就会生成另一组新的伪随机数。但是如果我们一直需要不同的随机数就需要一直改变种子的值,有没有什么办法呢?

1.3 time

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

time_t time(time_t* timer);

time函数会返回当前的日历时间,其实返回的是1970年1月1日0时0分0秒到现在程序运行时间的差值,单位是秒,返回类型是time_t(实质上就是32位或64位的整形类型),time函数的参数timer如果是非NULL指针的话,函数也会将这个返回的差值放入到timer指向的内存中。
如果timer是NULL那么将只返回当前程序运行的时间差,也被叫做“时间戳”,time函数使用时需要包含头文件time.h

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
srand((unsigned)time(NULL));
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}

第一次运行结果:
在这里插入图片描述
第二次运行结果:
在这里插入图片描述

1.4 设置随机数范围

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
srand((unsigned)time(NULL));
printf("%d", rand() % 100);  //余数的范围是 0~99
printf("%d", rand() % 100 + 1);  //余数的范围是 1~100
printf("%d", 100 + rand() % 101);  //余数的范围是 100~200
return 0;
}

2 猜数字游戏实现

代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void menu()
{
//system("cls");
printf("***********************\n");
printf("******  1. play  ******\n");
printf("******  0. exit  ******\n");
printf("***********************\n");
}
void game()
{
//生成0~100的随机数
srand((unsigned int)time(NULL));
int x = rand() % 101;
int input = 0;
int count = 5;
while (count)
{
printf("你还有%d次机会\n", count);
printf("请输入数字大小>");
scanf("%d", &input);
if (input < x)
{
printf("猜小了\n");
count--;
}
else if (input > x)
{
printf("猜大了\n");
count--;
}
else
{
printf("猜对了!!!\n");
break;
}
if (count == 0)
{
printf("游戏结束数字是%d\n", x);
}
}

}
int main()
{
int choose = 0;
do
{
menu();
printf("请输入>>");
scanf("%d", &choose);
switch (choose)
{
case 1:
game();
break;
case 0:
printf("游戏结束");
break;
defult:
printf("选择错误,请重新选择0/1");
break;
}
} while (choose);
return 0;
}

原文地址:https://blog.csdn.net/weixin_74837455/article/details/142760225

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!