石头剪刀布小游戏

新建一个玩家类、电脑类、裁判类

玩家类的出拳方式由button控件来决定,一共三个,然后把这三个可能性转化成数字123用于比较

电脑类的出拳方式由随机数产生123来决定

裁判类主要在于将玩家和电脑的出拳进行比较,并且返回一个结果,结果就是平手,玩家赢,电脑赢

这一共三个结果要返回,并且显示在lable控件上,所以用了一个枚举类型来存放这个返回的结果,通过比较玩家和电脑出拳代表的数字相减的结果得出答案

这是主程序调用

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 winform程序练习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            string str = "剪刀";
            lblFistPlayer.Text = str;
            Player player = new Player();
            int playerNumber = player.ShowFist(str);
            Computer cpu = new Computer();
            int cpuNumber = cpu.ShowFist();
            lblFistCpu.Text = cpu.Fist;//把电脑随机生成的出拳显示在电脑的lable控件上
            Result res = Judge.Judger(playerNumber, cpuNumber);
            lblResult.Text = res.ToString();
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            string str = "石头";
            lblFistPlayer.Text = str;
            Player player = new Player();
            int playerNumber = player.ShowFist(str);
            Computer cpu = new Computer();
            int cpuNumber = cpu.ShowFist();
            lblFistCpu.Text = cpu.Fist;//把电脑随机生成的出拳显示在电脑的lable控件上
            Result res = Judge.Judger(playerNumber, cpuNumber);
            lblResult .Text = res.ToString();
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            string str = "";
            lblFistPlayer.Text = str;
            Player player = new Player();
            int playerNumber = player.ShowFist(str);
            Computer cpu = new Computer();
            int cpuNumber = cpu.ShowFist();
            lblFistCpu.Text = cpu.Fist;//把电脑随机生成的出拳显示在电脑的lable控件上
            Result res = Judge.Judger(playerNumber, cpuNumber);
            lblResult.Text = res.ToString();
        }
    }
}
View Code

玩家类

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

namespace winform程序练习
{
    class Player
    {
        public int ShowFist(string Fist)
        {
            int num = 0;
            switch (Fist)
            {
                case "石头":num = 1;
                    break;
                case "剪刀":num = 2;
                    break;
                case "":num = 3;
                    break;
            }
            return num;

        }
    }
}
View Code

电脑类

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

namespace winform程序练习
{
    class Computer
    {
        //用来存储出的拳头
        public string Fist
        {
            get;
            set;
        }
        Random r = new Random();
        public int ShowFist()
        {
            int number = r.Next(1, 4);
            switch (number )
            {
                case 1:this.Fist = "拳头";
                    break;
                case 2:this.Fist = "剪刀";
                    break;
                case 3:this.Fist = "";
                    break;
            }
            return number ;
        }
    }
}
View Code

裁判类

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

namespace winform程序练习
{
    public enum Result
    {
        玩家赢,
        电脑赢,
        平手

    }
    class Judge
    {
        public static Result Judger(int playerNumber,int cpuNumber)
        {
            if(playerNumber -cpuNumber==-1||playerNumber -cpuNumber==2)
            {
                return Result.玩家赢;
            }
            else if(playerNumber -cpuNumber ==0)
            {
                return Result.平手;
            }
            else
            {
                return Result.电脑赢;
            }
        }
    }
}
View Code

控件界面:

 

posted @ 2021-08-21 22:10  静态类  阅读(85)  评论(0编辑  收藏  举报