码迷,mamicode.com
首页 > Windows程序 > 详细

C#------猜拳游戏

时间:2015-09-08 15:27:36      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

namespace 出拳游戏05
{
    //玩家类
    class Player
    {
        //存储出拳的内容,显示到窗体中
        public string FistName { get; set; }

        //出拳的方法
        public int Play(string name)
        { 
            this.FistName = name;

            switch (name)
            {
                case "石头": return 1;
                case"剪刀":return 2;
                case"":return 3;
            }
            return 0;
        }
    }
}

 

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

namespace 出拳游戏05
{
    //电脑类
    class Computer
    {
        //存储出拳的内容
        public string FistName { get; set; }

        public int Play()
        {
            Random r = new Random();
            int num = r.Next(1, 4);

            switch (num)
            {
                case 1: this.FistName = "石头"; break;
                case 2: this.FistName = "剪刀"; break;
                case 3: this.FistName = ""; break;

            }
            return num;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 出拳游戏05
{
    //裁判类
    class CaiPan
    {
        public string WhoWin(int pNum, int cNum)
        { 
            //1-1 = 0 平手
            //1-2 = -1 玩家赢
            //1-3 = -2 电脑赢

            //2-1 = 1 电脑赢了
            //2-2 = 0 平手
            //2-3 = -1 玩家赢了

            //3-1 = 2 玩家赢了
            //3-2 = 1 电脑赢了
            //3-3 = 0 平手

            if (pNum - cNum == -1 || pNum - cNum == 2)
            {
                return "玩家赢了";
            }
            else if (pNum - cNum == -2 || pNum - cNum == 1)
            {
                return "电脑赢了";
            }
            else
            {
                return "平手了";
            }
        }
    }
}
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 出拳游戏05
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnshitou_Click(object sender, EventArgs e)
        {
            PlayGame(btnshitou.Text);
        }

        private void PlayGame(string name)
        {
            Player p = new Player();
            int pNum = p.Play(name);

            //把出拳的内容显示到窗体中
            labPlay.Text = p.FistName;

            //电脑
            Computer c = new Computer();
            int cNum = c.Play();
            labComputer.Text = c.FistName;

            //裁判
            CaiPan cp = new CaiPan();
            labres.Text = cp.WhoWin(pNum, cNum);
        }

        private void btnjiandao_Click(object sender, EventArgs e)
        {
            PlayGame(btnjiandao.Text);
        }

        private void btnbu_Click(object sender, EventArgs e)
        {
            PlayGame(btnbu.Text);
        }
    }
}

 

C#------猜拳游戏

标签:

原文地址:http://www.cnblogs.com/phpweige/p/4791661.html

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