C#簡易人機(jī)對抗“石頭剪刀布”游戲的實現(xiàn)
需要實現(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)文章
使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能
這篇文章主要為大家介紹了使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
C#操作SQLite數(shù)據(jù)庫方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
這篇文章主要介紹了C#操作SQLite數(shù)據(jù)庫方法,包括針對SQLite數(shù)據(jù)庫的創(chuàng)建,連接,插入,查詢,刪除等操作,并提供了一個SQLite的封裝類,需要的朋友可以參考下2016-07-07
C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法示例
這篇文章主要介紹了C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法,結(jié)合具體實例形式分析了數(shù)據(jù)庫的創(chuàng)建及checkedListBox1控件連接數(shù)據(jù)庫的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

