C#实现猜拳游戏_三局两胜制(窗体)

界面设计

在这里插入图片描述

代码

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;

namespace game_caiquan
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }
        /// <summary>
        /// 比赛过程
        /// </summary>
        /// <param name="num1"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        private int clickCount;//定义按钮的点击次数
        int count_result = 0;
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的 C# 窗体应用程序编写猜拳游戏的示例代码: 1. 创建一个 Windows 窗体应用程序项目,命名为“猜拳游戏”。 2. 在窗体中添加三个按钮,分别代表“石头”、“剪刀”、“布”三种手势。 3. 在窗体中添加一个 Label 控件,用于显示游戏结果。 4. 在窗体的代码中,定义一个数组来存储三种手势的名称和对应的编号: ``` string[] gestures = { "石头", "剪刀", "布" }; ``` 5. 在窗体的代码中,定义一个 Random 类型的变量,用于生成随机数: ``` Random random = new Random(); ``` 6. 在窗体的代码中,为每个按钮的 Click 事件添加处理程序,用于模拟玩家和计算机的出拳,并比较胜负: ``` private void btnStone_Click(object sender, EventArgs e) { int playerGesture = 0; // 玩家出拳为石头 int computerGesture = random.Next(0, 3); // 计算机随机出拳 string result = CompareGesture(playerGesture, computerGesture); // 比较胜负 lblResult.Text = result; // 显示游戏结果 } private void btnScissors_Click(object sender, EventArgs e) { int playerGesture = 1; // 玩家出拳为剪刀 int computerGesture = random.Next(0, 3); // 计算机随机出拳 string result = CompareGesture(playerGesture, computerGesture); // 比较胜负 lblResult.Text = result; // 显示游戏结果 } private void btnPaper_Click(object sender, EventArgs e) { int playerGesture = 2; // 玩家出拳为布 int computerGesture = random.Next(0, 3); // 计算机随机出拳 string result = CompareGesture(playerGesture, computerGesture); // 比较胜负 lblResult.Text = result; // 显示游戏结果 } ``` 7. 在窗体的代码中,定义一个 CompareGesture 方法,用于比较玩家和计算机的手势,并返回游戏结果: ``` private string CompareGesture(int playerGesture, int computerGesture) { if (playerGesture == computerGesture) { return "平局"; } else if ((playerGesture + 1) % 3 == computerGesture) { return "你输了"; } else { return "你赢了"; } } ``` 8. 运行程序,点击三个按钮进行游戏,可以看到游戏结果在 Label 中显示。 这就是一个简单的 C# 窗体应用程序编写猜拳游戏的示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值