枚举封装方法的应用--剪刀石头布游戏

Posted mCat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了枚举封装方法的应用--剪刀石头布游戏相关的知识,希望对你有一定的参考价值。

Form

封装方法:将需要封装成方法的代码选中,右键-重构-提取方法

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 _5.石头剪刀布游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = "剪刀";
            Game(str);
        }

        private void Game(string str)
        {
            lblPlayer.Text = str;

            Computer computer = new Computer();
            int computerNum = computer.ShowFist(ref lblComputer);

            Player player = new Player();
            int playerNum = player.ShowFist(str);

            Result res = Judge.showJudge(playerNum, computerNum);
            lblResult.Text = res.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string str = "石头";
            Game(str);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string str = "布";
            Game(str);
        }
    }
}

  

Palyer.cs

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

namespace _5.石头剪刀布游戏
{
    class Player
    {
        public int ShowFist(string str)
        {
            switch(str)
            {
                case "剪刀":
                    return 1;
                    break;
                case "石头":
                    return 2;
                case "布":
                    return 3;
                default :
                    return 0;
            }
        }
    }
}

Computer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _5.石头剪刀布游戏
{
    class Computer
    {
        public string Fist
        {
            get;
            set;
        }
        public int ShowFist(ref Label lblComputer)
        {
            Random r = new Random();
            int rNum = r.Next(1, 4);
            switch (rNum)
            {
                case 1:
                    Fist = "剪刀";
                    break;
                case 2:
                    this.Fist = "石头";
                    break;
                case 3:
                    this.Fist = "布";
                    break;
            }

            lblComputer.Text = this.Fist;
            return rNum;
        }
    }
}

Judge.cs

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

namespace _5.石头剪刀布游戏
{
    public enum Result
    {
        玩家赢,
        电脑赢,
        平局
    }
    class Judge
    {
        public static Result showJudge(int palyer,int computer)
        {
            int res = palyer - computer;
            if (res == -2 || res == 2 || res == 1)
                return Result.玩家赢;
            else if (res == 0)
            {
                return Result.平局;
            }
            else
                return Result.电脑赢;
        }

    }
}

  

以上是关于枚举封装方法的应用--剪刀石头布游戏的主要内容,如果未能解决你的问题,请参考以下文章

Python Tkinter实战案例,搞定剪刀石头布小游戏,就是这么强!

Java自制人机小游戏——————————剪刀石头布

python小游戏(剪刀石头布)

python小游戏(剪刀石头布)

剪刀石头布游戏

团队-游戏《石头,剪刀,布》-代码设计规范