標籤:
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); } }}
C#------猜拳遊戲