【C#】简易人机对抗“石头剪刀布”游戏

需要实现如下图所示的人机猜拳小游戏:
在这里插入图片描述
我们需要建立一个玩家类Player、一个电脑类Computer、一个裁判类Judge来分别模拟各自的操作:

【Player.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:石头剪刀布游戏
 */

using System;

namespace Test2_2
{
	public class Player
	{
        public string FistName { get; set; }
       	public int Play(string name)
       	{
            FistName = name;
            switch (FistName)
            {
                case "石头":
                    return 1;
                case "剪刀":
                    return 2;
                case "布":
                    return 3;
                default:
                    return 0;
            }
        }
	}
}

【Computer.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:石头剪刀布游戏
 */

using System;

namespace Test2_2
{
	public class Computer
	{
        public string FistName { get; set; }
        public int RandomPlay()
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            int num = random.Next(1, 4);
            switch (num)
            {
                case 1:
            		FistName = "石头";
            		break;
                case 2:
            		FistName = "剪刀";
            		break;
                case 3:
            		FistName = "布";
            		break;
            }
            return num;
        }
	}
}

【Judge.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:石头剪刀布游戏
 */

using System;

namespace Test2_2
{
	public class Judge
	{
        public string Win(int play, int computer)
        {
            int result = play - computer;
            switch (result)
            {
                case -1:
               		return "你赢了";
                case 2:
                    return "你赢了";
                case -2:
                    return "你输了";
                case 1:
                    return "你输了";
                default:
                    return "平手";
            }
        }
	}
}

【窗体设计】
共有5个标签(3个空标签),三个按钮。
在这里插入图片描述
【MainForm.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:石头剪刀布游戏
 */

using System;
using System.Windows.Forms;

namespace Test2_2
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		void Button1Click(object sender, EventArgs e)
		{
            Player p = new Player();
            int playerName = p.Play(button1.Text);
            label3.Text = p.FistName;
            Computer c = new Computer();
            int computerName = c.RandomPlay();
            label4.Text = c.FistName;
            Judge judge = new Judge();
            label5.Text = judge.Win(playerName, computerName);
		}
		void Button2Click(object sender, EventArgs e)
		{
            Player p = new Player();
            int playerName = p.Play(button2.Text);
            label3.Text = p.FistName;
            Computer c = new Computer();
            int computerName = c.RandomPlay();
            label4.Text = c.FistName;
            Judge judge = new Judge();
            label5.Text = judge.Win(playerName, computerName);
		}
		void Button3Click(object sender, EventArgs e)
		{
            Player p = new Player();
            int playerName = p.Play(button3.Text);
            label3.Text = p.FistName;
            Computer c = new Computer();
            int computerName = c.RandomPlay();
            label4.Text = c.FistName;
            Judge judge = new Judge();
            label5.Text = judge.Win(playerName, computerName);
		}
	}
}

【Program.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:石头剪刀布游戏
 */

using System;
using System.Windows.Forms;

namespace Test2_2
{
	internal sealed class Program
	{
		[STAThread]
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
	}
}

【测试结果】
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 75
    点赞
  • 244
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论
当然可以!以下是一个简单的石头剪刀游戏C#代码示例: ```csharp using System; class Program { static void Main(string[] args) { Console.WriteLine("欢迎来到石头剪刀游戏!"); while (true) { Console.WriteLine("请选择:1. 石头 2. 剪刀 3. "); int playerChoice = Convert.ToInt32(Console.ReadLine()); Random random = new Random(); int computerChoice = random.Next(1, 4); Console.WriteLine("你选择了:" + GetChoiceName(playerChoice)); Console.WriteLine("电脑选择了:" + GetChoiceName(computerChoice)); string result = GetGameResult(playerChoice, computerChoice); Console.WriteLine(result); Console.WriteLine("是否继续游戏?(Y/N)"); string continueGame = Console.ReadLine(); if (continueGame.ToUpper() != "Y") break; } Console.WriteLine("游戏结束!"); } static string GetChoiceName(int choice) { switch (choice) { case 1: return "石头"; case 2: return "剪刀"; case 3: return ""; default: return ""; } } static string GetGameResult(int playerChoice, int computerChoice) { if (playerChoice == computerChoice) return "平局!"; else if ((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1)) return "你赢了!"; else return "你输了!"; } } ``` 这段代码实现了一个简单的石头剪刀游戏。玩家通过输入数字选择石头剪刀,电脑随机生成选择,然后判断胜负并输出结果。玩家可以选择是否继续游戏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jeron Zhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值