猜拳游戏(用面向对象思想实现)

Form1.cs

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnJianDao_Click(object sender, EventArgs e)
        {
            Player player = new Player();
            Computer computer = new Computer();
            string pcChooseStr=Computer.PCchoose();
            //lblDianNao.Text = pcChooseStr; 文字展示
            string jiandaoUrl = Computer.BackPic(pcChooseStr);
            picMy.Load(@"d:\我的文档\Visual Studio 2010\Projects\随机点名\猜拳游戏\Img\剪刀.jpg");
            picPC.Load(jiandaoUrl);
            lblResult.Text = Player.OutJianDao(pcChooseStr);


        }

        private void btnShiTou_Click(object sender, EventArgs e)
        {
            Player player = new Player();
            Computer computer = new Computer();
            string pcChooseStr = Computer.PCchoose();
            //lblDianNao.Text = pcChooseStr;
            string shitouUrl = Computer.BackPic(pcChooseStr);
            picMy.Load(@"d:\我的文档\Visual Studio 2010\Projects\随机点名\猜拳游戏\Img\石头.jpg");
            picPC.Load(shitouUrl);
            lblResult.Text = Player.OutShiTou(pcChooseStr);
        }

        private void btnBu_Click(object sender, EventArgs e)
        {
            Player player = new Player();
            Computer computer = new Computer();
            string pcChooseStr = Computer.PCchoose();
            //lblDianNao.Text = pcChooseStr;
            string buUrl = Computer.BackPic(pcChooseStr);
            picMy.Load(@"d:\我的文档\Visual Studio 2010\Projects\随机点名\猜拳游戏\Img\布.jpg");
            picPC.Load(buUrl);
            lblResult.Text = Player.OutBu(pcChooseStr);
        }
    }
}

Player.cs 类

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 猜拳游戏
{
    public class Player
    {
        #region 没有用上的代码 可删除
        //private string shitou;

        //public string Shitou
        //{
        //    get { return shitou; }
        //    set { shitou = value; }
        //}
        //private string jiandao;

        //public string Jiandao
        //{
        //    get { return jiandao; }
        //    set { jiandao = value; }
        //}
        //private string bu;

        //public string Bu
        //{
        //    get { return bu; }
        //    set { bu = value; }
        //}
        #endregion

        public static string OutJianDao(string pcChose,string s="剪刀")
        {
            if (pcChose == "剪刀")
            {
                return "平手,再战三百回合!";
            }
            else if (pcChose == "石头")
            {
                return "我输了,杯具啊!";
            }
            else
            {
               return "我赢了!O(∩_∩)";
            }
        }


        public static string OutShiTou(string pcChose, string s = "石头")
        {
            if (pcChose == "剪刀")
            {
                return "我赢了!O(∩_∩)!";
            }
            else if (pcChose == "石头")
            {
                return "平手,再战三百回合!";
            }
            else
            {
                return "我输了,杯具啊!";
            }
        }

        public static string OutBu(string pcChose, string s = "")
        {
            if (pcChose == "剪刀")
            {
                return "我输了,杯具啊!";
            }
            else if (pcChose == "石头")
            {
                return "我赢了!O(∩_∩)!";
            }
            else
            {
                return "平手,再战三百回合!";
            }
        }


    }
}

Computer.cs 类

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 猜拳游戏
{
    class Computer
    {
        #region 没有用上的代码 可删除
        //private string pcShitou;

        //public string PcShitou
        //{
        //    get { return pcShitou; }
        //    set { pcShitou = value; }
        //}
        //private string pcJiandao;

        //public string PcJiandao
        //{
        //    get { return pcJiandao; }
        //    set { pcJiandao = value; }
        //}
        //private string pcBu;

        //public string PcBu
        //{
        //    get { return pcBu; }
        //    set { pcBu = value; }
        //}
        #endregion

        public static string PCchoose()
        {
            Random r = new Random();
            int i = r.Next(0, 3);
            string[] pcChose = { "石头", "剪刀", "" };
            return pcChose[i];
        }

        public static string BackPic(string s)
        {

            if (s == "剪刀")
            {
                string picJianDao = @"d:\我的文档\Visual Studio 2010\Projects\随机点名\猜拳游戏\Img\剪刀.jpg";
                return picJianDao;
            }
            else if (s == "石头")
            {
                string picShitou = @"d:\我的文档\Visual Studio 2010\Projects\随机点名\猜拳游戏\Img\石头.jpg";
                return picShitou;
            }
            else
            {
                string picBu = @"d:\我的文档\Visual Studio 2010\Projects\随机点名\猜拳游戏\Img\布.jpg";
                return picBu;
            }
        }
        
    }
}

运行效果:

资源地址:猜拳游戏.rar

posted @ 2013-03-30 00:02  IT浪潮之巅  阅读(557)  评论(0编辑  收藏  举报
   友情链接: 淘宝优惠券