Winform实现石头剪刀布小游戏

1、电脑玩家类

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RockScissorsClothApp
{
    public class Computer
    {
        public Card Play()
        {
            Random random = new Random();
            int num = random.Next(0, 3);
            switch (num)
            {
                case 0:
                    return Card.Rock;
                case 1:
                    return Card.Scissors;
                default:
                    return Card.CLoth;
            }
        }
        public static string ToString(Card c)
        {
            return c.ToString();
        }
    }
    public enum Card
    {
        Rock,
        Scissors,
        CLoth
    }
}

2、玩家类

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RockScissorsClothApp
{
    public class Player
    {
        public Card Play(string name)
        {
            switch (name)
            {
                case "石头":
                    return Card.Rock;
                case "剪刀":
                    return Card.Scissors;
                case "布":
                default:
                    return Card.CLoth;
            }
        }
    }
}

3、裁判类

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RockScissorsClothApp
{
    public class Referee
    {
        public static Result Judge(Card c1, Card c2)
        {
            int dif = c1 - c2;
            switch (dif)
            {
                case 0:
                    return Result.Draw;
                case -1:
                case 2:
                    return Result.lost;
                case -2:
                case 1:
                default:
                    return Result.Win;
            }
        }

        public static string ToString(Result c)
        {
            return c.ToString();
        }
    }
    public enum Result
    {
        Win,
        lost,
        Draw
    }
}

4、应用程序

csharp 复制代码
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 RockScissorsClothApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnRock_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            if (button != null)
            {
                Computer computer = new Computer();
                Card c1 = computer.Play();
                lblComputerCard.Text = Computer.ToString(c1);
                string str = button.Text;
                Player player = new Player();
                Card c2 = player.Play(str);
                lblPlayerCard.Text = Computer.ToString(c2);
                Result result = Referee.Judge(c1, c2);
                lblRefereeResult.Text = result.ToString();
            }
        }
    }
}

5、运行效果

裁判结果为玩家的输赢。

相关推荐
界面开发小八哥1 小时前
界面控件DevExpress WinForms v25.1新功能预览 - 功能区组件全新升级
人工智能·.net·界面控件·winform·devexpress
时光追逐者8 天前
分享5款开源、美观的 WinForm UI 控件库
ui·开源·c#·.net·winform
啊波次得饿佛哥13 天前
在winform中使用chromiumWebBrowser显示Echarts图表
前端·javascript·echarts·winform·cefsharp
hjjdebug13 天前
c++中的enum变量 和 constexpr说明符
c++·enum·constexpr
景天科技苑15 天前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配
PfCoder1 个月前
WinForm真入门(13)——ListBox控件详解
windows·c#·visual studio·winform
幻想趾于现实1 个月前
C# Winform 入门(15)之制作二维码和拼接(QR)
开发语言·c#·winform
霸道流氓气质1 个月前
Winform入门进阶企业级开发示例:http接口数据清洗转换、断线续传、mqtt数据传输实例详解(附代码资源下载)
http·c#·winform
PfCoder1 个月前
WinForm真入门(11)——ComboBox控件详解
windows·c#·visual studio·winform
幻想趾于现实1 个月前
C# Winform 入门(11)之制作酷炫灯光效果
开发语言·c#·winform