石头剪刀布游戏

自己写的一个石头剪刀布游戏,如果有需要更改的地方请指出

//石头剪刀布①
#define _CRT_SECURE_NO_WARNINGS // scanf_s编写起来太过于麻烦,直接把这个警告关掉,便于编写。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义猜拳选项
#define Rock 1
#define Paper 2
#define Scissors 3
void rules() {
    printf("欢迎参加剪刀石头布游戏\n石头:1,布:2,剪刀:3\n");//先进行游戏介绍
    printf("提醒:因为srand函数是秒级的,请注意输入数字的时候不要过快哦,不然就会导致电脑只出一个招式。\n");//提醒srand的注意事项
}
int choice() {
    int choice;
    if (scanf("%d", &choice) != 1 || choice < 1 || choice > 3) {
        printf("输入无效,请输入1到3的数字。\n");
        return -1;
    }
    return choice;
}
int comchoice() {
    srand((int)time(NULL)); // 利用随机数保证每次电脑出的结果不同
    return rand() % 3 + 1; // rand % 3 的范围是0-2, 加1 是 1-3,满足猜拳范围。
}
void printChoice(int choice) {
    switch (choice) {
    case Rock:
        printf("石头");
        break;
    case Paper:
        printf("布");
        break;
    case Scissors:
        printf("剪刀");
        break;
    }
}
int winner(int userChoice, int comChoice) {
    if (userChoice == comChoice) {
        printf("平局!你和电脑都出了 ");
        printChoice(userChoice);
        return 0; // 平局返回0
    }
    else if (((userChoice == Rock && comChoice == Scissors) ||
        (userChoice == Paper && comChoice == Rock) ||
        (userChoice == Scissors && comChoice == Paper))) {
        printf("你赢了!你出了 ");
        printChoice(userChoice);
        printf(",电脑出了 ");
        printChoice(comChoice);
        return 1; // 用户赢返回1
    }
    else {
        printf("你输了!你出了 ");
        printChoice(userChoice);
        printf(",电脑出了 ");
        printChoice(comChoice);
        return -1; // 用户输返回-1
    }
}
int main() {
    int totalgames, win, userwin = 0, comwin = 0;
    rules();
    while (1) { // 循环直至获取有效输入
        printf("请输入比赛局数(总局数必须为整数且为奇数): ");
        if (scanf("%d", &totalgames) != 1 || totalgames % 2 == 0) { // 检查输入是否为整数以及是否为奇数
            printf("无效输入,总局数必须为奇数。\n");//输入无效,循环继续
        }
        break; // 成功获取有效输入,退出循环
    }
    win = (totalgames / 2) + 1;
    for (int i = 0; i < totalgames;) {
        printf("\n第%d局开始:\n", i + 1);
        int userChoice, computerChoice, result;
        while ((userChoice = choice()) == -1); // 确保输入有效
        computerChoice = comchoice();
        result = winner(userChoice, computerChoice);
        if (result == 1) {
            userwin++;
            i++; // 只有当结果不是平局时才增加局数计数器
        }
        else if (result == -1) {
            comwin++;
            i++; // 同上
        }
        // 如果一方达到获胜条件,则提前结束
        if (userwin >= win || comwin >= win)
            break;
    }
    printf("\n最终结果: ");
    if (userwin >= win)
        printf("恭喜你赢得了比赛!");
    else if (comwin >= win)
        printf("很遗憾,电脑赢得了比赛。");
    else
        printf("比赛结束,未分胜负。");
    return 0;
}
}

在这里插入图片描述
这是运行的结果。
可以自己写一写,很锻炼coding能力。

