2012-05-07 35 views
0

所以我正在开发一个扫雷游戏,我已经分配了地雷,但是我必须在它们周围生成数字。我已经开发了代码,但它似乎没有工作。 指出我正在使用DataGridView很重要。生成数字VB.NET

这里是我的代码看起来像现在:

声明我的变量:

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 

Atributing正确的图像,它是在DataGridView的tablw矩阵数:

Private Sub tab1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles tab1.CellContentClick 
    My.Computer.Audio.Play("butao.wav") 
    Dim r, c As Integer 
    r = tab1.CurrentCell.RowIndex + 1 
    c = tab1.CurrentCell.ColumnIndex + 1 
    Label5.Text = "linha=" + Str(r) 
    Label6.Text = "coluna=" + Str(c) 
    'MessageBox.Show(minas(r, c)) 


    If (jogo(r, c) = 1) Then 
     MessageBox.Show("Ja jogou") 
    Else 
    'When There is a bomb 
     If mat(r, c) = 1 Then 
      Me.tab1.Rows(r - 1).Cells(c - 1).Value = Image.FromFile("mina1.png") 
      jogo(r, c) = 1 
      player1 = player1 + 3 
      Label1.Text = Val(player1) 
     End If 
     'When There is a number 
     If mat(r, c) > 1 Then 
      Call preenche_num(r - 1, c - 1) 
     'When there is nothing 
     End If 
     If mat(r, c) = 0 Then 
      Me.tab1.Rows(r - 1).Cells(c - 1).Value = Image.FromFile("vazio.png") 
     End If 

    End If 
End Sub 

Diferent号的可能性:

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 

我相信我的错误是什么地方,我生成的数字。 最好的问候,joao。

+0

你的问题到底是什么?什么不行?你写了什么测试?他们说什么?你是否遇到异常?意外的结果?回答这个问题非常困难,没有关于您的问题的更多具体细节。 –

+0

一切工作正常,我能够生成的标志,但他们的数字不会出现。编译器给我没有错误。 – joao

+0

为什么你不试着解决他们说的话?顺便说一句,你说VB.NET作为标记,但你只是做过程式编程,而你在地方使用旧的或奇怪的语法。 – MarioDS

回答

0

首先备注:程序英文!

我们无法读取您的代码。如果标识符不重要,那么你做错了一些事情:他们应该对帮助理解代码很重要。英语是编程语言 - 拥抱这一点。

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

如果你坚持用数字编码所有可能的值,使地雷为-1,数字代表它们自己。

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

第四,风格。通过在第一次使用时声明变量,使您的生活更轻松,不在之前。在开始处有变量声明块不会使代码更清晰。我也怀疑你应该启用Option ExplicitOption Strict。我也删除了VB6风格的残余:例如Call是不必要的,并且应该删除StrVal以支持.NET方法。

+0

为什么downvote? –

2

您正在使用您的循环auxlinhaauxcolumn

For auxlinha = 1 To 10 
    For auxcolumn = 1 To 10 

但你正在使用linecolumn检查循环内的项目:

If mat(line, column) = 1 Then 'tem uma mina 

此外,当你有固定那么,你的算法仍然会失败。你正在写与你正在阅读的同一个数组,并且在你阅读的位置之前写作。 A 1意味着最初阵列中的矿山,但是一旦你开始增加矿体周围的方块,就不会再知道1是否意味着矿山,或者是否意味着矿体靠近该矿体。

此外,您正在循环从1到10,因此沿边缘不会考虑地雷。你必须循环所有的方格,并且当你增加周围的方格时检查你是否在边界。