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

Winform实现石头剪刀布小游戏

1、电脑玩家类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RockScissorsClothApp
{public class Computer{public Card Play(){Random random = new Random();int num = random.Next(0, 3);switch (num){case 0:return Card.Rock;case 1:return Card.Scissors;default:return Card.CLoth;}}public static string ToString(Card c){return c.ToString();}}public enum Card{Rock,Scissors,CLoth}
}

2、玩家类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RockScissorsClothApp
{public class Player{public Card Play(string name){switch (name){case "石头":return Card.Rock;case "剪刀":return Card.Scissors;case "布":default:return Card.CLoth;}}}
}

3、裁判类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RockScissorsClothApp
{public class Referee{public static Result Judge(Card c1, Card c2){int dif = c1 - c2;switch (dif){case 0:return Result.Draw;case -1:case 2:return Result.lost;case -2:case 1:default:return Result.Win;}}public static string ToString(Result c){return c.ToString();}}public enum Result{Win,lost,Draw}
}

4、应用程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace RockScissorsClothApp
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void BtnRock_Click(object sender, EventArgs e){Button button = sender as Button;if (button != null){Computer computer = new Computer();Card c1 = computer.Play();lblComputerCard.Text = Computer.ToString(c1);string str = button.Text;Player player = new Player();Card c2 = player.Play(str);lblPlayerCard.Text = Computer.ToString(c2);Result result = Referee.Judge(c1, c2);lblRefereeResult.Text = result.ToString();}}}
}

5、运行效果
在这里插入图片描述
裁判结果为玩家的输赢。

http://www.lryc.cn/news/435868.html

相关文章:

  • 计算机的错误计算(九十)
  • 对游戏语音软件Oopz遭遇DDoS攻击后的一些建议
  • 解锁Android开发利器:MVVM架构_android的mvvm
  • llama.cpp demo
  • OpenCV结构分析与形状描述符(19)查找二维点集的最小面积外接旋转矩形函数minAreaRect()的使用
  • [SWPU2019]Web1 超详细教程
  • 【区块链通用服务平台及组件】基于向量数据库与 LLM 的智能合约 Copilot
  • mfc140u.dll丢失有啥方法能够进行修复?分享几种mfc140u.dll丢失的解决办法
  • 【PyQt6 应用程序】在用户登录界面实现密码密文保存复用
  • 赋能百业:多模态处理技术与大模型架构下的AI解决方案落地实践
  • 游戏论坛网站|基于Springboot+vue的游戏论坛网站系统游戏分享网站(源码+数据库+文档)
  • 【go】pprof 性能分析
  • Python | Leetcode Python题解之第397题整数替换
  • JDBC使用
  • 633. 平方数之和-LeetCode(C++)
  • Linux shell编程学习笔记79:cpio命令——文件和目录归档工具(下)
  • 《 C++ 修炼全景指南:七 》优先级队列在行动:解密 C++ priority_queue 的实现与应用
  • 通信工程学习:什么是HSS归属用户服务器
  • mysql workbench 如何访问远程数据库
  • ICMAN触摸感应芯片方案
  • 面向个小微型企业的开源大模型(Qwen2等)商业化, AI部署成本分析与优化策略(费用分析、资源消耗分析)
  • pandas判断一列中存在nan值
  • 如何将 Electron 项目上架 Apple Store
  • R语言统计分析——功效分析2(t检验,ANOVA)
  • android 侧滑返回上一界面备忘
  • golang学习笔记18——golang 访问 mysql 数据库全解析
  • 苹果账号登录后端验证两种方式 python2
  • FlinkCDC 3.2.0 新增优点 Pattern Replacement in routing rules
  • 《 C++ 修炼全景指南:六 》深入探索 C++ 标准库中的 stack 与 queue 容器适配器
  • 高级java每日一道面试题-2024年9月07日-JVM篇-说一下类加载的执行过程?