生成数字VB.NET

生成数字VB.NET,vb.net,matrix,minesweeper,Vb.net,Matrix,Minesweeper,所以我正在开发一个扫雷游戏,我已经分配了地雷,但我必须生成它们周围的数字。我已经开发了代码,但它似乎不起作用。 我使用的是DataGridView,这一点很重要 下面是我的代码现在的样子: 声明我的变量: Public line As Integer Public column As Integer Public numbandeiras As Integer Public mat(11, 11) As Integer Public Const MINASEXISTE = 34 Public j

所以我正在开发一个扫雷游戏,我已经分配了地雷,但我必须生成它们周围的数字。我已经开发了代码,但它似乎不起作用。 我使用的是DataGridView,这一点很重要

下面是我的代码现在的样子:

声明我的变量:

Public line As Integer
Public column As Integer
Public numbandeiras As Integer
Public mat(11, 11) As Integer
Public Const MINASEXISTE = 34
Public jogo(11, 11) As Integer
初始化地雷:

Public Sub initflags()

    'initialize the matrix
    For i = 0 To 11
        For j = 0 To 11
            mat(i, j) = 0
        Next
    Next


    'generate the mines
    Do Until numbandeiras = MINASEXISTE



        'Randomize()
        line = Int(Rnd() * 10) + 1
        column = Int(Rnd() * 10) + 1
        r = line
        c = column
        If mat(line, column) = 0 Then
            numbandeiras = numbandeiras + 1


            mat(line, column) = 1

        End If

    Loop

'call numbers    
Call avisinhos()

End Sub
 Public Sub avisinhos()

    'declaring my local variables
    Dim conta As Integer = 0
    Dim linestart As Integer = -1
    Dim lineend As Integer = 1
    Dim colstart As Integer = -1
    Dim colend As Integer = 1
    Dim auxlinha, auxcolumn As Integer


  'generating the numbers in the matrix
    For auxlinha = 1 To 10
        For auxcolumn = 1 To 10
            While conta < MINASEXISTE
                If mat(line, column) = 1 Then 'tem uma mina


                    mat(auxlinha - 1, auxcolumn - 1) = mat(auxlinha - 1, auxcolumn - 1) + 1
                    mat(auxlinha - 1, auxcolumn) = mat(auxlinha - 1, auxcolumn) + 1
                    mat(auxlinha - 1, auxcolumn + 1) = mat(auxlinha - 1, auxcolumn + 1) + 1
                    mat(auxlinha, auxcolumn - 1) = mat(auxlinha, auxcolumn - 1) + 1
                    mat(auxlinha, auxcolumn + 1) = mat(auxlinha, auxcolumn + 1) + 1
                    mat(auxlinha + 1, auxcolumn - 1) = mat(auxlinha + 1, auxcolumn - 1) + 1
                    mat(auxlinha + 1, auxcolumn) = mat(auxlinha + 1, auxcolumn) + 1
                    mat(auxlinha + 1, auxcolumn + 1) = mat(auxlinha + 1, auxcolumn + 1) + 1
                End If

                conta = conta + 1
            End While
        Next
    Next

End Sub
End Module
生成矿山周围的数字:

Public Sub initflags()

    'initialize the matrix
    For i = 0 To 11
        For j = 0 To 11
            mat(i, j) = 0
        Next
    Next


    'generate the mines
    Do Until numbandeiras = MINASEXISTE



        'Randomize()
        line = Int(Rnd() * 10) + 1
        column = Int(Rnd() * 10) + 1
        r = line
        c = column
        If mat(line, column) = 0 Then
            numbandeiras = numbandeiras + 1


            mat(line, column) = 1

        End If

    Loop

'call numbers    
Call avisinhos()

