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

裁判结果为玩家的输赢。

相关推荐
199909239 天前
C#+Winform+SQLServer+图书管理系统
sqlserver·c#·winform
Crazy Struggle10 天前
.NET 开发的分流抢票软件,不做广告、不收集隐私
.net·winform·分流抢票
_oP_i13 天前
winform判断Windows 是否运行在 macOS 系统的虚拟机
windows·macos·winform
阿赵3D20 天前
阿赵的MaxScript学习笔记分享十六《MaxScript和WinForm交互》
笔记·学习·交互·winform·dotnet·maxscript
许野平1 个月前
Rust: enum 和 i32 的区别和互换
python·算法·rust·enum·i32
Crazy Struggle1 个月前
WinForm 通用权限框架,简单实用支持二次开发
winform·前后端分离·权限管理系统
※※冰馨※※1 个月前
[C#与C++交互] 跨进程通信NamedPipes
开发语言·c#·winform
c#上位机2 个月前
winform跨线程更新界面
c#·winform
界面开发小八哥2 个月前
DevExpress WinForms中文教程:Data Grid - 使用服务器模式的大数据源和即时反馈?
.net·界面控件·winform·devexpress·ui开发
刘欣的博客2 个月前
.net6 WinForm EF7 sqlite 做成一个exe执行文件
.net·winform·sqlite 一个exe·ef7