一、游戏要求
1.电脑⾃动⽣成1~100的随机数
2. 玩家猜数字,猜数字的过程中,根据猜测数据的⼤⼩给出⼤了或⼩了的反馈,直到猜对,游戏结束
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
while (1)
{
printf("请猜数字>:");
scanf_s("%d", &guess);
if (guess < r)
{
printf("猜小了\n");
}
else if (guess > r)
{
printf("猜大了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
void me()
{
printf("***********************\n");
printf("****** 1. play ******\n");
printf("****** 0. exit ******\n");
printf("***********************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
me();
printf("请选择:>");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
一、菜单部分
void meu()
{
printf(".........................\n");
printf("..........1.开始.........\n");
printf("..........0.结束.........\n");
printf(".........................\n");
}
meu()
函数是一个简单的 C 语言函数,它使用 printf
语句打印出一些文本信息。这个函数没有参数,也没有返回值,它的作用可能是为了在控制台显示一个简单的菜单,让用户选择开始或结束某个过程。
二、游戏部分
void game()
{
int count = 5;
int r = rand() % 100 + 1; // 取余0~99+1
int gusee = 0;
while (1)
{
if (count > 0)
{
printf("请输入数字:");
scanf("%d", &gusee);
if (gusee > r)
{
printf("猜大了\n");
}
else if (gusee < r)
{
printf("猜小了\n");
}
else
{
printf("恭喜你猜对了\n");
break;
}
count--;
printf("你还有%d次机会",count);
}
else
{
printf("游戏结束\n");
printf("正确值是%d\n",r);
break;
}
}
}
首先我们需要生成1~100的随机数
C语⾔提供了⼀个函数叫 rand,这函数是可以⽣成随机数的,函数原型如下所⽰:
int rand (void);
rand函数会返回⼀个伪随机数,这个随机数的范围是在0~RAND_MAX之间,这个RAND_MAX的⼤⼩是 依赖编译器上实现的,但是⼤部分编译器上是32767。
rand函数的使⽤需要包含⼀个头⽂件是:stdlib.h
int r = rand() % 100 + 1; // 取余0~99+1
其次根据猜测数据的⼤⼩给出⼤了或⼩了的反馈,在这里我们运用嵌套语句
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
while (1)
{
printf("请猜数字>:");
scanf_s("%d", &guess);
if (guess < r)
{
printf("猜小了\n");
}
else if (guess > r)
{
printf("猜大了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
创造一个while(1)循环语句,再判断所输入值和随机数生成值的大小,直到猜对为止,break跳出循环。
三、main()主函数
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
me();
printf("请选择:>");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
1、srand(time(NULL));
是 C 语言中常用的一行代码,用于初始化随机数生成器。
其实rand函数⽣成的随机数是伪随机的,伪随机数不是真正 的随机数,是通过某种算法⽣成的随 机数。真正的随机数的是⽆法预测下⼀个值是多少的。⽽rand函 数是对⼀个叫“种⼦”的基准值进⾏运算⽣成的随机数。
C语⾔中⼜提供了⼀个函数叫 srand,⽤来初始化随机数的⽣成器的,srand的原型如下:
void srand (unsigned int seed);
程序中在调⽤ rand 函数之前先调⽤ srand 函数,通过 srand 函数的参数seed来设置rand函数⽣成随 机数的时候的种⼦,只要种⼦在变化,每次⽣成的随机数序列也就变化起来了。 那也就是说给srand的种⼦是如果是随机的,rand就能⽣成随机数;在⽣成随机数的时候⼜需要⼀个随 机数,这就⽭盾了。
在程序中我们⼀般是使⽤程序运⾏的时间作为种⼦的,因为时间时刻在发⽣变化的。 在C语⾔中有⼀个函数叫 time ,就可以获得这个时间,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
四、如果加上猜测还剩下的次数
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void meu()
{
printf(".........................\n");
printf("..........1.开始.........\n");
printf("..........0.结束.........\n");
printf(".........................\n");
}
void game()
{
int count = 5;
int r = rand() % 100 + 1; // 取余0~99+1
int gusee = 0;
while (1)
{
if (count > 0)
{
printf("请输入数字:");
scanf("%d", &gusee);
if (gusee > r)
{
printf("猜大了\n");
}
else if (gusee < r)
{
printf("猜小了\n");
}
else
{
printf("恭喜你猜对了\n");
break;
}
count--;
printf("你还有%d次机会",count);
}
else
{
printf("游戏结束\n");
printf("正确值是%d\n",r);
break;
}
}
}
int main()
{
int input = 0;
srand(time(NULL));
do
{
meu();
printf("请选择:");
scanf("%d",&input);
switch(input)
{case 1:
game();
case 0:
printf("游戏结束\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}