//石头剪刀布②
#define _CRT_SECURE_NO_WARNINGS // scanf_s编写起来太过于麻烦,直接把这个警告关掉,便于编写。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义猜拳选项
#define Rock 1
#define Paper 2
#define Scissors 3
void rules() {
    printf("欢迎参加剪刀石头布游戏\n石头:1,布:2,剪刀:3\n");//先进行游戏介绍
    printf("提醒:因为srand函数是秒级的,请注意输入数字的时候不要过快哦,不然就会导致电脑只出一个招式。\n");//提醒srand的注意事项
}
int choice() {
    int choice;
    if (scanf("%d", &choice) != 1 || choice < 1 || choice > 3) {
        printf("输入无效,请输入1到3的数字。\n");
        return -1;
    }
    return choice;
}
int comchoice() {
    srand((int)time(NULL));// 利用随机数保证每次电脑出的结果不同
    return rand() % 3 + 1; // rand % 3 的范围是0-2, 加1 是 1-3,满足猜拳范围。
}
int printAndDecideWinner(int userChoice, int comChoice) {
    if (userChoice == comChoice) {
        printf("平局!你和电脑都出了 ");
        switch (userChoice) {
        case Rock:
            printf("石头");
            break;
        case Paper:
            printf("布");
            break;
        case Scissors:
            printf("剪刀");
            break;
        }
        return 0;//返回0,便于在主函数时操作
    }
    else if (((userChoice == Rock && comChoice == Scissors) ||
        (userChoice == Paper && comChoice == Rock) ||
        (userChoice == Scissors && comChoice == Paper))) {
        printf("你赢了!你出了 ");
        switch (userChoice) {
        case Rock:
            printf("石头");
            break;
        case Paper:
            printf("布");
            break;
        case Scissors:
            printf("剪刀");
            break;
        }
        printf(",电脑出了 ");
        switch (comChoice) {
        case Rock:
            printf("石头");
            break;
        case Paper:
            printf("布");
            break;
        case Scissors:
            printf("剪刀");
            break;
        }
        return 1;//返回1到主函数操作
    }
    else {
        printf("你输了!你出了 ");
        switch (userChoice) {
        case Rock:
            printf("石头");
            break;
        case Paper:
            printf("布");
            break;
        case Scissors:
            printf("剪刀");
            break;
        }
        printf(",电脑出了 ");
        switch (comChoice) {
        case Rock:
            printf("石头");
            break;
        case Paper:
            printf("布");
            break;
        case Scissors:
            printf("剪刀");
            break;
        }
        return -1;//同理
    }
}
int main() {
    int totalgames, win, userwin = 0, comwin = 0;
    rules();
    while (true) {//一直循环直到获得有效输出
        printf("请输入比赛局数(总局数必须为整数且为奇数): ");
        if (scanf("%d", &totalgames) != 1 || totalgames % 2 == 0) {// 检查输入是否为整数以及是否为奇数
            printf("无效输入,总局数必须为奇数。\n");//输入无效,循环继续
        }
        else {
            break; //成功获取有效输入,退出循环
        }
    }
    win = (totalgames / 2) + 1;
    for (int i = 1; i <= totalgames;) {// 确保输入有效(如果return的值是-1的话,会一直让用户输入值)
        printf("\n第%d局开始:\n", i);
        int userchoice, computerchoice, result;
        userchoice = choice();
        computerchoice = comchoice();
        while (userchoice == -1) {
            userchoice = choice();
            computerchoice = comchoice();
        }
        result = printAndDecideWinner(userchoice, computerchoice);
        if (result == 1) {
            userwin++;
            i++;// 只有当结果不是平局时才增加局数计数器
        }
        else if (result == -1) {
            comwin++;
            i++; // 同上
        }
        if (userwin >= win || comwin >= win) // 如果一方达到获胜条件,那就提前结束。
            break;
    }
    printf("\n最终结果: ");
    if (userwin >= win)
        printf("恭喜你赢得了比赛!");
    else if (comwin >= win)
        printf("很遗憾,电脑赢得了比赛。");
    else
        printf("比赛结束,未分胜负。");
    return 0;
}

修改过的第二版方法。更通俗易懂,用switch case 一个函数实现所有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值