编写剪刀(scissor)、石头(stone)、布(cloth)游戏

本文介绍了一个简单的石头剪刀布游戏程序实现,使用C语言完成用户交互与随机数生成,通过具体的代码示例展示了游戏逻辑及流程控制。

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int main(void)

{

    int user = -1;

    int computer = -1;

    int result = -1;

 

    char gesture[3][16] = {"scissor", "stone", "cloth"};

#if 0

    printf("%s\n", gesture[0]);

    printf("%s\n", gesture[1]);

    printf("%c\n", gesture[1][3]);

#endif

 

#if 1

    srand(time(NULL));

    while (1) {

        // 1. 提示用户进行输入

INPUT:

        printf("Please input thegesture (0 - scissor 1 - stone 2 - cloth 3 - quit):\n");

 

        // 2. 接收用户的输入

        scanf("%d", &user);

        if (user < 0 || user > 3) {

            goto INPUT;

        }

 

        if (user == 3) {

            break;       

        }

 

        // 3. 程序随机生成石头、剪刀、布中的一种

        computer = rand() % 3;

 

        // 4. 打印用户输入和电脑生成的信息

        printf("\nYour:%s\tComputer: %s\n", gesture[user], gesture[computer]);

        // 5. 根据游戏规则,判断输赢结果

        // (U - C + 4 ) % 3 - 1

        result = (user - computer + 4) % 3 - 1;

 

        // 6. 输出结果

        if (result > 0) {

            printf("You Win!\n");

        } else if (result == 0) {

            printf("Draw!\n");

        } else {

            printf("You lose!\n");

        }

    }

#endif

    printf("Thanks for yourjoin!\n");

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值