码迷,mamicode.com
首页 > Web开发 > 详细

.Net基础加强02

时间:2014-07-14 22:50:35      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   art   for   io   

猜拳游戏:

玩家类:

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

namespace 猜拳游戏
{
    public  class Player
    {
        //用来存储结果的属性
        public string FistName
        {
            get;
            set;
        }
        //出拳的方法  1表示剪刀 2表示石头 3表示布
        public int ShowFist(string fist)
        {
            int result = -1;
            this.FistName = fist;
            switch (fist)
            { 
                case "剪刀":
                    result = 1;
                    break;
                case "石头":
                    result = 2;
                    break;
                case "":
                    result = 3;
                    break;
            }
            return result;
        }
    }
}

计算机类:

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

namespace 猜拳游戏
{
    public class Computer
    {
        public string FistName
        {
            get;
            set;
        }
        //计算机的出拳方式
        public int ShowFist()
        {
            Random rdm = new Random();
            int result = rdm.Next(1, 4);// [1,4)
            switch(result)
            {
                case 1:
                    this.FistName="剪刀";
                    break;
                 case 2:
                    this.FistName="石头";
                    break;
                  case 3:
                    this.FistName="";
                    break;
             }
            return result;
        }
    }
}

裁判类:

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

namespace 猜拳游戏
{
    public class CaiPan
    {
        public string PanDuanShuYing(int playerFist, int computerFist)
        {
            if (playerFist == computerFist)
            {
                return "棋逢对手";
            }
            if (playerFist - computerFist == -2 || playerFist - computerFist == 1)
            {
                return "恭喜你获胜";
            }
            else
            {
                return "下次加油!";
            }
        }
    }
} 

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 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用户单击剪刀
        private void btnjiandao_Click(object sender, EventArgs e)
        {
            // new一个玩家对象
            Player P1 = new Player();
            // 调用玩家出拳
            int playerFist=P1.ShowFist("剪刀");
            lblPlayer.Text = P1.FistName;
            // new一个计算机对象
            Computer computer = new Computer();
            //调用计算机出拳
            int computerFist = computer.ShowFist();
            lblComputer.Text = computer.FistName;
            //调用裁判
            CaiPan cp = new CaiPan();
            lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist);
        }

        private void btnshitou_Click(object sender, EventArgs e)
        {
            // new一个玩家对象
            Player P1 = new Player();
            // 调用玩家出拳
            int playerFist = P1.ShowFist("石头");
            lblPlayer.Text = P1.FistName;
            // new一个计算机对象
            Computer computer = new Computer();
            //调用计算机出拳
            int computerFist = computer.ShowFist();
            lblComputer.Text = computer.FistName;
            //调用裁判
            CaiPan cp = new CaiPan();
            lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist);
        }

        private void btnbu_Click(object sender, EventArgs e)
        {
            // new一个玩家对象
            Player P1 = new Player();
            // 调用玩家出拳
            int playerFist = P1.ShowFist("");
            lblPlayer.Text = P1.FistName;
            // new一个计算机对象
            Computer computer = new Computer();
            //调用计算机出拳
            int computerFist = computer.ShowFist();
            lblComputer.Text = computer.FistName;
            //调用裁判
            CaiPan cp = new CaiPan();
            lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist);
        }
    }
}

 提取方法,封装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 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用户单击剪刀
        private void btnjiandao_Click(object sender, EventArgs e)
        {
            StartGame("剪刀");
        }
        private void btnshitou_Click(object sender, EventArgs e)
        {
            StartGame("石头");
        }
        private void btnbu_Click(object sender, EventArgs e)
        {
            StartGame("");
        }
        private void StartGame(string item)
        {
            // new一个玩家对象
            Player P1 = new Player();
            // 调用玩家出拳
            int playerFist = P1.ShowFist("item");
            lblPlayer.Text = item;
            // new一个计算机对象
            Computer computer = new Computer();
            //调用计算机出拳
            int computerFist = computer.ShowFist();
            lblComputer.Text = computer.FistName;
            //调用裁判
            CaiPan cp = new CaiPan();
            lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist);
        }

    }
}

 或者把三个按钮都设置成 btnjiandao_Click事件,代码如下 同样能实现

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 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用户单击剪刀
        private void btnjiandao_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            StartGame(btn.Text);
        }

        private void StartGame(string item)
        {
            // new一个玩家对象
            Player P1 = new Player();
            // 调用玩家出拳
            int playerFist = P1.ShowFist("item");
            lblPlayer.Text = item;
            // new一个计算机对象
            Computer computer = new Computer();
            //调用计算机出拳
            int computerFist = computer.ShowFist();
            lblComputer.Text = computer.FistName;
            //调用裁判
            CaiPan cp = new CaiPan();
            lblResult.Text = cp.PanDuanShuYing(playerFist, computerFist);
        }

    }
}

 

 

.Net基础加强02,布布扣,bubuko.com

.Net基础加强02

标签:style   blog   color   art   for   io   

原文地址:http://www.cnblogs.com/yechangzhong-826217795/p/3842407.html

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