C#实现剪刀石头布游戏

作者:必胜虾 时间:2021-11-10 05:19:32 

本文实例为大家分享了C#实现剪刀石头布游戏的具体代码,供大家参考,具体内容如下

游戏界面如下所示:

C#实现剪刀石头布游戏

首先我们必须知道要创建三个类玩家类,电脑类,裁判类

1、玩家类中的代码为


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

namespace PlayGame
{
class Player
{
public int showFist(string str)
{
int num = 0;
switch (str)
{
case "剪刀": num = 1; break;
case "石头": num = 2; break;
case "布": num = 3; break;

}
return num;

}
}
}

2、电脑类中的代码为


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

namespace PlayGame
{
class Computer
{
/// <summary>
/// 存储电脑出的拳头
/// </summary>
public string fist
{
get;
set;
}
public int cpuShowFist()
{
Random r = new Random();
int cnum=r.Next(1,4);
switch(cnum)
{
case 1: this.fist = "剪刀"; break;
case 2: this.fist = "石头"; break;
case 3: this.fist = "布"; break;
}
return cnum;
}
}
}

3、裁判类中的代码为


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

namespace PlayGame
{
class Judge
{
public enum Result
{
电脑赢,玩家赢,平局
}
public static Result caipan(int playerNum, int cpuNum)
{
if ((playerNum - cpuNum) == 1 || (playerNum - cpuNum) == -2)
return Result.玩家赢;
else if((playerNum-cpuNum)==0)
return Result.平局;
else
return Result.电脑赢;
}
}
}

4、其他的事件代码


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 PlayGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btncut_Click(object sender, EventArgs e)
{

NewMethod(btncut.Text);

}

private void NewMethod(string str)
{
lblPlayer.Text = str;
Player player = new Player();
int playerNum = player.showFist(str);

Computer cpu = new Computer();
int cpuNum = cpu.cpuShowFist();
lblComputer.Text = cpu.fist;

PlayGame.Judge.Result res = Judge.caipan(playerNum, cpuNum);
lblJudge.Text = res.ToString();
}

private void btnstone_Click(object sender, EventArgs e)
{
NewMethod(btnstone.Text);
}

private void btnbu_Click(object sender, EventArgs e)
{
NewMethod(btnbu.Text);
}
}
}

来源:https://blog.csdn.net/cxq_1993/article/details/46456391

标签:C#,剪刀石头布,游戏
0
投稿

猜你喜欢

  • Netty分布式pipeline管道创建方法跟踪解析

    2023-11-03 02:55:51
  • C#中ListView用法实例

    2021-10-15 06:10:24
  • java进阶解析Springboot上传excel存入数据库步骤

    2022-01-07 15:18:51
  • 详解C# ConcurrentBag的实现原理

    2022-11-17 02:26:05
  • SpringMvc后台接收json数据中文乱码问题详解

    2022-12-03 00:08:58
  • Spring Batch批处理框架使用解析

    2021-12-24 03:41:19
  • 深入浅出MappedByteBuffer(推荐)

    2023-11-14 19:59:43
  • Android文本输入框(EditText)输入密码时显示与隐藏

    2022-04-24 06:23:47
  • c#实现metro文件压缩解压示例

    2022-07-07 22:36:33
  • 详解如何在Android studio中更新sdk版本和build-tools版本

    2023-11-18 04:36:15
  • windows 部署JAVA环境安装iDea的详细步骤

    2022-10-09 01:09:26
  • java实现简单石头剪刀布小游戏

    2021-09-17 07:25:41
  • springboot拦截器Interceptor的使用,你都了解吗

    2023-01-01 21:53:40
  • Java 自定义Spring框架以及Spring框架的基本使用

    2021-05-29 19:35:57
  • Java中为什么start方法不能重复调用而run方法可以?

    2023-11-15 03:04:02
  • C# Web应用调试开启外部访问步骤解析

    2023-04-01 15:24:01
  • Maven中的SnapShot版本和正式版本的区别

    2023-11-03 16:31:46
  • C#实现简单记事本程序

    2022-07-20 23:52:37
  • java中Memcached的使用实例(包括与Spring整合)

    2021-11-28 13:29:06
  • springboot 自定义异常并捕获异常返给前端的实现代码

    2022-07-23 03:09:52
  • asp之家 软件编程 m.aspxhome.com