代码改变世界

拼图游戏

  精诚所至 金石为开  阅读(753)  评论(2编辑  收藏  举报

本例创建一个简单的拼图游戏,将一个打乱的图形拼合完整,本例用数字替代图形,游戏界面如图。

20120409200851

程序代码如下。

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace eg50_pintu
{
    public partial class MainForm : Form
    {
        int flag;
        private int count;
        private int[] numbers=new int[16];
        public MainForm()
        {
            InitializeComponent();
        }
         
        void T1KeyDown(object sender, KeyEventArgs e)
        {
            if(flag==0)
            {
                string keyId=e.KeyCode.ToString();
                switch(keyId)
                {
                        case "Right":LeftPress();
                        break;
                        case "Left":RightPress();
                        break;
                        case "Down":UpPress();
                        break;
                        case "Up":DownPress();
                        break;
                }
            }
        }
        private void assign()
        {
            if(numbers[0]!=-1)
                b1.Text=numbers[0].ToString();
            else
            {
                b1.Text="*";
                b1.Visible=false;
            }
            if(numbers[1]!=-1)
                b2.Text=numbers[1].ToString();
            else
            {
                b2.Text="*";
                b2.Visible=false;
            }
            if(numbers[2]!=-1)
                b3.Text=numbers[2].ToString();
            else
            {
                b3.Text="*";
                b3.Visible=false;
            }
            if(numbers[3]!=-1)
                b4.Text=numbers[3].ToString();
            else
            {
                b4.Text="*";
                b4.Visible=false;
            }
            if(numbers[4]!=-1)
                b5.Text=numbers[4].ToString();
            else
            {
                b5.Text="*";
                b5.Visible=false;
            }
            if(numbers[5]!=-1)
                b6.Text=numbers[5].ToString();
            else
            {
                b6.Text="*";
                b6.Visible=false;
            }
            if(numbers[6]!=-1)
                b7.Text=numbers[6].ToString();
            else
            {
                b7.Text="*";
                b7.Visible=false;
            }
            if(numbers[7]!=-1)
                b8.Text=numbers[7].ToString();
            else
            {
                b8.Text="*";
                b8.Visible=false;
            }
            if(numbers[8]!=-1)
                b9.Text=numbers[8].ToString();
            else
            {
                b9.Text="*";
                b9.Visible=false;
            }
            if(numbers[9]!=-1)
                b10.Text=numbers[9].ToString();
            else
            {
                b10.Text="*";
                b10.Visible=false;
            }
            if(numbers[10]!=-1)
                b11.Text=numbers[10].ToString();
            else
            {
                b11.Text="*";
                b11.Visible=false;
            }
            if(numbers[11]!=-1)
                b12.Text=numbers[11].ToString();
            else
            {
                b12.Text="*";
                b12.Visible=false;
            }
            if(numbers[12]!=-1)
                b13.Text=numbers[12].ToString();
            else
            {
                b13.Text="*";
                b13.Visible=false;
            }
            if(numbers[13]!=-1)
                b14.Text=numbers[13].ToString();
            else
            {
                b14.Text="*";
                b14.Visible=false;
            }
            if(numbers[14]!=-1)
                b15.Text=numbers[14].ToString();
            else
            {
                b15.Text="*";
                b15.Visible=false;
            }
            if(numbers[15]!=-1)
                b16.Text=numbers[15].ToString();
            else
            {
                b16.Text="*";
                b16.Visible=false;
            }
        }
        public void RightPress()
        {
            int chk;
            int loc;
            int swap;
            loc=locator();
            if(((loc+1)%4)==0)
                return;
            swap=numbers[loc+1];
            numbers[loc+1]=-1;
            numbers[loc]=swap;
            MakeVisible();
            assign();
            chk=CheckForCompletion();
            lbl3.Text=count.ToString();
            if(chk==0)
            {
                count++;
                lbl3.Text=count.ToString();
            }
            if(chk==1)
            {
                flag=1;
                lbl1.Visible=true;
            }
        }
        public void LeftPress()
        {
            int chk;
            int loc;
            int swap;
            loc=locator();
            if((loc%4)==0)
                return;
            swap=numbers[loc-1];
            numbers[loc-1]=-1;
            numbers[loc]=swap;
            MakeVisible();
            assign();
            chk=CheckForCompletion();
            lbl3.Text=count.ToString();
            if(chk==0)
            {
                count++;
                lbl3.Text=count.ToString();
            }
            if(chk==1)
            {
                flag=1;
                lbl1.Visible=true;
            }
        }
        public void UpPress()
        {
            int chk;
            int loc;
            int swap;
            loc=locator();
            if(loc<=3)
                return;
            swap=numbers[loc-4];
            numbers[loc]=swap;
            MakeVisible();
            assign();
            chk=CheckForCompletion();
            lbl3.Text=count.ToString();
            if(chk==0)
            {
                count++;
                lbl3.Text=count.ToString();
            }
            if(chk==1)
            {
                flag=1;
                lbl1.Visible=true;
            }
        }
        public void DownPress()
        {
            int chk;
            int loc;
            int swap;
            loc=locator();
            if(loc>1)
                return;
            swap=numbers[loc+4];
            numbers[loc]=swap;
            MakeVisible();
            assign();
            chk=CheckForCompletion();
            lbl3.Text=count.ToString();
            if(chk==0)
            {
                count++;
                lbl3.Text=count.ToString();
            }
            if(chk==1)
            {
                flag=1;
                lbl1.Visible=true;
            }
        }
        private int locator()
        {
            int i;
            for(i=0;i<=15;i++)
            {
                if(numbers[i]==-1)
                    break;
                else continue;
            }
            return i;
        }
        private int CheckForCompletion()
        {
            int j;
            for(int i=0;i<=15;i++)
            {
                if(numbers[i]==-1)
                    numbers[i]=16;
            }
            for (j=0;j<=14;j++)
            {
                if(numbers[j]>numbers[j+1])
                    break;
            }
            if(j==15)
                return 1;
            else
            {
                for(int i=0;i<=15;i++)
                {
                    if(numbers[i]==16)
                        numbers[i]=-1;
                }
                return 0;
            }
        }
        private void MakeVisible()
        {
            b1.Visible=true;
            b2.Visible=true;
            b3.Visible=true;
            b4.Visible=true;
            b5.Visible=true;
            b6.Visible=true;
            b7.Visible=true;
            b8.Visible=true;
            b9.Visible=true;
            b10.Visible=true;
            b11.Visible=true;
            b12.Visible=true;
            b13.Visible=true;
            b14.Visible=true;
            b15.Visible=true;
            b16.Visible=true;
        }
    }
}
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示