第二个案例(猜拳游戏)

添加组件

这里写图片描述

Content Name:labPlayer
content Name:labComputer
Player Name:labResult

Player类的实现

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

namespace Guess
{
    public class Player
    {
        //自动属性//
        public string Punch { get; set; }
        public int Play(string punch)
        {
            this.Punch = punch;

            int num = 0;
            switch (punch)
            {
                case "tone":
                    num = 1;
                    break;
                case "jian":
                    num = 2;
                    break;
                case "bu":
                    num = 3;
                    break;

            }

            return num;
        }
    }
}

Computer类的实现

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

namespace Guess
{
    public class Computer
    {
        public string Punch { get; set; }
        public int Play()
        {
            Random r = new Random();//随机数//
            int num=r.Next(1,4);
            switch(num)
            {
                case 1:
                    this.Punch = "tone";
                    break;
                case 2:
                    this.Punch = "jian";
                    break;
                case 3:
                    this.Punch = "bu";
                    break;
            }
            return num;
        }
    }
}

Referee(裁判)类的实现

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

namespace Guess
{
    public class Referee
    {
        //判断谁赢谁输
        public string Win(int playerNum,int computerNum)
        {
            if(playerNum-computerNum==0)
            {
                return "double win";
            }
            else if(playerNum-computerNum==-1||playerNum-computerNum==2)
            {
                return "player win";
            }
            else
            {
                return "computer win";
            }
        }
    }
}

主程序的实现

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 Guess
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //选中需要重构的代码,右键,Refactor->Extract Method
        private void Play(string name)
        {
            Player p = new Player();
            int pNum = p.Play(name);

            labPlayer.Text = p.Punch;

            Computer c = new Computer();
            int cNum = c.Play();

            labComputer.Text = p.Punch;

            Referee r = new Referee();
            string result = r.Win(pNum, cNum);

            labresult.Text = result;
        }

        //在图形化界面中,把各个按钮的click事件绑定该函数
        private void button_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            Play(btn.Text);
        }

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值