【问题标题】:Coding Minesweeper in Excel VBA: Mines are overlapping in same cell在 Excel VBA 中编码扫雷:地雷在同一个单元格中重叠
【发布时间】:2018-09-25 15:31:54
【问题描述】:

所以我正在为一个班级做扫雷游戏,一切都或多或少地正常,但是我遇到了一个问题。有时地雷被放置在同一个牢房中。即:如果我有 10 个炸弹,有时会显示 9 个炸弹。

到目前为止,这是我的代码:

Sub Minesweeper()
Dim Mines As Integer
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim m As Integer
Dim OverLap As Integer

'This is for Centering Text
For i = 8 To 15
    For j = 6 To 13
        Cells(i, j).HorizontalAlignment = xlCenter
    Next j
Next i

'This is for setting Table/Board
For i = 8 To 15
    For j = 6 To 13
        'By default all cells will = 1 until bomb is placed
        Cells(i, j).Value = 1
    Next j
Next i

'This generates certain Number of Mines
For Mines = 1 To 10
    Cells(((Int((15 - 8 + 1) * Rnd + 1)) + 7), ((Int((13 - 6 + 1) * Rnd + 1))) + 5).Value = 0
Next Mines

'This is for converting Mines to o and color change
For i = 8 To 15
    For j = 6 To 13
    If Cells(i, j).Value = 0 Then
        Cells(i, j).Value = "o"
        Cells(i, j).Font.Color = RGB(250, 0, 0)
    ElseIf Cells(i, j).Value >= 1 Then
        Cells(i, j).Font.Color = RGB(0, 0, 0)
    End If
    Next j
Next i

End Sub

【问题讨论】:

    标签: excel duplicates minesweeper


    【解决方案1】:

    在 Mines = 1 到 10 期间对值进行“如果”检查。首先声明您的单元格值,然后检查该单元格是否为 1。如果是,则放置 0。如果不是,则生成另一个随机数单元格并重新检查。

    For Mines = 1 To 10
    1
        x = Int((15 - 8 + 1) * Rnd + 1) + 7
        y = Int((13 - 6 + 1) * Rnd + 1) + 5
        If Cells(x, y).Value = 1 Then
            Cells(x, y).Value = 0
        Else:
            GoTo 1
        End If
    Next Mines
    

    【讨论】:

      猜你喜欢
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      相关资源
      最近更新 更多