简简单单的猜拳熊游戏(winform版)

这是一个使用C# Winform编写的简易猜拳游戏,玩家可以选择石头、剪刀或布,电脑随机出拳。通过比较玩家和电脑的出拳,程序会自动判断胜负,并在界面上展示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
using System.Collections;

namespace caiquan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //电脑随机出拳 在txt2中显示
        public string strPC = "";
        public string PC()
        {
            Random ran = new Random();

            int n = ran.Next(1, 4);

            if (n == 1)
            {
                strPC = "石头";
            }
            else if (n == 2)
            {
                strPC = "剪刀";
            }
            else if (n == 3)
            {
                strPC = "布";
            }
            else
            {
                throw new Exception("未知错误!");
            }
            txt2.Text = strPC;
            return strPC;
        }

        //玩家出拳 点击按钮
        private void btn1_Click(object sender, EventArgs e)
        {
            string shitou = btn1.Text;
            txt1.Text = shitou;

            int vPlayer = changetoValue(shitou);
            PC();
            int vPC = changetoValue(strPC);

            Compare(vPlayer, vPC);
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            string jiandao = btn2.Text;
            txt1.Text = jiandao;

            int vPlayer = changetoValue(jiandao);
            PC();
            int vPC = changetoValue(strPC);

            Compare(vPlayer, vPC);
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            string bu = btn3.Text;
            txt1.Text = bu;

            int vPlayer = changetoValue(bu);
            PC();
            int vPC = changetoValue(strPC);

            Compare(vPlayer, vPC);
        }

        //写一个将鼠标点击事件 将字符串转换成相应数值的方法
        public int changetoValue(string str)
        {
            int n = 0;

            //对传入的字符串进行判断
            //如果为石头 则给n 赋予值1

            if (str == "石头")
            {
                n = 1;
            }
            else if (str == "剪刀")
            {
                n = 2;
            }
            else if (str == "布")
            {
                n = 3;
            }
            else { throw new Exception("未知错误"); }
            return n;
        }

      
        //比较选手与电脑的值
        public void Compare(int Player,int PC)
        {
            //赢的情况有2种 1种是 玩家比电脑值小1  一种是小2
            if (Player - PC == -1 || Player - PC == 2)
            {
                txt3.Text = "恭喜您!您赢了!";
            }
            else if (Player - PC == 0)
            {
                txt3.Text = "您和电脑出的一样,这局平!";
            }
            else
            {
                txt3.Text = "很遗憾!您输了!";
            }
        }
      
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值