标签:
namespace 出拳游戏05 { //玩家类 class Player { //存储出拳的内容,显示到窗体中 public string FistName { get; set; } //出拳的方法 public int Play(string name) { this.FistName = name; switch (name) { case "石头": return 1; case"剪刀":return 2; case"布":return 3; } return 0; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 出拳游戏05 { //电脑类 class Computer { //存储出拳的内容 public string FistName { get; set; } public int Play() { Random r = new Random(); int num = r.Next(1, 4); switch (num) { case 1: this.FistName = "石头"; break; case 2: this.FistName = "剪刀"; break; case 3: this.FistName = "布"; break; } return num; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 出拳游戏05 { //裁判类 class CaiPan { public string WhoWin(int pNum, int cNum) { //1-1 = 0 平手 //1-2 = -1 玩家赢 //1-3 = -2 电脑赢 //2-1 = 1 电脑赢了 //2-2 = 0 平手 //2-3 = -1 玩家赢了 //3-1 = 2 玩家赢了 //3-2 = 1 电脑赢了 //3-3 = 0 平手 if (pNum - cNum == -1 || pNum - cNum == 2) { return "玩家赢了"; } else if (pNum - cNum == -2 || pNum - cNum == 1) { return "电脑赢了"; } else { return "平手了"; } } } }
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 出拳游戏05 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnshitou_Click(object sender, EventArgs e) { PlayGame(btnshitou.Text); } private void PlayGame(string name) { Player p = new Player(); int pNum = p.Play(name); //把出拳的内容显示到窗体中 labPlay.Text = p.FistName; //电脑 Computer c = new Computer(); int cNum = c.Play(); labComputer.Text = c.FistName; //裁判 CaiPan cp = new CaiPan(); labres.Text = cp.WhoWin(pNum, cNum); } private void btnjiandao_Click(object sender, EventArgs e) { PlayGame(btnjiandao.Text); } private void btnbu_Click(object sender, EventArgs e) { PlayGame(btnbu.Text); } } }
标签:
原文地址:http://www.cnblogs.com/phpweige/p/4791661.html