End Sub
 Public Sub avisinhos()

    'declaring my local variables
    Dim conta As Integer = 0
    Dim linestart As Integer = -1
    Dim lineend As Integer = 1
    Dim colstart As Integer = -1
    Dim colend As Integer = 1
    Dim auxlinha, auxcolumn As Integer


  'generating the numbers in the matrix
    For auxlinha = 1 To 10
        For auxcolumn = 1 To 10
            While conta < MINASEXISTE
                If mat(line, column) = 1 Then 'tem uma mina


                    mat(auxlinha - 1, auxcolumn - 1) = mat(auxlinha - 1, auxcolumn - 1) + 1
                    mat(auxlinha - 1, auxcolumn) = mat(auxlinha - 1, auxcolumn) + 1
                    mat(auxlinha - 1, auxcolumn + 1) = mat(auxlinha - 1, auxcolumn + 1) + 1
                    mat(auxlinha, auxcolumn - 1) = mat(auxlinha, auxcolumn - 1) + 1
                    mat(auxlinha, auxcolumn + 1) = mat(auxlinha, auxcolumn + 1) + 1
                    mat(auxlinha + 1, auxcolumn - 1) = mat(auxlinha + 1, auxcolumn - 1) + 1
                    mat(auxlinha + 1, auxcolumn) = mat(auxlinha + 1, auxcolumn) + 1
                    mat(auxlinha + 1, auxcolumn + 1) = mat(auxlinha + 1, auxcolumn + 1) + 1
                End If

                conta = conta + 1
            End While
        Next
    Next

End Sub
End Module
不同数量的可能性:

 Sub preenche_num(ByVal r As Integer, ByVal c As Integer)

    Select Case mat(r, c)
        Case 2
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("um.png")
        Case 3
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("dois.png")
        Case 4
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("tres.png")
        Case 5
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("quatro.png")
        Case 6
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("cinco.png")
        Case 7
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("seis.png")
        Case 8
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("sete.png")
        Case 9
            Me.tab1.Rows(r).Cells(c).Value = Image.FromFile("oito.png")
    End Select
End Sub
我相信我的错误就在我生成数字的地方。
向乔致意。

首先备注:英语节目

我们无法读取您的代码。如果标识符不重要,说明您做错了什么:它们在帮助理解代码方面应该很重要。英语是编程语言——接受这一点

其次,用2-9代表数字1-8有点不直观,而且容易出错,你不觉得吗

如果你坚持用数字来编码所有可能的值,那么就用-1来表示数字本身

第三个,关于调试:您怀疑生成的数字不起作用。嗯,这很容易测试:只需硬编码一些数字,然后看看显示器是否工作。或者反过来,计算数字并在调试窗口中输出


第四,关于风格。通过在首次使用时声明变量,而不是在首次使用之前声明变量,使您的生活更轻松。在开头有变量声明块并不能使代码更清晰。我还怀疑您应该启用
选项显式
选项严格
。我还将删除VB6风格的残余:例如,
调用
是不必要的,而
Str
Val
应该删除,以支持.NET方法。

您在循环中使用的是
auxlinha
auxcolumn

For auxlinha = 1 To 10
  For auxcolumn = 1 To 10
但是,您使用
检查循环中的项目:

    If mat(line, column) = 1 Then 'tem uma mina
而且,当你修复了这个问题,你的算法仍然会失败得很惨。你在阅读的同一个数组中写作,并且在阅读位置之前写作。
1
表示初始数组中的地雷,但一旦开始增加地雷周围的方格,您就不再知道
1
是否表示地雷,或者它是否表示在该方格附近有地雷


此外,你是从1循环到10,所以没有地雷沿边缘将被考虑。您必须循环所有方块,并在增加周围方块时检查是否处于边界。

您的问题到底是什么?什么不起作用?你写过测试吗?他们怎么说?你有例外吗?意外的结果?如果没有关于你的问题的更具体的细节,很难回答这个问题。一切都很好,我能够生成标志,但是它们周围的数字不会出现。编译器没有给我错误。你为什么不试着解决他们说的问题呢??顺便说一句,你说VB.NET是一个标记,但你只是做程序编程,而且在某些地方使用了旧的或奇怪的语法。我建议你试着找出代码中似乎没有按预期工作的部分。然后只发布那部分。我知道这可能很困难,因为您的代码是过程性的,但是如果我们不必自己费力地阅读所有代码,我们就可以更容易地提供帮助。