最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#簡易人機(jī)對抗“石頭剪刀布”游戲的實現(xiàn)

 更新時間:2022年05月10日 11:14:35   作者:JeronZhou  
本文主要介紹了C#簡易人機(jī)對抗“石頭剪刀布”游戲的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

需要實現(xiàn)如下圖所示的人機(jī)猜拳小游戲:

在這里插入圖片描述

我們需要建立一個玩家類Player、一個電腦類Computer、一個裁判類Judge來分別模擬各自的操作:

【Player.cs】

/*
 * 作者:JeronZhou
 * 時間: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
 * 時間: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
 * 時間: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è)計】
共有5個標(biāo)簽(3個空標(biāo)簽),三個按鈕。

在這里插入圖片描述

【MainForm.cs】

/*
 * 作者:JeronZhou
 * 時間: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
 * 時間: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());
		}
	}
}

【測試結(jié)果】

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

 到此這篇關(guān)于C#簡易人機(jī)對抗“石頭剪刀布”游戲的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# 石頭剪刀布內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

定边县| 修文县| 锡林郭勒盟| 孝义市| 天津市| 灌南县| 新巴尔虎左旗| 景东| 苗栗市| 孝义市| 通许县| 潍坊市| 仪征市| 东丽区| 休宁县| 兴和县| 清水县| 钟山县| 长丰县| 中西区| 府谷县| 寿光市| 阳东县| 赤城县| 永川市| 临澧县| 河间市| 宝山区| 那曲县| 瑞安市| 彭山县| 乌审旗| 宁明县| 高台县| 炉霍县| 东城区| 溧水县| 手机| 肇东市| 双牌县| 昭通市|