石头剪刀布游戏

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

#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("很遗憾,电脑赢得了比赛。");elseprintf("比赛结束,未分胜负。");return 0;
}

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/75922.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大数据系列之:Kerberos

大数据系列之&#xff1a;Kerberos 基本概念工作流程安全特性应用场景总结加密原理Kerberos认证流程更改您的密码授予账户访问权限票证管理Kerberos 票据属性使用 kinit 获取票据使用 klist 查看票据使用 kdestroy 销毁票据.k5identity 文件描述 Kerberos 是一种网络认证协议&a…

WPF 免费UI 控件HandyControl

示例效果和代码 直接可以用 Button 按钮 | HandyOrg 1.安装 , 输入 HandyControl 2.<!--配置HandyControl--> <!--配置HandyControl--> <ResourceDictionary Source"pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/> …

windows部署docker

1.下载docker 打开浏览器&#xff0c;访问 Docker Desktop 下载页面。 2.安装 Docker Desktop 运行安装程序&#xff1a; 双击下载的 Docker Desktop 安装包&#xff0c;启动安装程序。 选择安装选项&#xff1a; 按照屏幕上的指示进行操作。建议选择默认选项&#xff0c;包…

【Linux】远程登录时,使用图形界面报错:MoTTY X11 proxy: Unsupported authorisation protocol

1、问题描述 使用 MobaXterm 远程登录Ubuntu后,使用sudo权限运行图形界面程序报错: MoTTY X11 proxy: Unsupported authorisation protocol (gpartedbin:10518): Gtk-WARNING **: 22:01:34.377: cannot open display: localhost:10.02、查看SSH配置 修改 SSH 服务端配置,…

解决 Hugging Face SentenceTransformer 下载失败的完整指南:ProxyError、SSLError与手动下载方案

问题背景 在使用 Hugging Face 的 SentenceTransformer 加载预训练模型 all-MiniLM-L6-v2 时&#xff0c;遇到了以下错误&#xff1a; 代理连接失败&#xff08;ProxyError / SSLError: KRB5_S_TKT_NYV&#xff09;大文件下载中断&#xff08;unexpected EOF while reading&a…

MySQL——DQL的单表查询

1、查询表中所有的字段&#xff08;列&#xff09; 语法&#xff1a;select * from 表名; * 是通配符&#xff0c;用来表示所有的字段&#xff08;列&#xff09;。 select 表示查询哪些列。 from 表示从哪张表中查询。 2、查询表中指定的字段 语法&#xff1a;select 列…

开源RuoYi AI助手平台的未来趋势

近年来&#xff0c;人工智能技术的迅猛发展已经深刻地改变了我们的生活和工作方式。 无论是海外的GPT、Claude等国际知名AI助手&#xff0c;还是国内的DeepSeek、Kimi、Qwen等本土化解决方案&#xff0c;都为用户提供了前所未有的便利。然而&#xff0c;对于那些希望构建属于自…

[WUSTCTF2020]CV Maker1

进来是个华丽的界面&#xff0c;我们先跟随这个网页创造一个用户 发现了一个上传端口&#xff0c;尝试上传一个php文件并抓包 直接上传进不去&#xff0c;加个GIF89A uploads/d41d8cd98f00b204e9800998ecf8427e.php 传入 并且报告了 上传路径&#xff0c;然后使用蚁剑连接

Spring 中的 IOC

&#x1f331; 一、什么是 IOC&#xff1f; &#x1f4d6; 定义&#xff08;通俗理解&#xff09;&#xff1a; IOC&#xff08;Inversion of Control&#xff0c;控制反转&#xff09; 是一种设计思想&#xff1a;对象不再由你自己创建和管理&#xff0c;而是交给 Spring 容器…

Vue2-实现elementUI的select全选功能

文章目录 使用 Element UI 的全选功能自定义选项来模拟全选 在使用 Element UI 的 el-select组件时&#xff0c;实现“全选”功能&#xff0c;通常有两种方式&#xff1a;一种是使用内置的全选功能&#xff0c;另一种是通过自定义选项来模拟全选。 使用 Element UI 的全选功能…

