简单版猜拳游戏


界面很简单

//玩家类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 猜拳游戏
{
    /// <summary>
    /// 玩家类
    /// </summary>
   public class Player
    {
       //出拳结果
       public string FistName { get; set; }
       public int Play(string fistName)
       {
           this.FistName = fistName;
           int num = 0;
           switch (fistName)
           {
               case "石头": num = 1; break;
               case "剪刀": num = 2; break;
               case "布": num = 3; break;
           }
           return num;
       }
    }
}

//电脑类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 猜拳游戏
{
    /// <summary>
    /// 电脑类
    /// </summary>
    public 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 猜拳游戏
{
    /// <summary>
    /// 裁判类
    /// </summary>
   public class Judge
    {
       public string Win(int playNum, int computerNum)
       {
           if (playNum - computerNum == -1 || playNum - computerNum == 2)
           {
               return "玩家赢";
           }
           else if (playNum - computerNum == 0)
           {
               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 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStone_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            StartGame(btn.Text);
        }
        private void StartGame(string str)
        {
            Player player = new Player();
            int playerNum = player.Play(str);
            lbPlay.Text = player.FistName;


            Computer computer = new Computer();
            int computerNum = computer.Play();
            lbComputer.Text = computer.FistName;
            Judge jud = new Judge();
            lbRes.Text = jud.Win(playerNum, computerNum);
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值