问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

小游戏系列——猜数字

创作时间:
作者:
@小白创作中心

小游戏系列——猜数字

引用
CSDN
1.
https://blog.csdn.net/chir_yb/article/details/145534307

目录

  • 本篇目标
  • 1.随机数的生成
  • 1.1 rand
  • 1.2 srand
  • 1.3 time
  • 1.4 设置随机数的范围
  • 2.游戏过程
    1. 游戏菜单
  • 4.游戏主体
  • 5 猜数字游戏实现
  • 6增加难度
  • 结语

本篇目标

写一个猜数字游戏。
游戏要求:

  • 电脑自动生成1~100的一个随机数。
  • 在猜数字的过程中,根据猜测数据的大小给出大了或者小了的反馈,直到猜对,游戏结束。

1.随机数的生成

游戏的第一步,就是要产生随机数,那么怎样才能产生随机数呢?

1.1 rand

C语言提供了一个函数叫作rand,这个函数是可以生成随机数的,函数会返回一个伪随机数,这个随机数的范围在0~RAND_MAX之间,这个RAND_MAX的大小在每个编译器上可能不相同,大部分的编译器是32767。
rand函数的使用要包含一个头文件:stdlib.h。

代码展示:

#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;
}

上面是运行两次的结果,我们可以发现虽然每一次运行产生的数字是相对随机的,但是上一次和下一次运行结果却是一样的,这就有点问题了。


其实rand函数生成的随机数是伪随机数,不是真正的随机数,是通过一种算法生成的随机数,而真正的随机数是无法预测下一个值是多少的。rand函数是对一个叫种子的基准值进行运算生成的随机值。
之所以出现一样的结果,是因为rand函数生成随机数默认种子是1,那如果要生成不同的随机数,就要让种子发生变化。

1.2 srand

C语言又提供了一个函数叫srand,用来初始化随机数的生成器。
在程序中调用rand函数之前要先调用srand函数,通过srand的参数seed来设置rand函数生成随机数的时候的种子,只要种子在变化,每次生成的随机数也就变化。
那也就是说srand的参数seed如果是随机的,rand就能生成随机的随机数了,现在的问题是,要在生成随机数的时候需要一个随机数。

1.3 time

在程序中我们一般使用运行代码当时的时间作为种子,因为时间时时刻刻在变化的。
在C语言中有一个函数叫time,就是用来获得这个时间的。time函数会返回当前的时间,返回值是从1970年1月1日0时0分0秒到现在程序运行的时间之间的差值,单位为秒。time函数的参数timer如果是非NULL的指针的话,函数会将返回的差值放在timer指向的内存中带回去。如果timer是NULL,就只返回这个差值,time函数返回的这个时间差称为时间戳。time函数的使用需要包含头文件time.h。
如果只是让time函数返回时间戳,可以这样写:

代码展示:

#include <stdio.h>
#include <time.h>
int main()
{
    time(NULL);
    return 0;
}

我们改变了生成随机数的代码。

代码展示:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    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;
}

这时多运行几次看看,每次的运行结果就有差异了。
(注:截图只是当时的时间的结果,你的运行结果和上面的可能不一样。不要频繁使用。)

1.4 设置随机数的范围

如果我们要生成0~99之间的随机数。

代码展示:

rand()%100

如果我们要生成1~100之间的随机数。

代码展示:

rand()%100+1

如果我们要生成a~b之间的随机数。

代码展示:

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

2.游戏过程

游戏过程代码:

  1. 创造游戏开始界面(菜单)。
  2. 做出选择(退出还是开始)。
  3. 进行游戏(选择开始)。
    游戏解析:
    开始直接进入菜单,选择开始游戏还是退出游戏,在选择开始游戏后,进入游戏主体,这时生成一个随机数,但是不打印,玩家开始输入猜的数字,进行判断是大了还是小了,玩家不断缩小范围,循环往复,直到玩家猜对了,打印恭喜你,猜对了,游戏结束。
    注意:
  4. 菜单至少打印一遍,然后再选择开始游戏还是退出游戏,应该使用do-while循环。
  5. 游戏结束后,玩家还想玩,这就需要while循环了。

代码展示:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()//菜单
{
}
void game()//游戏主体
{
}
int main()
{
    int input;
    
    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;
}

3. 游戏菜单

代码展示:

void menu()
{
    printf("***********************\n");
    printf("******  1. play  ******\n");
    printf("******  0. exit  ******\n");
    printf("***********************\n");
}

4.游戏主体

代码展示:

int num = rand() % 100 + 1;
int guess = 0;
while (1)
{
    printf("请猜数字>:");
    scanf("%d", &guess);
    if (guess < num)
    {
        printf("猜⼩了\n");
    }
    else if (guess > num)
    {
        printf("猜⼤了\n");
    }
    else
    {
        printf("恭喜你,猜对了\n");
        break;
    }
}

5 猜数字游戏实现

代码展示:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
    printf("***********************\n");
    printf("******  1. play  ******\n");
    printf("******  0. exit  ******\n");
    printf("***********************\n");
}
void game()
{
    int num = rand() % 100 + 1;
    int guess = 0;
    while (1)
    {
        printf("请猜数字>:");
        scanf("%d", &guess);
        if (guess < num)
        {
            printf("猜⼩了\n");
        }
        else if (guess > num)
        {
            printf("猜⼤了\n");
        }
        else
        {
            printf("恭喜你,猜对了\n");
            break;
        }
    }
}
int main()
{
    int input;
    
    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;
}

6增加难度

限制猜数字的次数。

代码展示:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
    printf("***********************\n");
    printf("******  1. play  ******\n");
    printf("******  0. exit  ******\n");
    printf("***********************\n");
}
void game()
{
    int num = rand() % 100 + 1;
    int guess = 0;
    int count = 5;//新加的代码
    while (count)//改变的代码
    {
        printf("你还有%d次机会\n", count);//新加的代码
        printf("请猜数字>:");
        scanf("%d", &guess);
        if (guess < num)
        {
            printf("猜⼩了\n");
        }
        else if (guess > num)
        {
            printf("猜⼤了\n");
        }
        else
        {
            printf("恭喜你,猜对了\n");
            break;
        }
        count--;//新加的代码
    }
    if (count == 0);
    {
        printf("你失败了,正确结果是%d\n", num);
    }//新加的代码
}
int main()
{
    int input;
    
    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;
}

结语

OK,以上就是猜数字游戏的全部了,希望大家看完后能有所收获,一定要动手敲一敲代码,新手可能一遍有点困难,那就多看几遍,多敲几遍,欲戴王冠,必承其重。本人学识浅薄,如有错误,请大家指出,谢谢大家。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号