• 趣味C语言——【猜数字】小游戏


    🥰欢迎关注 轻松拿捏C语言系列,来和 小哇 一起进步!✊

    🎉创作不易,请多多支持🎉

    🌈感谢大家的阅读、点赞、收藏和关注💕

    🌹如有问题,欢迎指正 感谢


    目录

    代码:

    rand()

    srand()

    time()

    设置随机数范围

    🌟🌟产生a~b的随机数: 


     

    运用循环、选择语句和函数,我们可以制作一个简单的猜数字小游戏,

    假定游戏规则是给出一个1~100间的随机数,我们在限定次数中去猜数字

    代码:

    1. #include
    2. #include
    3. #include
    4. void menu()
    5. {
    6. printf("---------------------------------\n");
    7. printf("---------** 1.play game **-----\n");
    8. printf("---------** 0.exit game **-----\n");
    9. printf("---------------------------------\n");
    10. }
    11. int main()
    12. {
    13. int input;
    14. srand((unsigned int)time(NULL));//程序中只需要调用一次就可以了
    15. do
    16. {
    17. menu();
    18. int r = rand() % 100 + 1;
    19. scanf("%d", &input);
    20. printf("%d", r);
    21. switch (input)
    22. {
    23. case 1:
    24. printf("游戏开始---->");
    25. int count = 5;
    26. int guess = 0;
    27. while (count)
    28. {
    29. printf("你还有%d次机会\n", count);
    30. printf("请猜数字:\n");
    31. scanf("%d", &guess);
    32. if (guess > r)
    33. printf("猜大了\n");
    34. else if (guess < r)
    35. printf("猜小了\n");
    36. else
    37. {
    38. printf("恭喜你,猜对了\n");
    39. break;
    40. }
    41. count--;
    42. }
    43. if (count == 0)
    44. printf("全部猜错,游戏失败\n");
    45. break;
    46. case 0:
    47. printf("退出游戏!");
    48. break;
    49. default:
    50. printf("输入错误,请选择1或0\n");
    51. }
    52. } while (input);
    53. return 0;
    54. }

    运行截图: 

     这里讲一下有关随机数生成的代码:

    rand()

    int rand (void);

    使用要包含头文件 

    rand() 函数会返回一个伪随机数,伪随机数范围是0~RAND_MAX(大部分编译器上为32767)

    1. #include
    2. #include
    3. int main()
    4. {
    5. printf("%d\n", rand());
    6. printf("%d\n", rand());
    7. printf("%d\n", rand());
    8. printf("%d\n", rand());
    9. printf("%d\n", rand());
    10. return 0;
    11. }

    第一次运行:

     第二次运行:

    可以看到两次运行产生的随机数是一样的,这并没有实现真正的随机。

    这是因为rand()函数产生的随机数是根据一个叫种子的基准值来计算的,而rand函数的种子默认为1。

    所以要产生随机数我们还需要让种子也变化起来,

    这里就需要srand()函数了

    srand()

    使用要包含头文件 

    void srand (unsigned int seed);

    在调用rand函数前调用srand函数,通过srand函数的参数seed可以设置rand函数的种子,使种子变化起来。

    srand函数通常不需要在程序中频繁调用。

    在大多数情况下,你只需要在程序开始时调用一次srand函数,来设置随机数生成器的种子。

    time()

    使用要包含头文件 

    time_t time (time_t* timer); 

    time函数会返回程序运行时间到1970年1月1日0时0分0秒的时间差(也叫时间戳)

    time函数的参数 timer 如果是非NULL的指针的话,函数也会将这个返回的差值放在timer指向的内存 中带回去。

    如果 timer 是NULL,就只返回这个时间的差值。

    所以我们可以搭配这三个函数使用来产生真正的随机数:

    srand((unsigned int)time(NULL));

    rand();

    设置随机数范围

    上面游戏中我们需要产生1~100的随机数,

    写的 int r = rand()%100 + 1;

    如果要产生0~99的随机数:

    rand()%100;

    产生100~200的随机数:

    100+rand()%101或写成100+rand()%(200-100+1)

    🌟🌟产生a~b的随机数: 

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


     🎉🎉🎉本文内容结束啦,希望各位大佬多多指教!

    🌹🌹感谢大家三连支持

    💕敬请期待下篇文章吧~

  • 相关阅读:
    python面向对象
    FPGA设计时序约束八、others类约束之Set_Case_Analysis
    selenium键盘鼠标事件ActionChains
    Vue安装过程及环境配置
    springboot+rocketmq(4):实现延时消息
    计算机网络 网络层设备的 冲突域和广播域
    Flask 学习100-Flask-SocketIO 结合 xterm.js 实现网页版Xshell
    Linux:systemctl常用命令
    Python例题练习1
    【HIT-OSLAB-1-操作系统的引导】
  • 原文地址:https://blog.csdn.net/kiku20231213/article/details/139721586