猜数字游戏

猜数字游戏 在掌握了分支与循环相关知识之后我们就可以写一些有趣的代码了比如猜数字游戏游戏要求电脑⾃动⽣成1~100的随机数玩家猜数字猜数字的过程中根据猜测数据的⼤⼩给出⼤了或⼩了的反馈直到猜对游戏结束。1 · 生成随机数要想完成猜数字游戏⾸先得产⽣随机数那怎么产⽣随机数呢1 · 1 randC语⾔提供了⼀个函数叫rand这函数是可以⽣成随机数的函数原型如下所示int rand (void);rand函数会返回⼀个伪随机数这个随机数的范围是在0~RAND_MAX之间这个RAND_MAX的⼤⼩是依赖编译器上实现的⼤部分编译器上是32767。rand函数的使⽤需要包含⼀个头⽂件stdlib.h。那我们就测试⼀下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; }运行一下试试我们再运行一次就会发现还是这 5 个数字。我们可以看到虽然⼀次运⾏中产⽣的5个数字是相对随机的但是下⼀次运⾏程序⽣成的结果和上⼀次⼀模⼀样这就说明有点问题。如果再深⼊了解⼀下我们就不难发现其实rand函数⽣成的随机数是伪随机的伪随机数不是真正的随机数是通过某种算法⽣成的随机数。真正的随机数的是⽆法预测下⼀个值是多少的。⽽rand函数是对⼀个叫“种⼦”的基准值进⾏运算⽣成的随机数。之所以前⾯每次运⾏程序产⽣的随机数序列是⼀样的那是因为rand函数⽣成随机数的默认种⼦是1。如果要⽣成不同的随机数就要让种⼦是变化的。1 · 2 srandC语⾔中⼜提供了⼀个函数叫srand⽤来初始化随机数的⽣成器的srand的原型如下void srand (unsigned int seed);程序中在调⽤ rand 函数之前先调⽤ srand 函数通过 srand 函数的参数seed来设置rand函数⽣成随机数的时候的种⼦只要种⼦在变化每次⽣成的随机数序列也就变化起来了。那也就是说给srand的种⼦是如果是随机的rand就能⽣成随机数在⽣成随机数的时候⼜需要⼀个随机数这就⽭盾了。1 · 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就只返回这个时间的差值。time函数返回的这个时间差也被叫做时间戳。如果只是让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; }多运⾏⼏次看看每次的运⾏就有差异了。srand函数是不需要频繁调⽤的⼀次运⾏的程序中调⽤⼀次就够了。1 · 4 生成随机数的范围由于我们想要做的游戏是在 1 ~ 100 猜数字但 rand 生成的随机数的范围是在0~RAND_MAX。如何缩小这个范围呢可以用 %如果我们要⽣成0~99之间的随机数⽅法如下rand() % 100;//余数的范围是0~99所以我们要生成1~100之间的随机数就可以这么写rand()%1001;//%100的余数是0~99,0~99的数字1,范围是1~100以此类推如果要⽣成a~b的随机数⽅法如下a rand()%(b-a1)2 · 猜数字游戏的实现#include stdio.h #include stdlib.h #include time.h void Menu() { printf(***** 1 到 100猜数字 *****\n); printf(**************************\n); printf(***** 1 : play *****\n); printf(******0 : exit *****\n); printf(**************************\n); } void Game() { //生成 1 到 100 间的随机数 int i 0; int ret 0; ret (rand() % 100) 1; //猜数字 while (1) { printf(请输入要猜的数字:); scanf(%d, i); if (i ret) { printf(猜大了\n); } else if (i ret) { printf(猜小了\n); } else { printf(恭喜 猜中了 数字是%d\n, ret); break; } } } 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; }当然也可以增加难度限制猜的次数限制。void HardGame() { //生成 1 到 100 的随机数 int i 0; int chance 5; int ret 0; ret (rand() % 100) 1; //猜数字 while (chance) { printf(\n还有%d次机会\n, chance); printf(请输入要猜的数字:); scanf(%d, i); if (i ret) { printf(猜大了\n); } else if (i ret) { printf(猜小了\n); } else { printf(恭喜 猜中了 数字是%d\n, ret); break; } chance--; } if (chance 0) { printf(次数用尽 挑战失败正确答案是%d\n, ret); } }以上内容如有错误或不准确之处欢迎指出或者你有更好的想法也欢迎交流。