zoukankan      html  css  js  c++  java
  • C#之winform 猜拳小游戏

    C#之winform 猜拳小游戏

    1、建立项目文件

    2、进行界面布局

    2、1 玩家显示(控件:label)

    2、2  显示玩家进行选择的控件(控件:label)

     2、3 电脑显示(控件:label)

     2、4   显示电脑进行选择的控件(控件:label)

     2、5 结果显示(控件:label)

     2、6 玩家与电脑的游戏结果(控件:textBox)

     2、7  玩家的选择按钮(控件:Button)

    2、8 玩家的选择按钮(控件:Button)

     2、9  玩家的选择按钮(控件:Button)

     2、10 运行

    3 、代码实现

     3、1 创建玩家的类​

    3、2 创建电脑的类

    3、3 创建裁判类(决策是谁赢了)

    3、4 功能实现

    3、4、1 打开Form1对应的代码

     3、4、2 窗口的控制代码


    1、建立项目文件

    2、进行界面布局

    在这个界面布局中,我们要修改一些属性(下面的序号与上面的截图一一对应):

    2、1 玩家显示(控件:label)

     其中,Name :是我们在程序中对这个变量进行控制的名称

    text:控件label在显示的时候的名称。

    2、2  显示玩家进行选择的控件(控件:label)

    Name :是我们在程序中对这个变量进行控制的名称

    这里是指的是选择的是:石头、剪刀、布

     2、3 电脑显示(控件:label)

     Name :是我们在程序中对这个变量进行控制的名称

    text:控件label在显示的时候的名称。

     2、4   显示电脑进行选择的控件(控件:label)

    Name :是我们在程序中对这个变量进行控制的名称

    这里是指的是选择的是:石头、剪刀、布

     

      

     2、5 结果显示(控件:label)

    Name :是我们在程序中对这个变量进行控制的名称
    text:控件label在显示的时候的名称。

     2、6 玩家与电脑的游戏结果(控件:textBox)

    Name :是我们在程序中对这个变量进行控制的名称

    这里是指的是选择的是:赢、输、平

     2、7  玩家的选择按钮(控件:Button)

    Name :是我们在程序中对这个变量进行控制的名称
    text:控件label在显示的时候的名称。

    2、8 玩家的选择按钮(控件:Button)

    Name :是我们在程序中对这个变量进行控制的名称
    text:控件label在显示的时候的名称。

     2、9  玩家的选择按钮(控件:Button)

    Name :是我们在程序中对这个变量进行控制的名称
    text:控件label在显示的时候的名称。

     2、10 运行

      通过上面的布局后,我们可以进行运行一下,会得到一个界面

    3 、代码实现

    在这里,我们需要变现相应的代码,来实现上面的控件所要实现的功能;

     3、1 创建玩家的类

     创建类

     

     Player.cs的内容如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CaiQuanMyself
    {
        class Player
        {
            // 储存玩家的名称
            public string playerName
            {
                set;
                get;
            }
            /// <summary>
            /// funtion: 实现玩家猜拳的名称与数字之间的对应关系
            /// 1石头     2剪刀     3布
            /// return type: int
            /// </summary>
            /// <param name="str">玩家猜拳的名称</param>
            /// <returns>猜拳的名称对应的数字;如玩家选择"布",对应输出数字"3"</returns>
            public int PlayerInformation(string str)
            {
                
                int num = 0;
                switch(str)
                {
                    case "石头": num = 1; this.playerName = "石头"; break;
                    case "剪刀": num = 2; this.playerName = "剪刀"; break;
                    case "":   num = 3; this.playerName = ""; break;
                }
                return num;
            }
        }
    }
    View Code

    3、2 创建电脑的类

    (创建方式同3、1)

    实现的类如下:

    Computer.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CaiQuanMyself
    {
        class Computer
        {
            // 电脑猜拳的名称
            public string computerName
            {
                set;
                get;
            }
            /// <summary>
            /// function: 返回电脑出的拳对应的数字,同时将对应的名称存储
            /// return type: int
            /// </summary>
            /// <returns>返回猜拳对的数字</returns>
            public int ComputerInformation()
            {
                //1石头     2剪刀     3布
                Random rd = new Random();
                int num = rd.Next(1, 4); // 1,2,3之间的随机数
                switch (num)
                {
                    case 1: this.computerName = "石头"; break;
                    case 2: this.computerName = "剪刀"; break;
                    case 3: this.computerName = "";   break;
                }
    
                return num;
            }
        }
    }
    View Code

    3、3 创建裁判类(决策是谁赢了)

    (创建方式同3、1)

    实现的类如下:

    Referee.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CaiQuanMyself
    {
        class Referee
        {
    //裁判情况表格
    //1石头  2剪刀  3布
    //玩家  对应名称 电脑  对应名称 结果  玩家结果
    //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            平
    
            /// <summary>
            /// 裁判判决的情况
            /// </summary>
            public enum eResult
            {
                win =0,
                draw = 1,
                loss =2
            }
            /// <summary>
            /// 裁判判决的情况
            /// </summary>
            public string[] sResult = new string[] { "", "", ""};
    
            /// <summary>
            /// 裁决玩家与电脑之间的猜拳结果
            /// return type : int
            /// </summary>
            /// <param name="playerChoice">玩家猜拳</param>
            /// <param name="computerChocie">电脑猜拳</param>
            /// <returns>猜拳结果</returns>
            public int Judgement(int playerChoice, int computerChocie)
            {
                //result = -1,2赢  0平  其他则输  (指的是玩家输赢的情况)
                int result = playerChoice - computerChocie;
                if (result == -1 || result==2 )
                {
                    return (int)eResult.win;
                }
                else if (result == 0)
                {
                    return (int)eResult.draw;
                }
                else
                {
                    return (int)eResult.loss;
                }
            }
        }
    }
    View Code

    3、4 功能实现

    3、4、1 打开Form1对应的代码

     3、4、2 窗口的控制代码

    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 CaiQuQuanGame
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                //禁止最大化窗口
                this.MaximizeBox = false;
                // 禁止对窗口进行拖拉
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
            // 点击事件触发
            private void butStone_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                RunGame(btn.Text);
            }
            public void RunGame(string playerChoice)
            {
                // 获取输入
                Player pChoice = new Player();
                int pResult = pChoice.PlayerInformation(playerChoice);
                labPlayerChoice.Text = pChoice.playerName;
    
                Computer cChoice = new Computer();
                int cResult = cChoice.ComputerInformation();
                labComputerChoice.Text = cChoice.computerName;
    
                // 结果判断
                Referee rChoice = new Referee();
                int rResult = rChoice.Judgement(pResult, cResult);
    
                // 输出
                textBoxResult.Text = rChoice.sResult[rResult];
    
            }
        }
    }
    View Code

     到这里我们就完成了整个猜拳游戏的编写。

  • 相关阅读:
    『奇葩问题集锦』npm install 报错 node-pre-gyp ERR! node-pre-gyp -v v0.6.25
    『奇葩问题集锦』Ruby 切换淘宝源报错WARNING: Error fetching data: SSL_connect returned=1 errno=0 state=SSLv3 read s erver certificate B: certificate verify failed
    一分钟搭建Webpack+react+es6框架
    『奇葩问题集锦』Cannot find module 'webpack/lib/node/NodeTemplatePlugin'
    『奇葩问题集锦』Zepto 页面唤醒拨号功能点透
    webpack面试题(转载)
    手机端样式
    输入框问题
    白色表单隐式边框阴影
    线性渐变css
  • 原文地址:https://www.cnblogs.com/jyfootprint/p/10121629.html
Copyright © 2011-2022 走看看