拼图游戏【简单】

代码:

  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.Windows.Forms;
  9 
 10 namespace WindowsFormsApplication7
 11 {
 12     public partial class Form1 : Form
 13     {
 14         //声明一个3*3的二维数组
 15         int[,] pos = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
 16         int ar = 0;//数组x轴
 17         int ac = 0;//数组y轴
 18         int r = 5;//图片位置x轴值
 19         int c = 5;//图片位置y轴值
 20         int i = 0;//第几个数
 21         int hi = 0;//记录按键次数
 22 
 23         public Form1()
 24         {
 25             InitializeComponent();
 26 
 27             //运行随机生成将图片打乱
 28             PiRandom();
 29         }
 30         //将图片打乱顺序
 31         private void PiRandom()
 32         {
 33             //定义一个随机对象
 34             Random ran = new Random();
 35             //声明一个随机数接收变量
 36             int f;
 37             //成功第几次
 38             while (i < 8)
 39             {
 40                 //得到随机数(大于0小于9之间的整数)
 41                 f = ran.Next(9);
 42 
 43                 //查看随机数的数据类型
 44                 //label10.Text =Convert.ToString( f.GetType());
 45 
 46                 //判断不重复
 47                 if (numNotExists(f) == true && f > 0)
 48                 {
 49                     pos[ar, ac] = f;
 50                     switch (f)
 51                     {
 52                         case 1:
 53                             label1.Location = new Point(r, c);
 54                             break;
 55                         case 2:
 56                             label2.Location = new Point(r, c);
 57                             break;
 58                         case 3:
 59                             label3.Location = new Point(r, c);
 60                             break;
 61                         case 4: ;
 62                             label4.Location = new Point(r, c);
 63                             break;
 64                         case 5:
 65                             label5.Location = new Point(r, c);
 66                             break;
 67                         case 6:
 68                             label6.Location = new Point(r, c);
 69                             break;
 70                         case 7:
 71                             label7.Location = new Point(r, c);
 72                             break;
 73                         case 8: ;
 74                             label8.Location = new Point(r, c);
 75                             break;
 76                     }
 77                     r += 100;
 78                     ac++;
 79                     if (r > 300)
 80                     {
 81                         r = 5;
 82                         c += 100;
 83                     }
 84                     if (ac > 2)
 85                     {
 86                         ac = 0;
 87                         ar++; ;
 88                     }
 89                     i++;
 90                 }
 91                 else continue;
 92             }
 93             label9.Location = new Point(r, c);
 94             pos[2, 2] = 9;
 95         }
 96         //判断数组中是否有该值
 97         private bool numNotExists(int f)
 98         {
 99             for (int x = 0; x < 3; x++)
100             {
101                 for (int y = 0; y < 3; y++)
102                 {
103                     if (pos[x, y] == f)
104                     {
105                         return false;
106                     }
107                 }
108             }
109             return true;
110         }
111         //按键按下发生的事件(控制的是labl9这个控件)
112         private void Form1_KeyDown(object sender, KeyEventArgs e)
113         {
114             int temp;
115             //判断按键码
116             switch ((int)e.KeyCode)
117             {
118                 //
119                 case 38://
120                     if (ar < 2)
121                     {
122                         temp = pos[ar, ac];
123                         pos[ar, ac] = pos[ar + 1, ac];
124                         pos[ar + 1, ac] = temp;
125                         Swap(pos[ar, ac], temp);//调用交换控件
126                         hi++;//按键次数增加
127                         ar++;//记录当前0在数组中的位置
128                     }
129                     break;
130                 case 39://
131                     if (ac >0)
132                     {
133                         temp = pos[ar, ac];
134                         pos[ar, ac] = pos[ar, ac-1];
135                         pos[ar, ac-1] = temp;
136                         Swap(pos[ar, ac], temp);//调用交换控件
137                         hi++;//按键次数增加
138                         ac--;//记录当前0在数组中的位置
139                     }
140                     break;
141                 case 40://
142                     if (ar >0)
143                     {
144                         temp = pos[ar, ac];
145                         pos[ar, ac] = pos[ar - 1, ac];
146                         pos[ar - 1, ac] = temp;
147                         Swap(pos[ar, ac], temp);//调用交换控件
148                         hi++;//按键次数增加
149                         ar--;//记录当前0在数组中的位置
150                     }
151                     break;
152                 case 37://
153                     if (ac<2)
154                     {
155                         temp = pos[ar, ac];
156                         pos[ar, ac] = pos[ar, ac + 1];
157                         pos[ar, ac + 1] = temp;
158                         Swap(pos[ar, ac], temp);//调用交换控件
159                         hi++;//按键次数增加
160                         ac++;//记录当前0在数组中的位置
161                     }
162                     break;
163             }
164             label10.Text = hi.ToString();
165 
166             Boolean b = true;
167             int s = 1;
168             //for查看是否排列完成
169             for (int x = 0; x < 3; x++)
170             {
171                 for (int y = 0; y < 3; y++)
172                 {
173                     if (pos[x, y] != s)
174                     {
175                         b = false;
176                         break ;
177                     }
178                     s++;
179                 }
180             }
181             if(b)
182             {
183                 MessageBox.Show("一共移动了"+hi.ToString()+"次!!!","游戏结束");
184             }
185         }
186         //控件交换
187         private void Swap(int p, int t)
188         {
189             Point temp;
190             switch (p)
191             {
192                 case 1:
193                     temp = label1.Location;
194                     label1.Location = label9.Location;
195                     label9.Location = temp;
196                     break;
197                 case 2:
198                     temp = label2.Location;
199                     label2.Location = label9.Location;
200                     label9.Location = temp;
201                     break;
202                 case 3:
203                     temp = label3.Location;
204                     label3.Location = label9.Location;
205                     label9.Location = temp;
206                     break;
207                 case 4:
208                     temp = label4.Location;
209                     label4.Location = label9.Location;
210                     label9.Location = temp;
211                     break;
212                 case 5:
213                     temp = label5.Location;
214                     label5.Location = label9.Location;
215                     label9.Location = temp;
216                     break;
217                 case 6:
218                     temp = label6.Location;
219                     label6.Location = label9.Location;
220                     label9.Location = temp;
221                     break;
222                 case 7:
223                     temp = label7.Location;
224                     label7.Location = label9.Location;
225                     label9.Location = temp;
226                     break;
227                 case 8:
228                     temp = label8.Location;
229                     label8.Location = label9.Location;
230                     label9.Location = temp;
231                     break;
232             }
233         }
234     }
235 }

 【本篇新知识】:

  Point 是表示二维平面中定义的一个点的 x 和 y 坐标的有序对; 

  label9.Location;//控件坐标
原文地址:https://www.cnblogs.com/pengyouqiang88/p/5131682.html