码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-23 16:21:36      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:dom   .com   mod   ons   span   .text   initial   result   back   

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.电脑赢;
        }

    }
}

  

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

标签:dom   .com   mod   ons   span   .text   initial   result   back   

原文地址:http://www.cnblogs.com/my-cat/p/7418697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!