【水文】石头剪刀步

代码:

#include 
#include 
#include 
 
int main()
{
    int player, computer;
    srand((unsigned int)time(NULL));
 
    printf("石头剪刀布游戏开始了!\n");
    printf("请猜拳:\n");
    printf("1. 石头\n");
    printf("2. 剪刀\n");
    printf("3. 布\n");
 
    // 玩家输入选择,输入非数字类型退出
    while ((scanf("%d", &player)) == 1)
    { 
 
        if (player < 1 || player > 3)
        {
            printf("输入无效,请重新运行游戏并输入正确的选择。\n");
            return 0;
        }
 
        computer = rand() % 3 + 1; // 生成1~3的随机数
 
        printf("你选择了:");
        switch (player)
        {
        case 1:
            printf("石头\n");
            break;
        case 2:
            printf("剪刀\n");
            break;
        case 3:
            printf("布\n");
            break;
        }
 
        printf("电脑选择了:");
        switch (computer)
        {
        case 1:
            printf("石头\n");
            break;
        case 2:
            printf("剪刀\n");
            break;
        case 3:
            printf("布\n");
            break;
        }
 
        if (player == computer)
        {
            printf("平局!\n\n");
        }
        else if ((player == 1 && computer == 2) || (player == 2 && computer == 3) || (player == 3 && computer == 1))
        {
            printf("你赢了!\n\n");
        }
        else
        {
            printf("你输了!\n\n");
        }
 
        printf("石头剪刀布游戏开始了!\n");
        printf("请猜拳:\n");
        printf("1. 石头\n");
        printf("2. 剪刀\n");
        printf("3. 布\n");
    }
 
    return 0;
}

你可能感兴趣的:(C)