标签:dom .com mod ons span .text initial result back
Form
封装方法:将需要封装成方法的代码选中,右键-重构-提取方法
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 _5.石头剪刀布游戏
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = "剪刀";
Game(str);
}
private void Game(string str)
{
lblPlayer.Text = str;
Computer computer = new Computer();
int computerNum = computer.ShowFist(ref lblComputer);
Player player = new Player();
int playerNum = player.ShowFist(str);
Result res = Judge.showJudge(playerNum, computerNum);
lblResult.Text = res.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
string str = "石头";
Game(str);
}
private void button3_Click(object sender, EventArgs e)
{
string str = "布";
Game(str);
}
}
}
Palyer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _5.石头剪刀布游戏
{
class Player
{
public int ShowFist(string str)
{
switch(str)
{
case "剪刀":
return 1;
break;
case "石头":
return 2;
case "布":
return 3;
default :
return 0;
}
}
}
}
Computer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _5.石头剪刀布游戏
{
class Computer
{
public string Fist
{
get;
set;
}
public int ShowFist(ref Label lblComputer)
{
Random r = new Random();
int rNum = r.Next(1, 4);
switch (rNum)
{
case 1:
Fist = "剪刀";
break;
case 2:
this.Fist = "石头";
break;
case 3:
this.Fist = "布";
break;
}
lblComputer.Text = this.Fist;
return rNum;
}
}
}
Judge.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _5.石头剪刀布游戏
{
public enum Result
{
玩家赢,
电脑赢,
平局
}
class Judge
{
public static Result showJudge(int palyer,int computer)
{
int res = palyer - computer;
if (res == -2 || res == 2 || res == 1)
return Result.玩家赢;
else if (res == 0)
{
return Result.平局;
}
else
return Result.电脑赢;
}
}
}
标签:dom .com mod ons span .text initial result back
原文地址:http://www.cnblogs.com/my-cat/p/7418697.html