小菜Go:Ubuntu下Go语言开发环境搭建

前置要求Ubuntu环境搭建 文章推荐 此处推荐一个比较好的文章&#xff0c;基本按部就班就欧克~ 安装虚拟机&#xff08;VMware&#xff09;保姆级教程&#xff08;附安装包&#xff09;_vmware虚拟机-CSDN博客 安装可能遇到的问题 虚拟机安装遇到的问题如&#xff1a;Exception…

安卓中app_process运行报错Aborted,怎么查看具体的报错日志

我在pc端生成了一个jar包&#xff0c;可以正常执行&#xff0c;但是导入到安卓的/data/local/tmp下面执行就会报错 执行命令如下&#xff1a; adb shell cd /data/local/tmp app_process -Djava.class.path/data/local/tmp/demo.jar /data/local/tmp com.example.demo.Hello然…

Python 面向对象 - 依赖倒置原则 (DIP)

1. 核心概念 依赖倒置原则(Dependency Inversion Principle, DIP) 是SOLID原则中的"D"&#xff0c;包含两个关键点&#xff1a; 高层模块不应依赖低层模块&#xff0c;二者都应依赖抽象抽象不应依赖细节&#xff0c;细节应依赖抽象 2. 使用场景 典型应用场景 系…

centos7 yum install docker 安装错误

1、错误信息&#xff1a; [rootlocalhost atguigu]# yum install docker 已加载插件&#xff1a;fastestmirror, langpacks Repository base is listed more than once in the configuration Loading mirror speeds from cached hostfile Could not retrieve mirrorlist http:…

【Gorm】模型定义

intro package mainimport ("gorm.io/gorm""gorm.io/driver/sqlite" // GORM 使用该驱动来连接和操作 SQLite 数据库。 )type Product struct {gorm.Model // 嵌入GORM 内置的模型结构&#xff0c;包含 ID、CreatedAt、UpdatedAt、DeletedAt 四个字段Cod…

R语言从专家到小白

文章目录 下载安装R下载安装R StudioCRAN 下载安装R Index of /bin https://cran.r-project.org/ 下载安装R Studio https://posit.co/download/rstudio-desktop/ CRAN R综合档案网络。 CRAN 镜像是一个提供 R 语言软件和包的在线服务&#xff0c;用户可以从不同的地区选择…

Java的Selenium的特殊元素操作与定位之时间日期控件

分为两种情况: 控件没有限制手动输入&#xff0c;则直接调用sendKeys方法写入时间数据 //时间日期控件处理 chromeDriver.get ("https://www,fliggy,com/?ttidsem.000000736&hlreferidbaidu.082076&route sourceseo"); chromeDriver.findElement (By.xpat…

38常用控件_QWidget的enable属性(2)

实现用另一个按钮切换之前按钮的“可用”状态 在同一个界面中,要求不同的控件的 objectName 也是必须不同的.(不能重复&#xff09; 后续就可以通过 ui->objectName 方式来获取到对应的控件对象了 ui->pushButton // 得到了第一个按钮对应的对象 ui->pushButton 2 //…

【Linux学习笔记】初识进程概念和进程PCB

【Linux学习笔记】初识冯诺依曼体系和进程PCB &#x1f525;个人主页&#xff1a;大白的编程日记 &#x1f525;专栏&#xff1a;Linux学习笔记 文章目录 【Linux学习笔记】初识冯诺依曼体系和进程PCB前言一. 冯诺依曼体系结构1.1 关于冯诺依曼体系的要点&#xff1a; 二. 操…

7.3 主成分分析(PCA)

一、协方差矩阵 这节是介绍 SVD 在统计和数据分析中的一个主要应用&#xff0c;即主成分分析。例子来自于人类的基因组&#xff0c;脸部识别和金融&#xff0c;目的是理解一个大的数据矩阵&#xff08;测量值&#xff09;。对于 n n n 个样本&#xff0c;我们每个测量 m m m…