目录
写一个猜数字游戏
游戏要求:
电脑自动生成 1~100的随机数;
玩家猜数字,猜数字的过程中,根据猜测数据的大小给出大了或小了的反馈,直到猜对为止,游戏结束。
随机数的生成
要想完成猜数字游戏、首先得产生随机数。
rand
c语言提供了一个函数叫rand,这个函数是可以生成随机数的,函数原型为
int rand (void);
rand函数会返回一个伪随机数,这个随机数的范围是在0~RAND_MAX之间,这个RAND_MAX的大小是依赖编译器上实现的,但是大部分编译器上是32767。
rand函数的头文件是:stdlib.h
这里我们测试一下rand函数,这里多调用几次,产生5个随机数:
5个数是随机的,但两次运行的结果是一样的,这就说明有点问题。
rand函数生成的随机数是伪随机的,伪随机数不是真正的随机数,是通过某种算法生成的随机数。真正的随机数是无法预测下一个值是多少的。而rand函数是对一个叫“种子”的基准值进行运算生成的随机数。之所以前面每次运行程序产生的随机数序列是一样的,那是因为rand函数生成随机数的默认种子是1。如果要生成不同的随机数,就要让种子是变化的。
srand
c语言中又提供了一种函数叫srand,用来初始化随机数的生成器的,srand的原型如下:
void srand (unsigned int seed);
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
如果只是让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;
}
设计随机数的范围
rand() % 100 ; // 余数的范围是 0~99
如果要生成1~100之间的随机数
rand()%100+1;//%100的余数是0~99,0~99的数字+1,范围是1~100
如果要生成100~200的随机数
100 + rand()%( 200-100 + 1 ) // 余数的范围是 0~100 ,加 100 后就是 100~200
如果要生成a~b的随机数
a + rand()%(b-a+1)
猜游戏数字实现
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
//1. 生成随机值
int r = rand()%100+1;
//2. 猜数字
int guess = 0;
while (1)
{
printf("请猜数字:>");
scanf("%d", &guess);
if (guess > r)
printf("猜大了\n");
else if (guess < r)
printf("猜小了\n");
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
printf("*****************\n");
printf("**** 1. play ****\n");
printf("**** 0. exit ****\n");
printf("*****************\n");
printf("请选择:>");
scanf("%d", &input);//输入1:选择游戏 输入0:退出游戏
switch (input)//判断值
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
代码解释:
用 do while 先给游戏打印一个菜单
接着用Switch语句判断,如果input的值为 1,就开始游戏,如果input的值为0,就退出游戏,如果input的值不是1也不是0,就输出选择错误,重新选择
在switch语句的case 1里面封装一个函数game();
创建一个game()函数,不用返回任何值,所以用void;在game()里面写游戏的逻辑,在switch语句中的case 1里面调用。
把srand()写到main函数里(因为srand只用调用一次就行了,不用频繁的调用srand,如果写到game函数里每次玩游戏都要重新设置游戏的起点),设置随机数的起点time函数,参数为NULL,返回类型强制转化为 unsigned int(因为time的返回值time_t是一个整型,为了对应,所以用到了强制类型转换)
在game()里面写游戏的逻辑:首先使用rand();生成随机值
接下来是猜数字环节,因为猜数字不是一次就能猜准的,所以要用到循环,这里我们用到了while循环;
最后就可以运行程序,开始玩游戏了。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
//1. 生成随机值
int r = rand() % 100 + 1;
//2. 猜数字
int guess = 0;
int count = 5;
while (count)
{
printf("还剩下%d次机会", 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("很遗憾,在规定次数内没能猜出结果\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
printf("*****************\n");
printf("**** 1. play ****\n");
printf("**** 0. exit ****\n");
printf("*****************\n");
printf("请选择:>");
scanf("%d", &input);//输入1:选择游戏 输入0:退出游戏
switch (input)//判断值
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}