C语言---猜数字游戏

打印 上一主题 下一主题

主题 966|帖子 966|积分 2898

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
猜数字游戏代码

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. void meun()
  5. {
  6.         printf("**********************\n");
  7.         printf("******* 1.play *******\n");
  8.         printf("******* 0.quit *******\n");
  9.         printf("**********************\n");
  10. }
  11. void game()
  12. {
  13.         int rand_number = rand() % 100 + 1;//随机数的范围是1~100
  14.         int input2 = 0;
  15.         int right = 100;//最大是100
  16.         int left = 1;//最小是1
  17.         int count = 5;//猜数字机会
  18.         printf("猜数字游戏开始\n开始猜数字\n");
  19.         while (count)
  20.         {
  21.                 printf("请输入:");
  22.                 scanf("%d", &input2);
  23.                 if (input2 > rand_number)
  24.                 {
  25.                         count--;
  26.                         right = input2;
  27.                         printf("猜大了,范围是 %d--%d\n",left,right);
  28.                         printf("你还有%d机会\n", count);
  29.                 }
  30.                 if (input2 < rand_number)
  31.                 {
  32.                         count--;
  33.                         left = input2;
  34.                         printf("猜小了,范围是 %d--%d\n", left, right);
  35.                         printf("你还有%d机会\n", count);
  36.                 }
  37.                 if (input2 == rand_number)
  38.                 {
  39.                         printf("恭喜你猜对了\n");
  40.                         break;
  41.                 }
  42.         }
  43.         if (count == 0)
  44.         {
  45.                 printf("游戏失败\n");
  46.                 printf("要猜的数字是%d", rand_number);
  47.         }
  48. }
  49. int main()
  50. {
  51.         srand((unsigned int)time(NULL));
  52.         meun();
  53. regain:
  54.         printf("请选择:");
  55.         int input1 = 0;
  56.         scanf("%d", &input1);
  57.         switch (input1)
  58.         {
  59.         case 1:
  60.         {
  61.                 printf("进入游戏\n");
  62.                 game();
  63.                 break;       
  64.         }
  65.         case 0:
  66.         {
  67.                 printf("退出游戏\n");
  68.                 break;
  69.         }
  70.         default:
  71.         {
  72.                 printf("输入错误,请重新输入\n");
  73.                 goto regain;
  74.         }
  75.         }
  76.         return 0;
  77. }
复制代码
猜数字游戏思绪

1.菜单
2.玩游戏还是退出游戏
3.游戏–设置随机数
4.猜数字
1.菜单

  1. void meun()
  2. {
  3.         printf("**********************\n");
  4.         printf("******* 1.play *******\n");
  5.         printf("******* 0.quit *******\n");
  6.         printf("**********************\n");
  7. }
复制代码
提示玩家输入1表示玩游戏,输入0表示退出游戏
2.玩游戏还是退出游戏

运用 switch语句实现
  1. regain:
  2.         printf("请选择:");
  3.         int input1 = 0;
  4.         scanf("%d", &input1);
  5.         switch (input1)
  6.         {
  7.         case 1:
  8.         {
  9.                 printf("进入游戏\n");
  10.                 game();
  11.                 break;       
  12.         }
  13.         case 0:
  14.         {
  15.                 printf("退出游戏\n");
  16.                 break;
  17.         }
  18.         default:
  19.         {
  20.                 printf("输入错误,请重新输入\n");
  21.                 goto regain;
  22.         }
  23.         }
复制代码
input1 = 1进入游戏
input = 0退出游戏
input1等于其他数字时提示输入错误,重新输入
重新输入用goto语句实现,在上面这个代码中,会跳到regain这里
3.游戏–设置随机数

使用rand()函数设置随机数,为确保每次运行步伐时天生的随机数序列不同,通常需要使用srand()函数来设置随机数种子
srand()函数使用time(NULL)作为种子,意味着随机数种子会根据当前时间变革,从而使得每次运行步伐天生的随机数序列不同
rand() srand()的头文件是stdlib.h
time()的头文件是time.h
  1. 1 srand((unsigned int)time(NULL));
  2. 2 int rand_number = rand() % 100 + 1;//随机数的范围是1~100
复制代码
4.猜数字

判断输入的数与随机数那个大,输入的数比较大就输出猜大了,输入的数比较小就输出猜小了,相等就输出猜对了,并且停止循环(运用break)
  1. void game()
  2. {
  3.         int rand_number = rand() % 100 + 1;//随机数的范围是1~100
  4.         printf("猜数字游戏开始\n开始猜数字\n");
  5.         while (1)
  6.         {
  7.                 printf("请输入:");
  8.                 scanf("%d", &input2);
  9.                 if (input2 > rand_number)
  10.                 {
  11.                         printf("猜大了\n");
  12.                 }
  13.                 if (input2 < rand_number)
  14.                 {
  15.                         printf("猜小了"\n);
  16.                 }
  17.                 if (input2 == rand_number)
  18.                 {
  19.                         printf("恭喜你猜对了\n");
  20.                         break;
  21.                 }
  22.         }
  23. }
复制代码
到这猜数字的基本思绪就完成了
当然还可以改进,好比显树模围,多少次机会等等
left = 1 right = 100表示随机数的范围是1~100
count = 5表示有五次猜数字的机会
假设输入的input2 = 45,假如猜大了,让right = input2范围就变成了left -- right即1 -- 45;假如猜小了,让left = input2 范围就变成了left -- right即45 -- 100
每次猜完机会减一,即count--,当count == 0时,机会为0,游戏竣事
  1. void game()
  2. {
  3.         int rand_number = rand() % 100 + 1;//随机数的范围是1~100
  4.         int input2 = 0;
  5.         int right = 100;//最大是100
  6.         int left = 1;//最小是1
  7.         int count = 5;//猜数字机会
  8.         printf("猜数字游戏开始\n开始猜数字\n");
  9.         while (count)
  10.         {
  11.                 printf("请输入:");
  12.                 scanf("%d", &input2);
  13.                 if (input2 > rand_number)
  14.                 {
  15.                         count--;
  16.                         right = input2;
  17.                         printf("猜大了,范围是 %d--%d\n",left,right);
  18.                         printf("你还有%d机会\n", count);
  19.                 }
  20.                 if (input2 < rand_number)
  21.                 {
  22.                         count--;
  23.                         left = input2;
  24.                         printf("猜小了,范围是 %d--%d\n", left, right);
  25.                         printf("你还有%d机会\n", count);
  26.                 }
  27.                 if (input2 == rand_number)
  28.                 {
  29.                         printf("恭喜你猜对了\n");
  30.                         break;
  31.                 }
  32.         }
  33.         if (count == 0)
  34.         {
  35.                 printf("游戏失败\n");
  36.                 printf("要猜的数字是%d", rand_number);
  37.         }
  38. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦见你的名字

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表