.Net基础加强02
猜拳游戏:
玩家类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 猜拳游戏 { public class Player { //用来存储结果的属性 public string FistName { get; set; } //出拳的方法 1表示剪刀 2表示石头 3表示布 public int ShowFist(string fist) { int result = -1; this.FistName = fist; switch (fist) { case "剪刀": result = 1; break; case "石头": result = 2; break; case "布": result = 3; break; } return result; } } }
计算机类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 猜拳游戏 { public class Computer { public string FistName { get; set; } //计算机的出拳方式 public int ShowFist() { Random rdm = new Random(); int result = rdm.Next(1, 4);// [1,4) switch(result) { case 1: this.FistName="剪刀"; break; case 2: this.FistName="石头"; break; case 3: this.FistName="布"; break; } return result; } } }
裁判类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 猜拳游戏 { public class CaiPan { public string PanDuanShuYing(int playerFist, int computerFist) { if (playerFist == computerFist) { return "棋逢对手"; } if (playerFist - computerFist == -2 || playerFist - computerFist == 1) { return "恭喜你获胜"; } else { return "下次加油!"; } } } }
Form代码:
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 猜拳游戏 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //用户单击剪刀 private void btnjiandao_Click(object sender, EventArgs e) { // new一个玩家对象 Player P1 = new Player(); // 调用玩家出拳 int playerFist=P1.ShowFist("剪刀"); lblPlayer.Text = P1.FistName; // new一个计算机对象 Computer computer = new Computer(); //调用计算机出拳 int computerFist = computer.ShowFist(); lblComputer.Text = computer.FistName; //调用裁判 CaiPan cp = new CaiPan(); lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist); } private void btnshitou_Click(object sender, EventArgs e) { // new一个玩家对象 Player P1 = new Player(); // 调用玩家出拳 int playerFist = P1.ShowFist("石头"); lblPlayer.Text = P1.FistName; // new一个计算机对象 Computer computer = new Computer(); //调用计算机出拳 int computerFist = computer.ShowFist(); lblComputer.Text = computer.FistName; //调用裁判 CaiPan cp = new CaiPan(); lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist); } private void btnbu_Click(object sender, EventArgs e) { // new一个玩家对象 Player P1 = new Player(); // 调用玩家出拳 int playerFist = P1.ShowFist("布"); lblPlayer.Text = P1.FistName; // new一个计算机对象 Computer computer = new Computer(); //调用计算机出拳 int computerFist = computer.ShowFist(); lblComputer.Text = computer.FistName; //调用裁判 CaiPan cp = new CaiPan(); lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist); } } }
提取方法,封装Form代码:
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 猜拳游戏 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //用户单击剪刀 private void btnjiandao_Click(object sender, EventArgs e) { StartGame("剪刀"); } private void btnshitou_Click(object sender, EventArgs e) { StartGame("石头"); } private void btnbu_Click(object sender, EventArgs e) { StartGame("布"); } private void StartGame(string item) { // new一个玩家对象 Player P1 = new Player(); // 调用玩家出拳 int playerFist = P1.ShowFist("item"); lblPlayer.Text = item; // new一个计算机对象 Computer computer = new Computer(); //调用计算机出拳 int computerFist = computer.ShowFist(); lblComputer.Text = computer.FistName; //调用裁判 CaiPan cp = new CaiPan(); lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist); } } }
或者把三个按钮都设置成 btnjiandao_Click事件,代码如下 同样能实现
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 猜拳游戏 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //用户单击剪刀 private void btnjiandao_Click(object sender, EventArgs e) { Button btn = (Button)sender; StartGame(btn.Text); } private void StartGame(string item) { // new一个玩家对象 Player P1 = new Player(); // 调用玩家出拳 int playerFist = P1.ShowFist("item"); lblPlayer.Text = item; // new一个计算机对象 Computer computer = new Computer(); //调用计算机出拳 int computerFist = computer.ShowFist(); lblComputer.Text = computer.FistName; //调用裁判 CaiPan cp = new CaiPan(); lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。