当前位置: 首页 > news >正文

石头剪刀布游戏

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

#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((unsigned)time(NULL)); // 利用随机数保证每次电脑出的结果不同
    return rand() % 3 + 1; // rand % 3 的范围是0-2, 加1 是 1-3,满足猜拳范围。
}
void printChoice(int choice, const char* prefix) {
    switch (choice) {
    case Rock:
        printf("%s石头", prefix);
        break;
    case Paper:
        printf("%s布", prefix);
        break;
    case Scissors:
        printf("%s剪刀", prefix);
        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 一个函数实现所有

相关文章:

  • linux sudo不需要输入密码
  • 通过 itms-services 协议下载安装 IOS 应用
  • Shiro学习(六):Shiro整合CAS实现单点登录
  • JSON 是什么?通俗详解
  • Opencv计算机视觉编程攻略-第十节 估算图像之间的投影关系
  • 【力扣hot100题】(059)单词搜索
  • 华为IP(4)
  • 面试自我介绍
  • 介绍几种创意登录页(含完整源码)
  • Go语言-初学者日记(五):文件操作与 JSON 实战
  • PHP 项目搭建 ELK 日志监控体系完整指南
  • 第三方检测报告—科技成果鉴定测试
  • Ansible YAML 基础语法与关键词 的详细指南
  • 网络编程—TCP/IP模型(IP协议)
  • 若依系统弹窗父子页面传参数
  • 45.跳跃游戏 II
  • 网络编程—TCP/IP模型(数据链路层了解与知识补充)
  • DApp实战篇:前端技术栈一览
  • [ Redis ] | 初识Redis
  • 【数据结构】图的存储
  • 西安网站建设哪家公司好/宁德seo优化
  • 微网站定制/中文网站排行榜
  • 丰县数据网站建设多少钱/二次感染即将大爆发
  • 公司想为一个产品做多个网站/网址收录平台
  • 做3d打印网站/互联网营销师是干什么
  • 如何用css做网站/国际新闻今天