步步为营-05-面向对象简单实例
说明:涉及知识点:winform窗体,
1 首先大致的UI页面做好
2 面向对象分析
需要三个对象(电脑,玩家,裁判),
对象对应方法(出拳,出拳,判断)
2.1 首先定义电脑;
电脑随机出拳,使用到随机方法.其次要把随机数转化成具体出拳内容,通过属性值显示个UI
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FigerPlay { public class Computer { //获取出的内容,用于显示给UI private string fist; public string Fist { get { return fist; } set { fist = value; } } public int ShowFist() { //获取随机数--用于判断输赢 int computerResult = new Random().Next(1,4); switch (computerResult) { case 1: this.Fist = "剪刀"; break; case 2: this.Fist = "石头"; break; case 3: this.Fist = "布"; break; default : this.Fist = "未知异常"; break; } return computerResult; } } }
2.2 定义玩家
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FigerPlay { public class Player { private string fist; public string Fist { get { return fist; } set { fist = value; } } public int ShowFist(string fistContent) { this.Fist = fistContent; int computerResult = 0; switch (fistContent) { case "剪刀": computerResult = 1; break; case "石头": computerResult = 2; break; case "布": computerResult = 3; break; } return computerResult; } } }
2.3 定义裁判
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FigerPlay { public class Referee { public static string ShowResult(int a, int b) { string resultStr = string.Empty; int result = a - b; if (result == 1 || result == -2) { resultStr = "玩家胜"; } if (result == 0) { resultStr = "平"; } if (result == -1 || result == 2) { resultStr = "电脑胜"; } return resultStr ; } } }
2.4 mian方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 FigerPlay { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //当点击"按钮"是触发事件 private void btnScissors_Click(object sender, EventArgs e) { //1-调用用户的ShowFist方法:传递一个参数,返回一个int string result = btnScissors.Text; PlayGame(result); } private void btnStone_Click(object sender, EventArgs e) { //1-调用用户的ShowFist方法:传递一个参数,返回一个int string result = btnStone.Text; PlayGame(result); } private void btnCloth_Click(object sender, EventArgs e) { string result = btnCloth.Text; PlayGame(result); } private void PlayGame(string resu) { //1-调用用户的ShowFist方法:传递一个参数,返回一个int Player p = new Player(); string playerContent = resu; int playerResult = p.ShowFist(playerContent); //2-调用电脑的ShowFist方法 Computer c = new Computer(); int computerResult = c.ShowFist(); //3-调用裁判的比较方法 string result = Referee.ShowResult(playerResult, computerResult); //4-输出结果 lblComputerResult.Text = c.Fist; lblPlayResult.Text = p.Fist; lblShowResult.Text = result; } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步