C# Winform学习--- 实现石头剪刀布的游戏
本文使用winform实现简单的石头剪刀布的游戏,主要实现,电脑随机出拳,玩家手动点击出拳;实现简易背景图片3秒切换;简易统计信息。
1、效果图




2.实现代码
新建一个windows窗体程序,用数字1代表石头,用数字2代表剪刀,用数字3代表布,结果取玩家和电脑出拳之差,有三种结果
- 玩家赢: -1,2
- 平手: 0
- 玩家输: 其它值
新建3个类:
1)Computer.cs 电脑随机出拳
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 石头剪刀布
{
class Computer
{ public string Fist
{
get;
set;
} public int ShowFist()
{
Random rnd = new Random();
int fist = rnd.Next(, );
switch (fist)
{
case : Fist = "石头"; break;
case : Fist = "剪刀"; break;
case : Fist = "布"; break;
}
return fist;
} }
}
2)、Judge.cs 裁判类 判断输赢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 石头剪刀布
{ class Judge
{ public enum RESULT
{
玩家赢,
电脑赢,
平手
} public static RESULT WhoWin(int playerNum, int computerNum)
{
int result = playerNum - computerNum;
if (result == - || result == )
{
return RESULT.玩家赢;
}
else if (result == )
{
return RESULT.平手; }
else
{
return RESULT.电脑赢;
} } }
}
3)、Player.cs 玩家,出拳
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 石头剪刀布
{
class Player
{
public static int ShowFist(string fist)
{
switch (fist)
{
case "石头": return ;
case "剪刀": return ;
case "布": return ;
default: return ;
}
} }
}
界面后台实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 石头剪刀布
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 点击石头按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStone_Click(object sender, EventArgs e)
{
String fist = "石头";
Game(fist);
}
/// <summary>
/// 点击剪刀按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnScissors_Click(object sender, EventArgs e)
{
String fist = "剪刀";
Game(fist);
}
/// <summary>
/// 点击布按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCloth_Click(object sender, EventArgs e)
{
String fist = "布";
Game(fist); } //背景图片轮播
String[] paths = Directory.GetFiles(@"C:\work\stone");//此目录里面必须有图片,否则会报错
private void timer1_Tick(object sender, EventArgs e)
{
this.BackgroundImage = Image.FromFile(paths[new Random().Next(, paths.Length)]); }
static int playerWinTimes = ;//玩家赢的次数
static int gameTimes = ;//总共次数
static int tieTimes = ;//平手次数 /// <summary>
/// 通用方法
/// </summary>
/// <param name="fist"></param>
private void Game(String fist)
{
gameTimes++;
lbPlayer.Text = fist;
int playerNum = Player.ShowFist(fist);
Computer cpu = new Computer();
int cpuNum = cpu.ShowFist();
lbComputer.Text = cpu.Fist;
Judge.RESULT result = Judge.WhoWin(playerNum, cpuNum);
lbJudge.Text = result.ToString();
lbStatistics.Text = "统计信息:\n\n1.您赢了" + playerWinTimes + "场比赛!\n\n" + "2.平手了" + tieTimes + "次; \n\n" + "3.输掉了" + (gameTimes - playerWinTimes - tieTimes) + "场比赛; \n\n" + "4.共进行了" + gameTimes + "场比赛!\n\n"; if (result == Judge.RESULT.玩家赢)
{
playerWinTimes++;
MessageBox.Show("恭喜,您已经赢了" + playerWinTimes + "场比赛!" + " 共进行了" + gameTimes + "场比赛!");
}
else if (result == Judge.RESULT.平手)
{
tieTimes++;
} } }
}
实现游戏的难点在于要想到将石头剪刀布用数字来替换,如果逻辑通了实现起来并不难。
本文源码:https://github.com/amosli/CSharp/tree/stone
C# Winform学习--- 实现石头剪刀布的游戏的更多相关文章
- 微信小程序开发入门学习(1):石头剪刀布小游戏
从今天起开始捣鼓小程序了2018-12-17 10:02:15 跟着教程做了第一个入门实例有兴趣的朋友可以看看: 猜拳游戏布局 程序达到的效果 猜拳游戏的布局是纵向显示了三个组件:文本组件(tex ...
- Winform学习手册(目录)
一.基础: WINFORM学习笔记——创建Winform项目 WINFORM学习手册——TextBox.Lable.Button WINFORM学习笔记——窗体生命周期 WINFORM学习手册——对话 ...
- winform小程序---猜拳小游戏
因为学的时间不长,所以借鉴了一些资料做了这个小程序,大家共同学习,共同进步.感觉很有自信,世上无难事,只怕有心人. using System; using System.Collections.Gen ...
- java学习之坦克大战游戏
总结:由于这几天快过年比较忙然后没怎么写,写代码途中一些经验总结现在给忘记了.这次的小项目感觉比上次写的思路清楚了点.没有之前第一次写那么逻辑混乱,结构也搞的比之前的要好,添加功能比较容易.学习了之前 ...
- 【Unity 3D】学习笔记29:游戏的例子——简单的小制作地图
无论学习.只看不练是坏科学. 因此,要总结回想这怎么生产MMROPG小地图的游戏.于MMROPG游戏类,在游戏世界中行走时导致各地,通常在屏幕的右上角,将有一个区域,以显示当前的游戏场景微缩.在游戏世 ...
- 【Visual C++】游戏编程学习笔记之三:游戏循环的使用
本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44208419 作者:Zee ...
- 增强学习训练AI玩游戏
1.游戏简介 符号A为 AI Agent. 符号@为金币,AI Agent需要尽可能的接取. 符号* 为炸弹,AI Agent需要尽可能的躲避. 游戏下方一组数字含义如下: Bomb hit: 代表目 ...
- C语言编程学习打造——做题游戏
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- python学习之掷骰子游戏
""" 通过学习的python知识,写一个简单的python小游戏 游戏名字:掷骰子比大小 游戏规则: 1.玩家可以选择玩掷几个骰子游戏(默认3个) 2.玩家可以设置双方 ...
随机推荐
- 常用 C#操作字符串方法
staticvoid Main(string[] args) { string s =""; //(1)字符访问(下标访问s[i]) s ="ABCD"; Co ...
- 如何获取TypedArray?
当我们需要自定义控件的时候经常会使用到TypedArray这个类,使用完之后必须调用recycler()函数.... 但是如何获取呢? 有如下几个方式: context(实际最后调用的是context ...
- 用php怎么改文件名
php手册:bool rename ( string oldname, string newname [, resource context] )尝试把 oldname 重命名为 newname. 如 ...
- nodeJs 模块cookie-session api文档中文翻译,偶自己翻译的
原文英文文档链接点击 说明 简单的 基于cookie的 session中间件 安装 它是一个可以用npm注册的node模块,可以通过npm install命令安装 npm install cookie ...
- THINKPHP中关于接口问题(客户端)
一 apk版本号 客户端发送get请求访问服务器端的控制器方法,通过用户传过来的用户名和密码. 二 服务器端通过客户端传入的user and password 去数据库进行查询,Success就生成 ...
- cs11_c++_lab6
expressions.hh #ifndef EXPRESSIONS_HH #define EXPRESSIONS_HH #include "environment.hh" #in ...
- 1117 冲刺一(Day 1)
冲刺一(第一天) 项目需求确定 现阶段我们进行的项目是到店点餐系统.主要是开发手机端app为用户提供方便快捷的点餐服务.免去顾客到店后遇到因吃饭的人太多而找不到服务人员点餐的窘境.减少了服务人员因为忙 ...
- 移动端自动化环境搭建-RIDE的安装
A.安装依赖 RIDE 是 Robot Framework 测试数据的编辑器.它使测试用例的创建.运行.测试项目的组织可以在图形界面下完成. B.安装过程 下载地址:https://pypi.pyth ...
- 时间序列分析之ARIMA模型预测__R篇
http://www.cnblogs.com/bicoffee/p/3838049.html
- Hex string convert to Binary String and Vise-Versa(16进制和2进制字符串的相互转换)
这个转换在我们日常的编码中还是很有机会遇到的,这里贴出来和大家分享探讨. void pu_hex_to_binary(std::string strHex, std::string &strB ...