# ===== CodeLab: game-c-rps =====
# 以下代码片段按文章出现顺序拼接, 共 6 段

# ----- 片段 1 (c) -----
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* 电脑随机出拳:返回 1~3 */
int computer_choice(void) {
    return rand() % 3 + 1;
}

int main(void) {
    srand((unsigned)time(NULL));   /* 用当前时间做随机种子 */
    printf("电脑出拳:%d\n", computer_choice());
    return 0;
}

# ----- 片段 2 (c) -----
#include <stdio.h>

/* 判断玩家输入是否合法:0~3 */
int valid_choice(int p) {
    return p >= 0 && p <= 3;
}

int main(void) {
    printf("%d\n", valid_choice(2));   /* 合法 */
    printf("%d\n", valid_choice(9));   /* 非法 */
    printf("%d\n", valid_choice(-1));  /* 非法 */
    return 0;
}

# ----- 片段 3 (c) -----
#include <stdio.h>

/* result[玩家][电脑]:1 玩家赢, -1 电脑赢, 0 平局 */
static const int result[4][4] = {
    {0, 0, 0, 0},
    {0, 0, 1, -1},   /* 玩家出石头:赢剪刀,输布 */
    {0, -1, 0, 1},   /* 玩家出剪刀:输石头,赢布 */
    {0, 1, -1, 0},   /* 玩家出布:赢石头,输剪刀 */
};

int judge(int player, int computer) {
    return result[player][computer];
}

int main(void) {
    printf("石头vs剪刀:%d\n", judge(1, 2));
    printf("剪刀vs布:%d\n", judge(2, 3));
    printf("布vs石头:%d\n", judge(3, 1));
    return 0;
}

# ----- 片段 4 (c) -----
#include <stdio.h>

int main(void) {
    int round = 7, win = 4, lose = 2, draw = 1;

    printf("共 %d 局:胜 %d 负 %d 平 %d\n", round, win, lose, draw);
    /* 注意:必须写 100.0 而不是 100 */
    printf("胜率: %.1f%%\n", 100.0 * win / round);
    return 0;
}

# ----- 片段 5 (c) -----
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static const int result[4][4] = {
    {0, 0, 0, 0},
    {0, 0, 1, -1},
    {0, -1, 0, 1},
    {0, 1, -1, 0},
};

int computer_choice(void) {
    return rand() % 3 + 1;
}

int judge(int player, int computer) {
    return result[player][computer];
}

void show(int c) {
    switch (c) {
        case 1: printf("石头"); break;
        case 2: printf("剪刀"); break;
        case 3: printf("布");   break;
        default: break;
    }
}

int read_choice(void) {
    int p;
    while (1) {
        printf("输入 1(石头) 2(剪刀) 3(布) 0(退出): ");
        if (scanf("%d", &p) != 1) {
            /* 读不到整数:清空缓冲并返回 -1 表示异常 */
            while (getchar() != '\n')
                ;
            return -1;
        }
        if (p >= 0 && p <= 3)
            return p;
        printf("输入无效,请重新输入!\n");
    }
}

int main(void) {
    int round = 0, win = 0, lose = 0, draw = 0;
    srand((unsigned)time(NULL));

    printf("=== 石头剪刀布 ===\n");
    while (1) {
        int player = read_choice();
        if (player <= 0)      /* 0 退出,-1 表示输入异常 */
            break;

        int comp = computer_choice();
        printf("你出 ");
        show(player);
        printf(",电脑出 ");
        show(comp);
        printf("\n");

        int r = judge(player, comp);
        if (r == 1) {
            printf("你赢了!\n");
            win++;
        } else if (r == -1) {
            printf("你输了!\n");
            lose++;
        } else {
            printf("平局!\n");
            draw++;
        }
        round++;
    }

    printf("\n共 %d 局:胜 %d 负 %d 平 %d\n", round, win, lose, draw);
    if (round > 0)
        printf("胜率: %.1f%%\n", 100.0 * win / round);
    return 0;
}

# ----- 片段 6 (bash) -----
gcc rps.c -o rps
./rps
