VBA 扫雷:检查边界单元格的地雷数量?
VBA Minesweeper: Check border cells for mine count?
我在 VBA 中没有找到很多关于此的答案,但我正在制作一个扫雷 5x5 板,并试图让地雷周围的单元格显示有多少地雷正在接触它。我能够让中间的 9 工作,即带有 "X":
的空格
-----
-XXX-
-XXX-
-XXX-
-----
但是我在让边界单元格也计算在内时遇到了严重的问题。这是我的代码:
For i = 2 To 4
For j = 2 To 4
If BombArray(i, j) <> "X" Then
BombArray(i, j) = 0
If BombArray(i + 1, j - 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i + 1, j) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i + 1, j + 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i, j - 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i, j + 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i - 1, j - 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i - 1, j) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i - 1, j + 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
End If
Next j
Next i
我将索引设置为 2 到 4,否则 Excel 会抛出错误“9”:下标超出范围。
如有任何帮助,我们将不胜感激。
在测试单元格之前先测试一下你是否在边界上:
For i = 1 To 5
For j = 1 To 5
If BombArray(i, j) <> "X" Then
BombArray(i, j) = 0
If j <> 1 And i <> 5 Then
If BombArray(i + 1, j - 1) = "X" Then BombArray(i, j) = BombArray(i, j) + 1
End If
If i <> 5 Then
If BombArray(i + 1, j) = "X" Then BombArray(i, j) = BombArray(i, j) + 1
End If
'Etc.
'...
End If
Next j
Next i
您不需要为当前单元格周围的每个单元格设置数组或偏移量。
只需为扫雷器创建一个大范围和一个小范围 (3 x 3) 来计算它周围的炸弹。在循环中完成其余部分。
看下面的代码,注释里都有说明。随意更改中心单元格地址和扫雷范围以获得更大的扫雷游戏。
Sub myminesweeper()
Dim rng As Range, r2 As Range, c1 As Range, c2 As Range, center_cell As Range, irng As Range
Dim cnt As Integer
Set center_cell = Range("E7") 'set center cell of minesweeper range
Set rng = Range(center_cell.Offset(-3, -3), center_cell.Offset(3, 3)) 'set minesweeper range
'(simply change the offset numbers)
'currently it is 7 x 7
For Each c1 In rng 'loop through cells in minesweeper range
cnt = 0 'reset the counter
If c1.Value = "X" Then GoTo Skip 'skip calculation if there is mine
Set r2 = Range(c1.Offset(-1, -1), c1.Offset(1, 1)) 'set 3 x 3 range to check mines
For Each c2 In r2 'loop through cells in 3 x 3 range
Set irng = Application.Intersect(c2, rng) 'check if the cell is within minesweeper range
If Not irng Is Nothing Then 'if the cell is in range
If c2.Value = "X" Then cnt = cnt + 1 'check if there is a mine. if so, add 1 to cnt.
End If
Next c2
c1.Value = cnt 'set the cell's value to total count of mines around it (cnt)
Skip:
Next
End Sub
我在 VBA 中没有找到很多关于此的答案,但我正在制作一个扫雷 5x5 板,并试图让地雷周围的单元格显示有多少地雷正在接触它。我能够让中间的 9 工作,即带有 "X":
的空格-----
-XXX-
-XXX-
-XXX-
-----
但是我在让边界单元格也计算在内时遇到了严重的问题。这是我的代码:
For i = 2 To 4
For j = 2 To 4
If BombArray(i, j) <> "X" Then
BombArray(i, j) = 0
If BombArray(i + 1, j - 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i + 1, j) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i + 1, j + 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i, j - 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i, j + 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i - 1, j - 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i - 1, j) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
If BombArray(i - 1, j + 1) = "X" Then
BombArray(i, j) = BombArray(i, j) + 1
End If
End If
Next j
Next i
我将索引设置为 2 到 4,否则 Excel 会抛出错误“9”:下标超出范围。
如有任何帮助,我们将不胜感激。
在测试单元格之前先测试一下你是否在边界上:
For i = 1 To 5
For j = 1 To 5
If BombArray(i, j) <> "X" Then
BombArray(i, j) = 0
If j <> 1 And i <> 5 Then
If BombArray(i + 1, j - 1) = "X" Then BombArray(i, j) = BombArray(i, j) + 1
End If
If i <> 5 Then
If BombArray(i + 1, j) = "X" Then BombArray(i, j) = BombArray(i, j) + 1
End If
'Etc.
'...
End If
Next j
Next i
您不需要为当前单元格周围的每个单元格设置数组或偏移量。 只需为扫雷器创建一个大范围和一个小范围 (3 x 3) 来计算它周围的炸弹。在循环中完成其余部分。
看下面的代码,注释里都有说明。随意更改中心单元格地址和扫雷范围以获得更大的扫雷游戏。
Sub myminesweeper()
Dim rng As Range, r2 As Range, c1 As Range, c2 As Range, center_cell As Range, irng As Range
Dim cnt As Integer
Set center_cell = Range("E7") 'set center cell of minesweeper range
Set rng = Range(center_cell.Offset(-3, -3), center_cell.Offset(3, 3)) 'set minesweeper range
'(simply change the offset numbers)
'currently it is 7 x 7
For Each c1 In rng 'loop through cells in minesweeper range
cnt = 0 'reset the counter
If c1.Value = "X" Then GoTo Skip 'skip calculation if there is mine
Set r2 = Range(c1.Offset(-1, -1), c1.Offset(1, 1)) 'set 3 x 3 range to check mines
For Each c2 In r2 'loop through cells in 3 x 3 range
Set irng = Application.Intersect(c2, rng) 'check if the cell is within minesweeper range
If Not irng Is Nothing Then 'if the cell is in range
If c2.Value = "X" Then cnt = cnt + 1 'check if there is a mine. if so, add 1 to cnt.
End If
Next c2
c1.Value = cnt 'set the cell's value to total count of mines around it (cnt)
Skip:
Next
End Sub