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 CaiQuan
{
    class Player
    {
        //有个属性记录玩家出的是什么
        public String playName
        {
            set;
            get;
        }
        /// <summary>
        /// 玩家出拳的名称转化为对应的数字
        /// </summary>
        /// <param name="name">玩家出拳的名称</param>
        /// <returns>返回玩家出拳对应的数字:1石头;2剪刀;3布</returns>
        public int ShowWin(string name)
        {
            //石头 1;  剪刀 2 ; 布 3 ; 
            int num = -1;
            switch(name)
            {
                case "石头": num = 1; break;
                case "剪刀": num = 2; break;
                case "布":   num = 3; break;
            }
            this.playName = name;
            return num; //??
        }
        
    }
}

 

 

3、2 创建电脑的类

(创建方式同3、1)

实现的类如下:

Computer.cs

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

namespace CaiQuan
{
    class Computer
    {
        public string ComputerName
        {
            set;
            get;
        }
        /// <summary>
        /// 电脑出拳的结果
        /// </summary>
        /// <returns>电脑出拳的名称对应的数字</returns>
        public int ShowWin()
        {
            Random r = new Random();
            int num = r.Next(1, 4); //1,2,3
            switch(num)
            {
                case 1: ComputerName = "石头"; break;
                case 2: ComputerName = "剪刀"; break;
                case 3: ComputerName = "布";   break;
            }
            return num;
     
        }
    }
}

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;
            }
        }
    }
}

 

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];

        }
    }
}

 

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

 

  • 9
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值