第二个案例(猜拳游戏)
来源:互联网 发布:海量 top k算法 编辑:程序博客网 时间:2024/06/05 19:34
添加组件
Content Name:labPlayercontent Name:labComputerPlayer Name:labResult
Player类的实现
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Guess{ public class Player { //自动属性// public string Punch { get; set; } public int Play(string punch) { this.Punch = punch; int num = 0; switch (punch) { case "tone": num = 1; break; case "jian": num = 2; break; case "bu": num = 3; break; } return num; } }}
Computer类的实现
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Guess{ public class Computer { public string Punch { get; set; } public int Play() { Random r = new Random();//随机数// int num=r.Next(1,4); switch(num) { case 1: this.Punch = "tone"; break; case 2: this.Punch = "jian"; break; case 3: this.Punch = "bu"; break; } return num; } }}
Referee(裁判)类的实现
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Guess{ public class Referee { //判断谁赢谁输 public string Win(int playerNum,int computerNum) { if(playerNum-computerNum==0) { return "double win"; } else if(playerNum-computerNum==-1||playerNum-computerNum==2) { return "player win"; } else { return "computer win"; } } }}
主程序的实现
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Guess{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //选中需要重构的代码,右键,Refactor->Extract Method private void Play(string name) { Player p = new Player(); int pNum = p.Play(name); labPlayer.Text = p.Punch; Computer c = new Computer(); int cNum = c.Play(); labComputer.Text = p.Punch; Referee r = new Referee(); string result = r.Win(pNum, cNum); labresult.Text = result; } //在图形化界面中,把各个按钮的click事件绑定该函数 private void button_Click(object sender, EventArgs e) { Button btn = sender as Button; Play(btn.Text); } }}
阅读全文
0 0
- 第二个案例(猜拳游戏)
- android开发------案例二(猜拳游戏)
- 猜拳游戏案例
- 猜拳游戏(单击版)
- 猜拳游戏(函数调用)
- 人机猜拳游戏(修改)
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- 猜拳游戏
- iOS开发---使用Xcode8 添加Launch Image(启动图片)
- was compiled with optimization
- android 拼图小游戏
- mysql常用语句
- Android Studio更新成2.3以后Gradle大坑拯救法!
- 第二个案例(猜拳游戏)
- 六、rabbitMQ Routing
- 利用数组拼接来代替连表查询
- 学习笔记1
- muse-ui上手 图标不显示问题
- Python如何引入自定义模块?
- Java注解(Annotation)详解(一)——概述及JDK自带注解
- FatFS文件系统详解-附移植建议
- lower_bound和upper_bound