C#实现剪刀石头布游戏
作者:必胜虾 时间:2021-11-10 05:19:32
本文实例为大家分享了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
投稿
猜你喜欢
springboot操作静态资源文件的方法
2022-07-13 06:29:11
C#中DataSet、DataTable、DataRow数据的复制方法
2021-10-26 20:10:31
Java实现接口的枚举类示例
2023-06-18 01:22:04
SpringMVC @GetMapping注解路径冲突问题解决
2022-12-18 01:43:17
Android开发必备知识 为什么说Kotlin值得一试
2022-02-09 08:41:38
java 类加载与自定义类加载器详解
2022-05-21 04:31:31
详解Java关键字final
2023-11-29 09:10:27
详解java8在Collection中新增加的方法removeIf
2022-06-04 20:51:45
Android实现界面左右滑动切换功能
2022-08-30 02:00:58
详解Spring cloud使用Ribbon进行Restful请求
2021-07-09 11:05:28
Java8中Optional类型和Kotlin中可空类型的使用对比
2023-07-29 07:49:21
Android实现类似360,QQ管家那样的悬浮窗
2022-07-04 02:58:05
Java基础之从HelloWorld到面向对象
2022-11-23 11:55:06
SpringMVC 跨重定向请求传递数据的方法实现
2022-10-18 06:33:08
Android 模仿QQ侧滑删除ListView功能示例
2023-10-27 21:03:43
详解Java利用深度优先遍历解决迷宫问题
2022-08-20 02:46:54
浅谈byte和长度为8的boolean数组互相转换
2023-11-07 00:34:37
浅析C#中静态方法和非静态方法的区别
2023-04-07 07:55:15
c# 网址压缩简单实现短网址
2022-10-06 15:37:17
Android提高之MediaPlayer音视频播放
2021-05-29 08:44:37