首页 > 代码库 > winform小程序---猜拳小游戏

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

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

  1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Linq;  7 using System.Text;  8 using System.Threading.Tasks;  9 using System.Windows.Forms; 10  11 namespace Test_game_猜拳 12 { 13     public partial class Form1 : Form 14     { 15         public Form1() 16         { 17             InitializeComponent(); 18         } 19  20         private void button1_Click(object sender, EventArgs e) 21         { 22             shitou(); 23         } 24  25         private void button2_Click(object sender, EventArgs e) 26         { 27             jiandao(); 28         } 29  30         private void button3_Click(object sender, EventArgs e) 31         { 32             bu(); 33         } 34  35         //定义石头方法 36         public void shitou() 37         { 38             label2.Text = button1.Text; 39             label5.Text = bj(toint(),pc()); 40         } 41         //定义剪刀方法 42         public void jiandao() 43         { 44             label2.Text = button2.Text; 45             label5.Text = bj(toint(), pc()); 46         } 47         //定义布方法 48         public void bu() 49         { 50             label2.Text = button3.Text; 51             label5.Text = bj(toint(), pc()); 52         } 53  54         //将石头剪刀布转换为数值 55         public int toint() 56         { 57             int n; 58             switch (label2.Text) 59             { 60                 case "石头": 61                     n = 1; 62                     break; 63                 case "剪刀": 64                     n = 2; 65                     break; 66                 case "": 67                     n = 3; 68                     break; 69                 default: 70                     throw new Exception("未知错误"); 71  72             } 73             return n; 74         } 75  76         //电脑随机产生数字 77         public int pc() 78         { 79             Random ran = new Random(); 80             int vpc = ran.Next(1,4);//random的Next方法取最小值不取最大值 81             string strpc = string.Empty; 82             switch (vpc) 83             { 84                 case 1: 85                     strpc = "石头"; 86                     break; 87                 case 2: 88                     strpc = "剪刀"; 89                     break; 90                 case 3: 91                     strpc = ""; 92                     break; 93                 default: 94                     throw new Exception("未知错误"); 95             } 96             label4.Text = strpc; 97             return vpc; 98         } 99 100         //将pc的值和toint的值对比101         public string bj(int user, int pc)102         {103             int temp = user - pc;104             string bj = string.Empty;105             if (temp == -1 || temp == 2)106             {107                 bj= "你赢了";108             }109             else if (temp == 0)110             {111                 bj = "平局";112             }113             else114             {115                 bj = "你输了";116             }117             return bj;118         }119 120     }121 }

 

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