Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _116_石头剪刀布
{
static class Program
{
public static double WinRate=0;
public static int Win=0;
public static int Failure=0;
public static int Draw=0;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _116_石头剪刀布
{
public enum enumResult
{
玩家赢,
电脑赢,
平局
}
public class Result
{
private int _win;
public int Win
{
get { return _win; }
set { _win = value; }
}
private int _faileure;
public int Failure
{
get { return _faileure; }
set { _faileure = value; }
}
private double _winRate;
public double WinRate
{
get { return _winRate; }
set { _winRate = value; }
}
public int Draw { get => _draw; set => _draw = value; }
private int _draw;
public enumResult Judge(int playerNumber, int computerNumber)
{
int sum;
if (playerNumber - computerNumber == -1 || playerNumber - computerNumber == 2)
{
Program.Win++;
sum = Program.Win + Program.Failure + Program.Draw;
WinRate = (double)Program.Win / sum;
return enumResult.玩家赢;
}
else if (playerNumber == computerNumber)
{
Program.Draw++;
sum = Program.Win + Program.Failure + Program.Draw;
WinRate = (double)Program.Win / sum;
return enumResult.平局;
}
else
{
Program.Draw++;
sum = Program.Win + Program.Failure + Program.Draw;
WinRate =(double) Program.Win / sum;
return enumResult.电脑赢;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _116_石头剪刀布
{
class Computer
{
/// <summary>
/// 存储电脑出的手势
/// </summary>
public string Fist { get; set; }
public int ShowFist()
{
Random r = new Random();
int rNumber = r.Next(1, 4);
switch (rNumber)
{
case 1: this.Fist = "石头"; break;
case 2: this.Fist = "剪刀"; break;
case 3: this.Fist = "布"; break;
default:
break;
}
return rNumber;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _116_石头剪刀布
{
class Player
{
public int ShowFist(string fist)
{
int num = 0;
switch (fist)
{
case "石头": num = 1; break;
case "剪刀": num = 2; break;
case "布": num = 3; break;
}
return num;
}
}
}
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 _116_石头剪刀布
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStone_Click(object sender, EventArgs e)
{
PlayGame(btnStone.Text);
}
private void BtnCut_Click(object sender, EventArgs e)
{
PlayGame(BtnCut.Text);
}
private void btnCloth_Click(object sender, EventArgs e)
{
PlayGame(btnCloth.Text);
}
/// <summary>
/// 点击石头剪刀布判断输赢
/// </summary>
/// <param name="text"></param>
private void PlayGame(string text)
{
//将btnStone的值给lblPlayer
lblPlayer.Text = text;
//调用Player类的ShowFist方法,给玩家出的手势赋int类型的值
Player player = new Player();
int playerNumber = player.ShowFist(lblPlayer.Text);
//调用Computer类的ShowFist方法,给电脑出的手势赋int类型的值
Computer computer = new Computer();
int computerNumber = computer.ShowFist();
//给lblComputer赋值computer.Fist属性
lblComputer.Text = computer.Fist;
//调用Result类的Judge方法,判断输赢
Result result = new Result();
enumResult res = result.Judge(playerNumber, computerNumber);
lblResult.Text = res.ToString();
//胜率
lblWinRate.Text = result.WinRate.ToString();
}
private void label5_Click(object sender, EventArgs e)
{
}
}
}