-
Notifications
You must be signed in to change notification settings - Fork 1
/
CTable.cls
454 lines (404 loc) · 12 KB
/
CTable.cls
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CTable"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
' 保存棋盘数据
Private mvarTables(7, 7) As Byte
'保持属性值的局部变量
Private mvarPanelHeight As Single '局部复制
'保持属性值的局部变量
Private mvarPanelWidth As Single '局部复制
'保持属性值的局部变量
Private mvarChessboardX As Single '局部复制
Private mvarChessboardY As Single '局部复制
'要引发该事件,请遵循下列语法使用 RaiseEvent:
'RaiseEvent DownChessMan[(arg1, arg2, ... , argn)]
Public Event DownChessMan(Col As Integer, Row As Integer, Man As Byte)
Public Function Umpire(ByVal Man As Byte) As Byte
Dim MyNumber As Integer
Dim YourNumber As Integer
On Error Resume Next
' 计算自己的棋子数
MyNumber = GetTotal(Man)
' 计算对方的棋子数
YourNumber = GetTotal(ToPartner(Man))
If MyNumber > YourNumber Then ' 如果大于,胜利
Umpire = GAME_WIN
ElseIf MyNumber < YourNumber Then ' 小于,失败
Umpire = GAME_LOSE
Else ' 其它情况,平局
Umpire = GAME_DRAW
End If
End Function
Public Sub Clear()
Dim i As Integer
Dim j As Integer
For i = 0 To 7
For j = 0 To 7
mvarTables(j, i) = T_NONE
Next j
Next i
End Sub
Public Property Let ChessboardX(ByVal vData As Single)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.ChessboardX = 5
mvarChessboardX = vData
End Property
Public Property Get ChessboardX() As Single
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.ChessboardX
ChessboardX = mvarChessboardX
End Property
Public Property Let ChessboardY(ByVal vData As Single)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.ChessboardY = 5
mvarChessboardY = vData
End Property
Public Property Get ChessboardY() As Single
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.ChessboardY
ChessboardY = mvarChessboardY
End Property
Public Property Let PanelWidth(ByVal vData As Single)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.PanelWidth = 5
mvarPanelWidth = vData
End Property
Public Property Get PanelWidth() As Single
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.PanelWidth
PanelWidth = mvarPanelWidth
End Property
Public Property Let PanelHeight(ByVal vData As Single)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.PanelHeight = 5
mvarPanelHeight = vData
End Property
Public Property Get PanelHeight() As Single
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.PanelHeight
PanelHeight = mvarPanelHeight
End Property
Public Function GetCol(ByVal X As Single) As Integer
GetCol = (X - ChessboardX + PanelWidth) \ PanelWidth - 1
End Function
Public Function GetRow(ByVal Y As Single) As Integer
GetRow = (Y - ChessboardY + PanelHeight) \ PanelHeight - 1
End Function
Public Function GetX(ByVal Col As Integer) As Single
GetX = mvarChessboardX + PanelWidth * Col
End Function
Public Function GetY(ByVal Row As Integer) As Single
GetY = mvarChessboardY + PanelHeight * Row
End Function
Public Function GetMan(ByVal Col As Integer, ByVal Row As Integer) As Byte
GetMan = mvarTables(Col, Row)
End Function
Public Function SetMan(ByVal Col As Integer, ByVal Row As Integer, ByVal Man As Byte) As Boolean
mvarTables(Col, Row) = Man
SetMan = True
End Function
Public Function GetTotal(ByVal Man As Byte) As Integer
Dim Col As Integer
Dim Row As Integer
Dim Num As Integer
'Num = 0
On Error Resume Next
For Row = 0 To 7
For Col = 0 To 7
If mvarTables(Col, Row) = Man Then Num = Num + 1
Next Col
Next Row
GetTotal = Num
End Function
Public Function CanDown(ByVal Man As Byte) As Boolean
Dim Col As Integer
Dim Row As Integer
On Error Resume Next
For Row = 0 To 7
For Col = 0 To 7
If IsDown(Col, Row, Man) Then
CanDown = True
Exit Function
End If
Next Col
Next Row
CanDown = False
End Function
Public Function IsDown(ByVal Col As Integer, ByVal Row As Integer, ByVal Man As Byte) As Boolean
Dim c As Integer
Dim r As Integer
Dim Value As Byte
On Error GoTo ErrHandler
If mvarTables(Col, Row) <> T_NONE Then
IsDown = False
Exit Function
End If
' To Left-Bottom
c = Col - 1: r = Row + 1
Do While (c >= 0 And r <= 7)
Value = mvarTables(c, r)
If Value = Man And Col - c > 1 And r - Row > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit Do
End If
c = c - 1: r = r + 1
Loop
' To Left
For c = Col - 1 To 0 Step -1
Value = mvarTables(c, Row)
If Value = Man And Col - c > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit For
End If
Next c
' To Left-Top
c = Col - 1: r = Row - 1
Do While (c >= 0 And r >= 0)
Value = mvarTables(c, r)
If Value = Man And Col - c > 1 And Row - r > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit Do
End If
c = c - 1: r = r - 1
Loop
' To Top
For r = Row - 1 To 0 Step -1
Value = mvarTables(Col, r)
If Value = Man And Row - r > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit For
End If
Next r
' To Right-Top
c = Col + 1: r = Row - 1
Do While (c <= 7 And r >= 0)
Value = mvarTables(c, r)
If Value = Man And c - Col > 1 And Row - r > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit Do
End If
c = c + 1: r = r - 1
Loop
' To Right
For c = Col + 1 To 7
Value = mvarTables(c, Row)
If Value = Man And c - Col > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit For
End If
Next c
' To Right-Bottom
c = Col + 1: r = Row + 1
Do While (c <= 7 And r <= 7)
Value = mvarTables(c, r)
If Value = Man And c - Col > 1 And r - Row > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit Do
End If
c = c + 1: r = r + 1
Loop
' To Bottom
For r = Row + 1 To 7
Value = mvarTables(Col, r)
If Value = Man And r - Row > 1 Then
IsDown = True
Exit Function
Else
If Value = T_NONE Or Value = Man Then Exit For
End If
Next r
ErrHandler:
IsDown = False
End Function
Private Sub SetDrawMan(ByVal StartCol As Integer, ByVal StartRow As Integer, ByVal EndCol As Integer, ByVal EndRow As Integer, ByVal Man As Byte)
Dim c As Integer
Dim r As Integer
Dim ColSgn As Integer
Dim RowSgn As Integer
On Error Resume Next
ColSgn = Sgn(EndCol - StartCol)
RowSgn = Sgn(EndRow - StartRow)
c = StartCol: r = StartRow
Do While (((c <= EndCol And ColSgn > 0) Or (c >= EndCol And ColSgn < 0) Or ColSgn = 0) And ((r <= EndRow And RowSgn > 0) Or (r >= EndRow And RowSgn < 0) Or RowSgn = 0))
mvarTables(c, r) = Man
RaiseEvent DownChessMan(c, r, Man)
c = c + ColSgn: r = r + RowSgn
Loop
End Sub
Public Function DownMan(ByVal Col As Integer, ByVal Row As Integer, ByVal Man As Byte) As Integer
Dim c As Integer
Dim r As Integer
Dim Value As Byte
Dim Number As Integer
Dim Temp As Integer
On Error Resume Next
If Man = T_NONE Then
DownMan = 0
Exit Function
End If
'Number = 0
'temp = 0
mvarTables(Col, Row) = Man
RaiseEvent DownChessMan(Col, Row, Man)
' To Left-Bottom
c = Col - 1: r = Row + 1
Do While (c >= 0 And r <= 7)
Value = mvarTables(c, r)
If Value = Man And Col - c > 1 And r - Row > 1 Then
Call SetDrawMan(Col - 1, Row + 1, c, r, Man)
Number = Temp
Exit Do
Else
If Value = T_NONE Or Value = Man Then Exit Do
Temp = Temp + 1
End If
c = c - 1: r = r + 1
Loop
' To Left
Temp = 0
For c = Col - 1 To 0 Step -1
Value = mvarTables(c, Row)
If Value = Man And Col - c > 1 Then
Call SetDrawMan(Col - 1, Row, c, Row, Man)
Number = Number + Temp
Exit For
Else
If Value = T_NONE Or Value = Man Then Exit For
Temp = Temp + 1
End If
Next c
' To Left-Top
Temp = 0
c = Col - 1: r = Row - 1
Do While (c >= 0 And r >= 0)
Value = mvarTables(c, r)
If Value = Man And Col - c > 1 And Row - r > 1 Then
Call SetDrawMan(Col - 1, Row - 1, c, r, Man)
Number = Number + Temp
Exit Do
Else
If Value = T_NONE Or Value = Man Then Exit Do
Temp = Temp + 1
End If
c = c - 1: r = r - 1
Loop
' To Top
Temp = 0
For r = Row - 1 To 0 Step -1
Value = mvarTables(Col, r)
If Value = Man And Row - r > 1 Then
Call SetDrawMan(Col, Row - 1, Col, r, Man)
Number = Number + Temp
Exit For
Else
If Value = T_NONE Or Value = Man Then Exit For
Temp = Temp + 1
End If
Next r
' To Right-Top
Temp = 0
c = Col + 1: r = Row - 1
Do While (c <= 7 And r >= 0)
Value = mvarTables(c, r)
If Value = Man And c - Col > 1 And Row - r > 1 Then
Call SetDrawMan(Col + 1, Row - 1, c, r, Man)
Number = Number + Temp
Exit Do
Else
If Value = T_NONE Or Value = Man Then Exit Do
Temp = Temp + 1
End If
c = c + 1: r = r - 1
Loop
' To Right
Temp = 0
For c = Col + 1 To 7
Value = mvarTables(c, Row)
If Value = Man And c - Col > 1 Then
Call SetDrawMan(Col + 1, Row, c, Row, Man)
Number = Number + Temp
Exit For
Else
If Value = T_NONE Or Value = Man Then Exit For
Temp = Temp + 1
End If
Next c
' To Right-Bottom
Temp = 0
c = Col + 1: r = Row + 1
Do While (c <= 7 And r <= 7)
Value = mvarTables(c, r)
If Value = Man And c - Col > 1 And r - Row > 1 Then
Call SetDrawMan(Col + 1, Row + 1, c, r, Man)
Number = Number + Temp
Exit Do
Else
If Value = T_NONE Or Value = Man Then Exit Do
Temp = Temp + 1
End If
c = c + 1: r = r + 1
Loop
' To Bottom
Temp = 0
For r = Row + 1 To 7
Value = mvarTables(Col, r)
If Value = Man And r - Row > 1 Then
Call SetDrawMan(Col, Row + 1, Col, r, Man)
Number = Number + Temp
Exit For
Else
If Value = T_NONE Or Value = Man Then Exit For
Temp = Temp + 1
End If
Next r
DownMan = Number
End Function
Private Sub Class_Initialize()
Dim i As Integer
Dim j As Integer
On Error Resume Next
For i = 0 To 7
For j = 0 To 7
mvarTables(j, i) = T_NONE
Next j
Next i
mvarTables(3, 3) = T_WHITE
mvarTables(3, 4) = T_BLACK
mvarTables(4, 3) = T_BLACK
mvarTables(4, 4) = T_WHITE
'mvarTables(3, 3) = T_BLACK
'mvarTables(3, 4) = T_BLACK
'mvarTables(4, 3) = T_BLACK
'mvarTables(4, 4) = T_BLACK
mvarChessboardX = TablePos.X + 390
mvarChessboardY = TablePos.Y + 355
mvarPanelHeight = 540
mvarPanelWidth = 540
End Sub