随便写了一下
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private readonly Random RandomAnswer;
private readonly string AIAnswer;
public Form1()
{
InitializeComponent();
RandomAnswer = new Random();
AIAnswer = "电脑:";
}
private void btnRock_Click(object sender, EventArgs e)
{
RSPGame(ChoiceType.Rock);
radioButtonRock.Checked = true;
}
private void btnScissors_Click(object sender, EventArgs e)
{
RSPGame(ChoiceType.Scissors);
radioButtonScissors.Checked = true;
}
private void btnPaper_Click(object sender, EventArgs e)
{
RSPGame(ChoiceType.Paper);
radioButtonPaper.Checked = true;
}
private void RSPGame(ChoiceType choice)
{
ChoiceType answer = GenerateAnswer();
if (choice == answer)
{
labelAnswer.Text = "=";
labelAnswer.ForeColor = Color.Blue;
}
else if (choice == ChoiceType.Rock && answer == ChoiceType.Scissors ||
choice == ChoiceType.Scissors && answer == ChoiceType.Paper ||
choice == ChoiceType.Paper && answer == ChoiceType.Rock)
{
labelAnswer.Text = "√";
labelAnswer.ForeColor = Color.Green;
}
else
{
labelAnswer.Text = "X";
labelAnswer.ForeColor = Color.Red;
}
labelAIAnswer.Text = AIAnswer + GetDescription(answer);
}
private string GetDescription(ChoiceType answer)
{
var field = answer.GetType().GetField(answer.ToString());
var attribute = field.GetCustomAttribute<DescriptionAttribute>();
return attribute.Description;
}
private ChoiceType GenerateAnswer()
{
return (ChoiceType)(RandomAnswer.Next() % 3);
}
}
internal enum ChoiceType
{
[Description("石头")]
Rock,
[Description("剪刀")]
Scissors,
[Description("布")]
Paper
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632