c#制作猜数字小游戏代码

  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 猜字小游戏
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19         //声明一个数组包含四个数,接下来储存4个随机数
 20         //public int[] correctnumber = new int[4];
 21         int num = 10;
 22         //判断游戏是否开始
 23         private static bool _beginflag;
 24         private void Form1_Load(object sender, EventArgs e)
 25         {
 26 
 27         }
 28 
 29         private void button1_Click(object sender, EventArgs e)
 30         {
 31             //表示游戏开始并清空
 32             _beginflag = true;
 33             onenum.Text = "";
 34             twonum.Text = "";
 35             threenum.Text = "";
 36             fournum.Text = "";
 37             //生成随机数,为数组每个值赋值
 38             Random rd = new Random();
 39             DBHelper.onenumber = rd.Next(1, 9).ToString();
 40             DBHelper.twonumber = rd.Next(1, 9).ToString();
 41             DBHelper.threenumber = rd.Next(1, 9).ToString();
 42             DBHelper.fournumber = rd.Next(1, 9).ToString();
 43             //判断四个数字是否有相同的数字,如果有相同数字重新获取随机数
 44             while (DBHelper.onenumber == DBHelper.twonumber ||
 45              DBHelper.onenumber == DBHelper.threenumber ||
 46               DBHelper.onenumber == DBHelper.fournumber
 47               )
 48             {
 49                 DBHelper.onenumber = rd.Next(1, 9).ToString();
 50             }
 51             while (DBHelper.twonumber == DBHelper.onenumber ||
 52              DBHelper.twonumber == DBHelper.threenumber ||
 53              DBHelper.twonumber == DBHelper.fournumber
 54               )
 55             {
 56                 DBHelper.twonumber = rd.Next(1, 9).ToString();
 57             }
 58             while (DBHelper.threenumber == DBHelper.onenumber ||
 59               DBHelper.threenumber == DBHelper.twonumber ||
 60              DBHelper.threenumber == DBHelper.fournumber
 61                )
 62             {
 63                 DBHelper.threenumber = rd.Next(1, 9).ToString();
 64             }
 65             while (DBHelper.fournumber == DBHelper.onenumber ||
 66              DBHelper.fournumber == DBHelper.twonumber ||
 67            DBHelper.fournumber == DBHelper.threenumber
 68               )
 69             {
 70                 DBHelper.fournumber = rd.Next(1, 9).ToString();
 71             }
 72            
 73             //点击开始游戏之后提示框提示信息
 74             textBox1.Text += "开始游戏..." + "\r\n";
 75             textBox1.Text += "提示信息..." + "\r\n";
 76         }
 77 
 78         
 79         private void button5_Click(object sender, EventArgs e)
 80         {
 81             //先判断游戏是否开始
 82             if (_beginflag==false)
 83             {
 84                 textBox1.Text += "游戏还没有开始..." + "\r\n";
 85             }
 86             else
 87             {
 88                 //传值
 89                 onenumber.Text = DBHelper.onenumber;
 90                 twonumber.Text = DBHelper.twonumber;
 91                 threenumber.Text = DBHelper.threenumber;
 92                 fournumber.Text = DBHelper.fournumber;
 93             }
 94            
 95         }
 96 
 97         //清空按钮
 98         private void button3_Click(object sender, EventArgs e)
 99         {
100             //清空操作
101             onenum.Text = "";
102             twonum.Text = "";
103             threenum.Text = "";
104             fournum.Text = "";
105         }
106 
107         //猜一猜按钮
108         private void button2_Click(object sender, EventArgs e)
109         {
110             //先判断游戏是否开始
111             if (_beginflag==false)
112             {
113             textBox1.Text += "游戏还没有开始..." + "\r\n";
114             }
115             //判断文本框是否为空,为空进行提示
116             if (_beginflag==true)
117             {
118                 if (onenum.Text==""||twonum.Text==""||threenum.Text==""||fournum.Text=="")
119                 {
120                     textBox1.Text += "各个数字均不能为空..." + "\r\n";
121                 }
122                 //答案正确的时候
123                 if (onenum.Text==DBHelper.onenumber&&twonum.Text==DBHelper.twonumber&&threenum.Text==DBHelper.threenumber&&fournum.Text==DBHelper.fournumber)
124                 {
125                     MessageBox.Show("恭喜你,挑战成功","提示");
126                     num = 10;
127                     textBox1.Text = "";
128                     onenum.Text = "";
129                     twonum.Text = "";
130                     threenum.Text = "";
131                     fournum.Text = "";
132                    
133                 }
134                 if (onenum.Text==DBHelper.onenumber&&(twonum.Text!=DBHelper.twonumber&&threenum.Text!=DBHelper.threenumber&&fournum.Text!=DBHelper.fournumber))
135                 {
136                     num=num-1;
137                     textBox1.Text += "第一位数字猜对了,第二,三,四位数字不正确..." + "\r\n";
138                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
139                 }
140                 if (twonum.Text == DBHelper.twonumber && (onenum.Text != DBHelper.onenumber && threenum.Text != DBHelper.threenumber && fournum.Text != DBHelper.fournumber))
141                 {
142                     num = num - 1;
143                     textBox1.Text += "第二位数字猜对了,第一,三,四位数字不正确..." + "\r\n";
144                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
145                 }
146                 if (threenum.Text == DBHelper.threenumber && (twonum.Text != DBHelper.twonumber && onenum.Text != DBHelper.onenumber && fournum.Text != DBHelper.fournumber))
147                 {
148                     num = num - 1;
149                     textBox1.Text += "第三位数字猜对了,第一,二,四位数字不正确..." + "\r\n";
150                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
151                 }
152                 if (fournum.Text == DBHelper.fournumber && (twonum.Text != DBHelper.twonumber && threenum.Text != DBHelper.threenumber && onenum.Text != DBHelper.onenumber))
153                 {
154                     num = num - 1;
155                     textBox1.Text += "第四位数字猜对了,第一,二,三位数字不正确..." + "\r\n";
156                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
157                 }
158                 if (fournum.Text != DBHelper.fournumber && (twonum.Text != DBHelper.twonumber && threenum.Text != DBHelper.threenumber && onenum.Text != DBHelper.onenumber))
159                 {
160                     num = num - 1;
161                     textBox1.Text += "很遗憾,四位数字都不正确..." + "\r\n";
162                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
163                 }
164                 if (onenum.Text==DBHelper.onenumber&&twonum.Text==DBHelper.twonumber&&(threenum.Text!=DBHelper.threenumber&&fournum.Text!=DBHelper.fournumber))
165                 {
166                     num = num - 1;
167                     textBox1.Text += "第一,二位数字猜对了,第三,四位数字不正确..." + "\r\n";
168                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
169                 }
170                 if (onenum.Text == DBHelper.onenumber && threenum.Text == DBHelper.threenumber && (twonum.Text != DBHelper.twonumber && fournum.Text != DBHelper.fournumber))
171                 {
172                     num = num - 1;
173                     textBox1.Text += "第一,三位数字猜对了,第二,四位数字不正确..." + "\r\n";
174                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
175                 }
176                 if (onenum.Text == DBHelper.onenumber && fournum.Text == DBHelper.fournumber && (threenum.Text != DBHelper.threenumber && twonum.Text != DBHelper.twonumber))
177                 {
178                     num = num - 1;
179                     textBox1.Text += "第一,四位数字猜对了,第二,三位数字不正确..." + "\r\n";
180                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
181                 }
182                 if (threenum.Text == DBHelper.threenumber && twonum.Text == DBHelper.twonumber && (onenum.Text != DBHelper.onenumber && fournum.Text != DBHelper.fournumber))
183                 {
184                     num = num - 1;
185                     textBox1.Text += "第二,三位数字猜对了,第一,四位数字不正确..." + "\r\n";
186                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
187                 }
188                 if (fournum.Text == DBHelper.fournumber && twonum.Text == DBHelper.twonumber && (threenum.Text != DBHelper.threenumber && onenum.Text != DBHelper.onenumber))
189                 {
190                     num = num - 1;
191                     textBox1.Text += "第二,四位数字猜对了,第一,三位数字不正确..." + "\r\n";
192                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
193                 }
194                 if (threenum.Text == DBHelper.threenumber && fournum.Text == DBHelper.fournumber && (onenum.Text != DBHelper.onenumber && twonum.Text != DBHelper.twonumber))
195                 {
196                     num = num - 1;
197                     textBox1.Text += "第三,四位数字猜对了,第一,二位数字不正确..." + "\r\n";
198                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
199                 }
200                 if (onenum.Text==DBHelper.onenumber&& twonum.Text==DBHelper.twonumber&&threenum.Text==DBHelper.threenumber&&fournum.Text!=DBHelper.fournumber)
201                 {
202                     num = num - 1;
203                     textBox1.Text += "第一,二,三位数字猜对了,第四位数字不正确..." + "\r\n";
204                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
205                 }
206                 if (onenum.Text == DBHelper.onenumber && twonum.Text == DBHelper.twonumber && threenum.Text != DBHelper.threenumber && fournum.Text == DBHelper.fournumber)
207                 {
208                     num = num - 1;
209                     textBox1.Text += "第一,二,四位数字猜对了,第三位数字不正确..." + "\r\n";
210                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
211                 }
212                 if (onenum.Text == DBHelper.onenumber && twonum.Text != DBHelper.twonumber && threenum.Text == DBHelper.threenumber && fournum.Text == DBHelper.fournumber)
213                 {
214                     num = num - 1;
215                     textBox1.Text += "第一,三,四位数字猜对了,第二位数字不正确..." + "\r\n";
216                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
217                 }
218                 if (onenum.Text != DBHelper.onenumber && twonum.Text == DBHelper.twonumber && threenum.Text == DBHelper.threenumber && fournum.Text == DBHelper.fournumber)
219                 {
220                     num = num - 1;
221                     textBox1.Text += "第二,三,四位数字猜对了,第一位数字不正确..." + "\r\n";
222                     textBox1.Text += "提示:您还剩余" + num + "次机会..." + "\r\n";
223                 }
224                 if (num==0)
225                 {
226                     MessageBox.Show("很遗憾,挑战失败","提示");
227                     textBox1.Text = "";
228                     onenum.Text = "";
229                     twonum.Text = "";
230                     threenum.Text = "";
231                     fournum.Text = "";
232                     num = 10;
233                 }
234             }
235         }
236 
237         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
238         {
239             onenum.Text = "";
240         }
241 
242         private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
243         {
244             twonum.Text = "";
245         }
246 
247         private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
248         {
249             threenum.Text = "";
250         }
251 
252         private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
253         {
254             fournum.Text = "";
255         }
256     }
257 }

 

posted on 2021-07-23 22:24  钟硕cool  阅读(601)  评论(0编辑  收藏  举报

导航