2012-05-07 80 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,因此沿邊緣不會考慮地雷。你必須循環所有的方格,並且當你增加周圍的方格時檢查你是否在邊界。