C#簡(jiǎn)易人機(jī)對(duì)抗“石頭剪刀布”游戲的實(shí)現(xiàn)
需要實(shí)現(xiàn)如下圖所示的人機(jī)猜拳小游戲:
我們需要建立一個(gè)玩家類(lèi)Player、一個(gè)電腦類(lèi)Computer、一個(gè)裁判類(lèi)Judge來(lái)分別模擬各自的操作:
【Player.cs】
/* * 作者:JeronZhou * 時(shí)間:2021-11-01 * 功能:石頭剪刀布游戲 */ using System; namespace Test2_2 { public class Player { public string FistName { get; set; } public int Play(string name) { FistName = name; switch (FistName) { case "石頭": return 1; case "剪刀": return 2; case "布": return 3; default: return 0; } } } }
【Computer.cs】
/* * 作者:JeronZhou * 時(shí)間:2021-11-01 * 功能:石頭剪刀布游戲 */ using System; namespace Test2_2 { public class Computer { public string FistName { get; set; } public int RandomPlay() { Random random = new Random(Guid.NewGuid().GetHashCode()); int num = random.Next(1, 4); switch (num) { case 1: FistName = "石頭"; break; case 2: FistName = "剪刀"; break; case 3: FistName = "布"; break; } return num; } } }
【Judge.cs】
/* * 作者:JeronZhou * 時(shí)間:2021-11-01 * 功能:石頭剪刀布游戲 */ using System; namespace Test2_2 { public class Judge { public string Win(int play, int computer) { int result = play - computer; switch (result) { case -1: return "你贏了"; case 2: return "你贏了"; case -2: return "你輸了"; case 1: return "你輸了"; default: return "平手"; } } } }
【窗體設(shè)計(jì)】
共有5個(gè)標(biāo)簽(3個(gè)空標(biāo)簽),三個(gè)按鈕。
【MainForm.cs】
/* * 作者:JeronZhou * 時(shí)間:2021-11-01 * 功能:石頭剪刀布游戲 */ using System; using System.Windows.Forms; namespace Test2_2 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } void Button1Click(object sender, EventArgs e) { Player p = new Player(); int playerName = p.Play(button1.Text); label3.Text = p.FistName; Computer c = new Computer(); int computerName = c.RandomPlay(); label4.Text = c.FistName; Judge judge = new Judge(); label5.Text = judge.Win(playerName, computerName); } void Button2Click(object sender, EventArgs e) { Player p = new Player(); int playerName = p.Play(button2.Text); label3.Text = p.FistName; Computer c = new Computer(); int computerName = c.RandomPlay(); label4.Text = c.FistName; Judge judge = new Judge(); label5.Text = judge.Win(playerName, computerName); } void Button3Click(object sender, EventArgs e) { Player p = new Player(); int playerName = p.Play(button3.Text); label3.Text = p.FistName; Computer c = new Computer(); int computerName = c.RandomPlay(); label4.Text = c.FistName; Judge judge = new Judge(); label5.Text = judge.Win(playerName, computerName); } } }
【Program.cs】
/* * 作者:JeronZhou * 時(shí)間:2021-11-01 * 功能:石頭剪刀布游戲 */ using System; using System.Windows.Forms; namespace Test2_2 { internal sealed class Program { [STAThread] private static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } }
【測(cè)試結(jié)果】
到此這篇關(guān)于C#簡(jiǎn)易人機(jī)對(duì)抗“石頭剪刀布”游戲的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# 石頭剪刀布內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SQL Server存儲(chǔ)過(guò)程在C#中調(diào)用的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于SQL Server存儲(chǔ)過(guò)程在C#中調(diào)用的簡(jiǎn)單實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SQL Server存儲(chǔ)過(guò)程具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Stream.Write 與 StreamWriter.Write 的不同
Stream.Write 與 StreamWriter.Write 是我們?cè)谙蛄髦袑?xiě)數(shù)據(jù)時(shí),最常用的方法。下面就詳細(xì)講解這兩個(gè)方法。2013-04-04C#單例模式(Singleton Pattern)實(shí)例教程
這篇文章主要介紹了C#單例模式(Singleton Pattern)的實(shí)現(xiàn)方法,主要講述了即時(shí)加載的單例模式、延遲加載的單例模式與線(xiàn)程安全的單例模式,需要的朋友可以參考下2014-09-09Unity UGUI的LayoutElement布局元素組件介紹使用示例
這篇文章主要為大家介紹了Unity UGUI的LayoutElement布局元素組件介紹使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07