|
看这个代码好懂些,不过我的代码很多冗余,水平有限!请指教!
Option Compare Database
Option Explicit
Dim ifOpen As Boolean
Dim wm As WinMine
Dim Cels(1 To 16, 1 To 30) As Boolean
Public Sub MineClassDemo()
Dim X As Long
Dim Y As Long
Dim a As Long
Dim b As Long
Dim gg As OpenCellsEnum
Dim ifok As Boolean
Dim t2 As Long
Dim times As Long
times = 0
'如果失败,则回这里重试
start:
'重试次数
times = times + 1
Debug.Print times
ifOpen = False
a = 1
b = 1
ifok = False
For X = 1 To 16
For Y = 1 To 30
Cels(X, Y) = False
Next Y
Next X
'第一次先随机打开30个格,因为只打开一个格很难完成
Do While Not ifok
Set wm = CreateWinMine()
'ÖØÐÂÓÎÏ·
wm.newgame
X = Fix(Rnd * 16) + 1
Y = Fix(Rnd * 30) + 1
wm.OpenCellEx X, Y
For t2 = 1 To 30 '打开30个格
If wm.ReadCell(X, Y) <> ciLose Then '
X = Fix(Rnd * 16) + 1
Y = Fix(Rnd * 30) + 1
wm.OpenCellEx X, Y
End If
If wm.ReadCell(X, Y) = ciLose Then ifok = False: t2 = 130
Next t2
If wm.ReadCell(X, Y) <> ciLose Then ifok = True: Cels(X, Y) = True '30个格成功打开后ifok=true,跳出while
Loop '如果30个格触雷,则再尝试
wm.DumpCellsDisplay
'ÉèÖñê¼Ç
'wm.SetTag 5, 5
'̽Ë÷À׸ñ
'wm.OpenCell 5, 5
'wm.OpenCells 5, 5
ifok = False'用于跳出循环
Do While Not ifok
ifOpen = False
For X = 1 To 16
For Y = 1 To 30
If Cels(X, Y) = False Then 'Èç¹û´Ë¸ñδ´ò¿ª
If wm.ReadCell(X, Y) = ciOneMine Then a = X: b = Y '这句其实暂时无有
If wm.ReadCell(X, Y) <> ciunknown Then '如果当前格未打开
If TrySet(X, Y) = True Then SetXy X, Y '如果九宫内的雷数与未打开数相同,则把所有未打开设为有雷,这是第一个计算
End If
If wm.ReadCell(X, Y) <> ciunknown Then
If CanOpen(X, Y) Then OpenAl X, Y '如果九宫内雷数与已设置雷数相同,则打开其余的格
End If
If wm.ReadCell(X, Y) = ciWin Then
Debug.Print wm.GetWinCode '»ñȡʤÀû´úÂë
ifok = True '
Exit Sub
End If
End If
If X = 16 And Y = 30 And ifOpen = False Then '如果一个循环下来都没有设雷或打开格,则失败
'失败的话在这里中断可手动打开某格
' wm.OpenCellEx 4, 8
'wm.DumpCellsDisplay
GoTo start '失败重试
End If
Next Y
Next X
Loop
'»ñÈ¡³¢ÊÔ´ÎÊý
'wm.GetTimes
End Sub
Private Sub SetXy(X As Long, Y As Long)
If X - 1 > 0 And Y - 1 > 0 Then If wm.ReadCell(X - 1, Y - 1) = ciunknown Then wm.SetTag X - 1, Y - 1
If Y - 1 > 0 Then If wm.ReadCell(X, Y - 1) = ciunknown Then wm.SetTag X, Y - 1
If Y - 1 > 0 And X + 1 < 17 Then If wm.ReadCell(X + 1, Y - 1) = ciunknown Then wm.SetTag X + 1, Y - 1
If X - 1 > 0 Then If wm.ReadCell(X - 1, Y) = ciunknown Then wm.SetTag X - 1, Y
If X + 1 < 17 Then If wm.ReadCell(X + 1, Y) = ciunknown Then wm.SetTag X + 1, Y
If X - 1 > 0 And Y + 1 < 31 Then If wm.ReadCell(X - 1, Y + 1) = ciunknown Then |
|