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、运行效果

裁判结果为玩家的输赢。

相关推荐
CSharp精选营7 小时前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform
小贺儿开发7 天前
Unity3D 家居视频遥控效果演示
unity·udp·人机交互·网络通信·winform·远程·photon
苏渡苇7 天前
枚举的高级用法——用枚举实现策略模式和状态机
java·单例模式·策略模式·枚举·状态机·enum
czhc11400756638 天前
winform 330 跨线程 异步
wpf·线程·winform
light blue bird10 天前
原生控件GDI完成作业协同界面
jvm·数据库·.net·winform·gdi+界面
light blue bird23 天前
MES/ERP大数据报表条件索引查询组件
数据库·.net·winform·t-sql·大数据报表
香煎三文鱼24 天前
winform读取不到App.config配置文件中的配置信息
winform
时光の尘24 天前
嵌入式面试八股文(二十)·C语言关键字相关知识点速通(static、const、volatile、struct、enum、union)
c语言·const·static·union·volatile·struct·enum
Aevget25 天前
界面控件DevExpress WinForms中文教程:Data Grid - 数据绑定(二)
.net·界面控件·winform·devexpress·ui开发
小曹要微笑25 天前
WinForms 验证码类的实现
c#·验证码·winform·验证码类