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

裁判结果为玩家的输赢。

相关推荐
xcLeigh6 小时前
C# Winform 2048小游戏源码
开发语言·c#·winform
界面开发小八哥3 天前
界面控件DevExpress WinForms v24.2新功能预览 - 支持.NET 9
.net·界面控件·winform·devexpress·ui开发
arbboter5 天前
RestSharp基本使用方法
开发语言·c#·winform·curl·webapi·restsharp
Crazy Struggle10 天前
.NET 8.0 通用管理平台,支持模块化、WinForms 和 WPF
vue·wpf·winform·.net 8.0·通用权限管理
ysdysyn15 天前
C# winfrom实现微信(其他应用)自动跟随C#窗口,拖动页面自动恢复到固定位置
开发语言·微信·c#·winform
小乖兽技术20 天前
WinForms 中使用 MVVM 模式构建应用:实现登录页面、页面导航及 SQLite 数据库连接完整框架搭建过程
数据库·sqlite·c#·winform·dotnet
WangMing_X22 天前
C# 单个函数实现各进制数间转换
java·开发语言·算法·c#·winform·软件
Crazy Struggle1 个月前
.NET 开源扁平化、美观的 C/S 控件库
.net·winform·控件库
威斯软科的老司机1 个月前
一波基于winform和wpf的桌面端界面,历久弥新。
wpf·winform·桌面界面
界面开发小八哥1 个月前
DevExpress WinForms中文教程:Data Grid - 如何点击获取信息?
c#·界面控件·winform·devexpress·ui开发