是 石头剪刀布  还是 剪刀石头布呢?

没想到它们之间的比值还有规律。

0 -1 -2

1 0 -1

2 1 0

平 赢 输

输 平 赢

赢 输 平

 

所以,0 是平局,-1 和 2 时是赢,-2 和 1 是输。

除了添加窗体,另外新建了一个类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _14猜拳游戏
{
    enum Result
    {
        平手,
        电脑赢,
        用户赢
    }
    enum Quan
    {
        石头,
        剪刀,
        布
    }

    class wocai
    {
        public Quan Computer()
        {
            Random r = new Random();
            int com = r.Next(0, 3);
            //if (com == 0)
            //{
            //    return Quan.石头;
            //}else if (com == 1)
            //{
            //    return Quan.剪刀;
            //}else
            //{
            //    return Quan.布;
            //}
            //return Enum.GetName(typeof(Quan), com); //返回字符串
            //从数值 返回 枚举类型
            return (Quan)com;  //返回枚举值类型  等于上面的if判断
        }

        public Result GetResult(Quan str1, Quan str2)
        {

            int res = str1 - str2;
            if (res == 0)
            {
                return Result.平手;
            }
            else if (res == -1 || res == 2)
            {
                return Result.用户赢;
            }
            else
            {
                return Result.电脑赢;
            }

        }
    }
}

 

窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _14猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Quan yonghu = Quan.石头;

            chuquan(yonghu);
        }

        private void chuquan(Quan yonghu)
        {
            wocai cai = new wocai();
            Quan com = cai.Computer();
            labelcomputer.Text = com.ToString();
            labelplayer.Text = yonghu.ToString();
            labelresult.Text = cai.GetResult(yonghu, com).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Quan yonghu = Quan.剪刀;

            chuquan(yonghu);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Quan yonghu = Quan.布;

            chuquan(yonghu);
        }
    }
}

使用枚举,感觉清晰一点,而且不用建多个类。

14猜拳游戏

14猜拳游戏

 

相关文章: