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;
using System.Collections;
namespace caiquan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//电脑随机出拳 在txt2中显示
public string strPC = "";
public string PC()
{
Random ran = new Random();
int n = ran.Next(1, 4);
if (n == 1)
{
strPC = "石头";
}
else if (n == 2)
{
strPC = "剪刀";
}
else if (n == 3)
{
strPC = "布";
}
else
{
throw new Exception("未知错误!");
}
txt2.Text = strPC;
return strPC;
}
//玩家出拳 点击按钮
private void btn1_Click(object sender, EventArgs e)
{
string shitou = btn1.Text;
txt1.Text = shitou;
int vPlayer = changetoValue(shitou);
PC();
int vPC = changetoValue(strPC);
Compare(vPlayer, vPC);
}
private void btn2_Click(object sender, EventArgs e)
{
string jiandao = btn2.Text;
txt1.Text = jiandao;
int vPlayer = changetoValue(jiandao);
PC();
int vPC = changetoValue(strPC);
Compare(vPlayer, vPC);
}
private void btn3_Click(object sender, EventArgs e)
{
string bu = btn3.Text;
txt1.Text = bu;
int vPlayer = changetoValue(bu);
PC();
int vPC = changetoValue(strPC);
Compare(vPlayer, vPC);
}
//写一个将鼠标点击事件 将字符串转换成相应数值的方法
public int changetoValue(string str)
{
int n = 0;
//对传入的字符串进行判断
//如果为石头 则给n 赋予值1
if (str == "石头")
{
n = 1;
}
else if (str == "剪刀")
{
n = 2;
}
else if (str == "布")
{
n = 3;
}
else { throw new Exception("未知错误"); }
return n;
}
//比较选手与电脑的值
public void Compare(int Player,int PC)
{
//赢的情况有2种 1种是 玩家比电脑值小1 一种是小2
if (Player - PC == -1 || Player - PC == 2)
{
txt3.Text = "恭喜您!您赢了!";
}
else if (Player - PC == 0)
{
txt3.Text = "您和电脑出的一样,这局平!";
}
else
{
txt3.Text = "很遗憾!您输了!";
}
}
}
}