-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogicGame.frm
435 lines (407 loc) · 12.7 KB
/
LogicGame.frm
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
VERSION 5.00
Begin VB.Form Form1
AutoRedraw = -1 'True
BorderStyle = 1 'Fixed Single
Caption = "LogicGame"
ClientHeight = 3090
ClientLeft = 150
ClientTop = 840
ClientWidth = 4680
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.Timer Timer2
Left = 2880
Top = 1320
End
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 1
Left = 1800
Top = 1320
End
Begin VB.PictureBox Picture1
Appearance = 0 'Flat
AutoRedraw = -1 'True
BackColor = &H80000005&
BorderStyle = 0 'None
ForeColor = &H80000008&
Height = 1500
Left = 1200
ScaleHeight = 100
ScaleMode = 3 'Pixel
ScaleWidth = 100
TabIndex = 0
Top = 840
Visible = 0 'False
Width = 1500
End
Begin VB.Menu BtnClear
Caption = "清除(&C)"
End
Begin VB.Menu BtnImport
Caption = "导入(&I)"
End
Begin VB.Menu BtnExport
Caption = "导出(&E)"
End
Begin VB.Menu BtnExportBmp
Caption = "导出图片(&X)"
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Board(99, 99) As Byte, CurMouseY#, CurMouseX#
Dim List&(10000)
Private Sub BtnClear_Click()
Dim i&, j&
For i = 0 To 99
For j = 0 To 99
Board(i, j) = 0
Next
Next
Timer1 = True
End Sub
Private Sub BtnExport_Click()
Dim Str As String * 2500
Dim i&, j&
For i = 0 To 99
For j = 0 To 24
Mid(Str, i * 25 + j + 1, 1) = Chr(Board(i, j * 4) + Board(i, j * 4 + 1) * 3 + Board(i, j * 4 + 2) * 9 + Board(i, j * 4 + 3) * 27 + 35)
Next
Next
Clipboard.Clear
Clipboard.SetText Str
MsgBox "已导出到剪贴板"
End Sub
Private Sub BtnExportBmp_Click()
Clipboard.Clear
Clipboard.SetData Picture1.Image
End Sub
Private Sub BtnImport_Click()
Dim Str$, i&, j&, e&
If Clipboard.GetFormat(1) Then ' String
Str = Clipboard.GetText
If Len(Str) <> 2500 Then
MsgBox "存档不合法"
Exit Sub
End If
For i = 0 To 99
For j = 0 To 24
e = Asc(Mid(Str, i * 25 + j + 1, 1)) - 35
If e < 0 Then e = 0
If e > 80 Then e = 80
Board(i, j * 4) = e Mod 3
e = e \ 3
Board(i, j * 4 + 1) = e Mod 3
e = e \ 3
Board(i, j * 4 + 2) = e Mod 3
e = e \ 3
Board(i, j * 4 + 3) = e Mod 3
Next
Next
ElseIf Clipboard.GetFormat(2) Then 'Bitmap
Picture1.PaintPicture Clipboard.GetData, 0, 0
For i = 0 To 99
For j = 0 To 99
e = Picture1.Point(j, i)
Board(i, j) = IIf(e And &H8000&, 0, IIf(e And &H80&, 2, 1))
Next
Next
Else
MsgBox "格式无效"
End If
Timer1 = True
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeySpace
Step
Case vbKeyA, vbKeyA + 32
Timer2.Interval = 0
Case vbKeyS, vbKeyS + 32
Timer2.Interval = 1000
Case vbKeyD, vbKeyD + 32
Timer2.Interval = 500
Case vbKeyF, vbKeyF + 32
Timer2.Interval = 200
Case vbKeyG, vbKeyG + 32
Timer2.Interval = 100
Case vbKeyH, vbKeyH + 32
Timer2.Interval = 1
End Select
End Sub
Private Sub Form_Load()
Dim CurrentWidth#
CurrentWidth = 15 * 800
Width = Width - ScaleWidth + CurrentWidth
Height = Height - ScaleHeight + CurrentWidth
Scale (0, 0)-(100, 100)
Redraw
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X < 0 Or Y < 0 Or X >= 100 Or Y >= 100 Then Exit Sub
If Button = 1 And Board(Int(Y), Int(X)) <> 0 Then
Fill Int(X), Int(Y), 3 Xor Board(Int(Y), Int(X))
Else
Form_MouseMove Button, Shift, X, Y
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i#, j&, k&
If X < 0 Or Y < 0 Or X >= 100 Or Y >= 100 Then Exit Sub
If Button = 2 Then
For i = 0 To 1 Step 0.01 * (1 + Shift * 2)
For j = Int(Y) - Shift To Int(Y) + Shift
For k = Int(X) - Shift To Int(X) + Shift
If j >= 0 And j < 100 And k >= 0 And k < 100 Then
Board(j, k) = 0
End If
Next
Next
Next
Timer1 = True
ElseIf Button = 1 Then
For i = 0 To 1 Step 0.01
Fill Int(X * i + CurMouseX * (1 - i)), Int(Y * i + CurMouseY * (1 - i)), 1
Next
End If
CurMouseY = Y
CurMouseX = X
End Sub
Function IsBridge(ByVal X&, ByVal Y&) As Boolean
If Board(X, Y) Then Exit Function
If X = 0 Or X = 99 Then Exit Function
If Y = 0 Or Y = 99 Then Exit Function
IsBridge = Board(X - 1, Y) * Board(X + 1, Y) * Board(X, Y - 1) * Board(X, Y + 1)
End Function
Sub Fill(ByVal X&, ByVal Y&, ByVal Color&)
Dim ListLen&, CurPos&
Board(Y, X) = Color
More:
If Y > 0 Then
If Board(Y - 1, X) = 3 - Color Then
Board(Y - 1, X) = Color
List(ListLen) = (Y - 1) * 65536 + X
ListLen = ListLen + 1
ElseIf IsBridge(Y - 1, X) Then
If Board(Y - 2, X) = 3 - Color Then
Board(Y - 2, X) = Color
List(ListLen) = (Y - 2) * 65536 + X
ListLen = ListLen + 1
End If
End If
End If
If Y < 99 Then
If Board(Y + 1, X) = 3 - Color Then
Board(Y + 1, X) = Color
List(ListLen) = (Y + 1) * 65536 + X
ListLen = ListLen + 1
ElseIf IsBridge(Y + 1, X) Then
If Board(Y + 2, X) = 3 - Color Then
Board(Y + 2, X) = Color
List(ListLen) = (Y + 2) * 65536 + X
ListLen = ListLen + 1
End If
End If
End If
If X > 0 Then
If Board(Y, X - 1) = 3 - Color Then
Board(Y, X - 1) = Color
List(ListLen) = (Y) * 65536 + X - 1
ListLen = ListLen + 1
ElseIf IsBridge(Y, X - 1) Then
If Board(Y, X - 2) = 3 - Color Then
Board(Y, X - 2) = Color
List(ListLen) = (Y) * 65536 + X - 2
ListLen = ListLen + 1
End If
End If
End If
If X < 99 Then
If Board(Y, X + 1) = 3 - Color Then
Board(Y, X + 1) = Color
List(ListLen) = (Y) * 65536 + X + 1
ListLen = ListLen + 1
ElseIf IsBridge(Y, X + 1) Then
If Board(Y, X + 2) = 3 - Color Then
Board(Y, X + 2) = Color
List(ListLen) = (Y) * 65536 + X + 2
ListLen = ListLen + 1
End If
End If
End If
If ListLen > CurPos Then
X = List(CurPos) And 65535
Y = List(CurPos) \ 65536
CurPos = CurPos + 1
GoTo More
End If
Timer1 = True
End Sub
Sub Redraw()
Debug.Print 1, Timer
Dim i&, j&, c&
For i = 0 To 99
For j = 0 To 99
Select Case Board(i, j)
Case 0
Select Case NeighborC(i, j)
Case 15
c = &HFFFF00
Case 14, 13, 11, 7
c = &HCC00&
Case Else
c = IIf(i + j And 1, &HFFCCFF, &HCCFFCC)
End Select
Case 1
c = vbBlack
Case 2
c = vbRed
End Select
Picture1.PSet (j, i), c
Next
Next
Debug.Print 2, Timer
Me.PaintPicture Picture1.Image, 0, 0, ScaleWidth, ScaleHeight
Debug.Print 3, Timer
End Sub
Private Sub Timer1_Timer()
Timer1 = False
Redraw
End Sub
Function NeighborC%(ByVal Y&, ByVal X&)
If Y > 0 Then If Board(Y - 1, X) Then NeighborC = NeighborC + 1
If X > 0 Then If Board(Y, X - 1) Then NeighborC = NeighborC + 2
If Y < 99 Then If Board(Y + 1, X) Then NeighborC = NeighborC + 4
If X < 99 Then If Board(Y, X + 1) Then NeighborC = NeighborC + 8
End Function
Sub Step()
Dim Y&, X&, Yl&, Xl&
Dim Val&(99, 99)
Dim ListLen&, CurPos&, Sum&
For Y = 0 To 99
For X = 0 To 99
If Board(Y, X) = 0 Then
Select Case NeighborC(Y, X)
Case 14
Val(Y + 1, X) = Val(Y + 1, X) + IIf(Board(Y, X - 1) = Board(Y, X + 1), -32768, 32768)
Case 13
Val(Y, X + 1) = Val(Y, X + 1) + IIf(Board(Y - 1, X) = Board(Y + 1, X), -32768, 32768)
Case 11
Val(Y - 1, X) = Val(Y - 1, X) + IIf(Board(Y, X - 1) = Board(Y, X + 1), -32768, 32768)
Case 7
Val(Y, X - 1) = Val(Y, X - 1) + IIf(Board(Y - 1, X) = Board(Y + 1, X), -32768, 32768)
End Select
Val(Y, X) = 1
End If
Next
Next
For Y = 0 To 99
For X = 0 To 99
If Val(Y, X) - 1 And 1 Then
ListLen = 0
CurPos = 0
List(0) = Y * 65536 + X
Yl = Y: Xl = X
Sum = Val(Yl, Xl) + (Board(Yl, Xl) And 2) - 1
Val(Yl, Xl) = 1
More: If Yl > 0 Then
If Board(Yl - 1, Xl) Then
If Val(Yl - 1, Xl) <> 1 Then
Sum = Sum + Val(Yl - 1, Xl) + (Board(Yl - 1, Xl) And 2) - 1
Val(Yl - 1, Xl) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl - 1) * 65536 + Xl
End If
ElseIf IsBridge(Yl - 1, Xl) Then
If Val(Yl - 2, Xl) <> 1 Then
Sum = Sum + Val(Yl - 2, Xl) + (Board(Yl - 2, Xl) And 2) - 1
Val(Yl - 2, Xl) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl - 2) * 65536 + Xl
End If
End If
End If
If Yl < 99 Then
If Board(Yl + 1, Xl) Then
If Val(Yl + 1, Xl) <> 1 Then
Sum = Sum + Val(Yl + 1, Xl) + (Board(Yl + 1, Xl) And 2) - 1
Val(Yl + 1, Xl) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl + 1) * 65536 + Xl
End If
ElseIf IsBridge(Yl + 1, Xl) Then
If Val(Yl + 2, Xl) <> 1 Then
Sum = Sum + Val(Yl + 2, Xl) + (Board(Yl + 2, Xl) And 2) - 1
Val(Yl + 2, Xl) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl + 2) * 65536 + Xl
End If
End If
End If
If Xl > 0 Then
If Board(Yl, Xl - 1) Then
If Val(Yl, Xl - 1) <> 1 Then
Sum = Sum + Val(Yl, Xl - 1) + (Board(Yl, Xl - 1) And 2) - 1
Val(Yl, Xl - 1) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl) * 65536 + Xl - 1
End If
ElseIf IsBridge(Yl, Xl - 1) Then
If Val(Yl, Xl - 2) <> 1 Then
Sum = Sum + Val(Yl, Xl - 2) + (Board(Yl, Xl - 2) And 2) - 1
Val(Yl, Xl - 2) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl) * 65536 + Xl - 2
End If
End If
End If
If Xl < 99 Then
If Board(Yl, Xl + 1) Then
If Val(Yl, Xl + 1) <> 1 Then
Sum = Sum + Val(Yl, Xl + 1) + (Board(Yl, Xl + 1) And 2) - 1
Val(Yl, Xl + 1) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl) * 65536 + Xl + 1
End If
ElseIf IsBridge(Yl, Xl + 1) Then
If Val(Yl, Xl + 2) <> 1 Then
Sum = Sum + Val(Yl, Xl + 2) + (Board(Yl, Xl + 2) And 2) - 1
Val(Yl, Xl + 2) = 1
ListLen = ListLen + 1
List(ListLen) = (Yl) * 65536 + Xl + 2
End If
End If
End If
If CurPos < ListLen Then
CurPos = CurPos + 1
Yl = List(CurPos) \ 65536
Xl = List(CurPos) And 65535
GoTo More
End If
Sum = IIf(Sum > 0, 5, 3)
For CurPos = 0 To ListLen
Val(List(CurPos) \ 65536, List(CurPos) And 65535) = Sum
Next
End If
Next
Next
For Y = 0 To 99
For X = 0 To 99
Board(Y, X) = Val(Y, X) \ 2
Next
Next
Timer1 = True
End Sub
Private Sub Timer2_Timer()
Timer2 = False
Step
Timer2 = True
End Sub