1. 程式人生 > >枚舉、封裝方法的應用--剪刀石頭布遊戲

枚舉、封裝方法的應用--剪刀石頭布遊戲

string play res efault stat 需要 提取方法 break eve

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.電腦贏;
        }

    }
}

  

枚舉、封裝方法的應用--剪刀石頭布遊戲