C语言基础练习题 之 猜拳游戏程序

想找人玩猜拳游戏,没人玩,那就做个简单的人机猜拳系统。

第一步写头文件

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

下面用到

rand函数要#include <stdlib.h>;

srand函数要#include <time.h>;

sleep函数要#include <unistd.h>;(函数不知道在哪个库的时候没关系,大多数软件编译完会提醒,没提醒就只能自己搜了。

第二步写主函数

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    //内容
    return 0;
}

第三步声明两个整型变量 ch 和 sh

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{

    int ch, sh;

    return 0;
}

分别用于存储用户的选择和电脑的选择。(名字可以根据自己的喜好,我这就ch和sh)

第四步初始化随机数生成器

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    
    srand(time(NULL)); 

    int ch, sh;

    return 0;
}

使用当前时间作为种子,确保每次运行程序时生成的随机数序列不同。

第五步生成电脑的随机选择

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    
    srand(time(NULL)); 

    int ch, sh;

    sh = rand() % 3 + 1;

    return 0;
}

生成一个0到2之间的随机数,然后加1,使得 sh 的值在1到3之间,分别代表剪刀、石头、布。

第六步打印提示信息,让用户输入他们的选择并读取用户输入的整数值

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    
    srand(time(NULL)); 

    int ch, sh;

    sh = rand() % 3 + 1;

    sleep(1);

    printf("请输入选项(1.剪刀 2.石头 3.布):\n");
    scanf("%d", &ch);

    return 0;
}

将其存储在变量 ch 中。加个延时一秒(也可不加)。

第七步使用 switch 语句根据电脑的选择打印电脑选择的结果。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    
    srand(time(NULL)); 

    int ch, sh;

    sh = rand() % 3 + 1;

    sleep(1);

    printf("请输入选项(1.剪刀 2.石头 3.布):\n");
    scanf("%d", &ch);

    switch(sh)
    {
        case 1:
            printf("电脑选择剪刀\n");
            break;
        case 2:
            printf("电脑选择石头\n");
            break;
        case 3:
            printf("电脑选择布\n");
            break;
    }


    return 0;
}

第八步判断用户和电脑的选择。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    
    srand(time(NULL)); 

    int ch, sh;

    sh = rand() % 3 + 1;

    sleep(1);

    printf("请输入选项(1.剪刀 2.石头 3.布):\n");
    scanf("%d", &ch);

    switch(sh)
    {
        case 1:
            printf("电脑选择剪刀\n");
            break;
        case 2:
            printf("电脑选择石头\n");
            break;
        case 3:
            printf("电脑选择布\n");
            break;
    }
    
    
    if(ch == sh)
    {
        printf("平局\n");
    }
    else
    {
        if((ch == 1 && sh == 3) || (ch == 2 && sh == 1) || (ch == 3 && sh == 2))
        {
            printf("你赢了\n");
        }
        else
        {
            printf("你输了\n");
        }
    }

    return 0;
}
  1. if(ch == sh):这个条件判断玩家的选择(ch)是否和电脑的选择(sh)相同。

    如果相同,即玩家和电脑选择了相同的选项,则执行printf("平局\n");输出“平局”。
  2. else:如果玩家的选择和电脑的选择不相同,即进入这个分支。

    if((ch == 1 && sh == 3) || (ch == 2 && sh == 1) || (ch == 3 && sh == 2)):这个条件判断玩家是否赢了。使用了逻辑或(||)来检查三种赢的情况:
    • ch == 3 && sh == 2:玩家选择了布(3),电脑选择了石头(2),布赢石头。
    • ch == 2 && sh == 1:玩家选择了石头(2),电脑选择了剪刀(1),石头赢剪刀。
    • ch == 1 && sh == 3:玩家选择了剪刀(1),电脑选择了布(3),剪刀赢布。
    • 如果上述任何一个条件为真,即玩家赢了,执行printf("你赢了\n");输出“你赢了”。
    • else:如果上述所有赢的条件都不满足,执行printf("你输了\n");输出“你输了”。

第九步加个while循环和退出条件,即完整代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    srand(time(NULL)); // 初始化随机数生成器

    while(1)
    {
        int ch, sh;
        sh = rand() % 3 + 1;

        sleep(1); // 延时1秒

        printf("请输入选项(1.剪刀 2.石头 3.布):\n");
        scanf("%d", &ch);

        switch(sh)
        {
            case 1:
                printf("电脑选择剪刀\n");
                break;
            case 2:
                printf("电脑选择石头\n");
                break;
            case 3:
                printf("电脑选择布\n");
                break;
        }

        if(ch == sh)
        {
            printf("平局\n");
        }
        else
        {
            if((ch == 1 && sh == 3) || (ch == 2 && sh == 1) || (ch == 3 && sh == 2))
            {
                printf("你赢了\n");
            }
            else
            {
                printf("你输了\n");
            }
        }

        printf("按任意键继续,输入0退出游戏:\n");
        scanf("%d", &ch);
        if(ch == 0) 
        break;
    }

    return 0;
}

再次读取用户输入,如果用户输入0, break 语句会跳出循环,结束游戏。

人生能有几回搏,努力提升自己。

本人(大学生)学习较浅,如有错误请在评论区评论,我虚心接受。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值