首页 > 代码库 > 简单版猜拳游戏

简单版猜拳游戏

界面很简单

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//玩家类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 猜拳游戏
{
    /// <summary>
    /// 玩家类
    /// </summary>
   public class Player
    {
       //出拳结果
       public string FistName { get; set; }
       public int Play(string fistName)
       {
           this.FistName = fistName;
           int num = 0;
           switch (fistName)
           {
               case "石头": num = 1; break;
               case "剪刀": num = 2; break;
               case "布": num = 3; break;
           }
           return num;
       }
    }
}
 
//电脑类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 猜拳游戏
{
    /// <summary>
    /// 电脑类
    /// </summary>
    public class Computer
    {
        //出拳结果
        public string FistName { get; set; }
        public int Play()
        {
            //随机生成
            Random r = new Random();
            int num = r.Next(1, 4);
            switch (num)
            {
                case 1: this.FistName = "石头"; break;
                case 2: this.FistName = "剪刀"; break;
                case 3: this.FistName = "布"; break;
            }
            return num;
        }
    }
}
 
//裁判
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 猜拳游戏
{
    /// <summary>
    /// 裁判类
    /// </summary>
   public class Judge
    {
       public string Win(int playNum, int computerNum)
       {
           if (playNum - computerNum == -1 || playNum - computerNum == 2)
           {
               return "玩家赢";
           }
           else if (playNum - computerNum == 0)
           {
               return "平手";
           }
           else
           {
               return "电脑赢";
           }
       }
    }
}
 
//窗体
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 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnStone_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            StartGame(btn.Text);
        }
        private void StartGame(string str)
        {
            Player player = new Player();
            int playerNum = player.Play(str);
            lbPlay.Text = player.FistName;
 
 
            Computer computer = new Computer();
            int computerNum = computer.Play();
            lbComputer.Text = computer.FistName;
            Judge jud = new Judge();
            lbRes.Text = jud.Win(playerNum, computerNum);
        }
    }
}