winform小程序---猜拳小游戏

时间:2023-03-08 17:26:43

因为学的时间不长,所以借鉴了一些资料做了这个小程序,大家共同学习,共同进步。感觉很有自信,世上无难事,只怕有心人。

 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 Test_game_猜拳
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
shitou();
} private void button2_Click(object sender, EventArgs e)
{
jiandao();
} private void button3_Click(object sender, EventArgs e)
{
bu();
} //定义石头方法
public void shitou()
{
label2.Text = button1.Text;
label5.Text = bj(toint(),pc());
}
//定义剪刀方法
public void jiandao()
{
label2.Text = button2.Text;
label5.Text = bj(toint(), pc());
}
//定义布方法
public void bu()
{
label2.Text = button3.Text;
label5.Text = bj(toint(), pc());
} //将石头剪刀布转换为数值
public int toint()
{
int n;
switch (label2.Text)
{
case "石头":
n = ;
break;
case "剪刀":
n = ;
break;
case "布":
n = ;
break;
default:
throw new Exception("未知错误"); }
return n;
} //电脑随机产生数字
public int pc()
{
Random ran = new Random();
int vpc = ran.Next(,);//random的Next方法取最小值不取最大值
string strpc = string.Empty;
switch (vpc)
{
case :
strpc = "石头";
break;
case :
strpc = "剪刀";
break;
case :
strpc = "布";
break;
default:
throw new Exception("未知错误");
}
label4.Text = strpc;
return vpc;
} //将pc的值和toint的值对比
public string bj(int user, int pc)
{
int temp = user - pc;
string bj = string.Empty;
if (temp == - || temp == )
{
bj= "你赢了";
}
else if (temp == )
{
bj = "平局";
}
else
{
bj = "你输了";
}
return bj;
} }
}