所以我正在開發一個掃雷遊戲,我已經分配了地雷,但是我必須在它們周圍生成數字。我已經開發了代碼,但它似乎沒有工作。 指出我正在使用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。
你的問題到底是什麼?什麼不行?你寫了什麼測試?他們說什麼?你是否遇到異常?意外的結果?回答這個問題非常困難,沒有關於您的問題的更多具體細節。 –
一切工作正常,我能夠生成的標誌,但他們的數字不會出現。編譯器給我沒有錯誤。 – joao
爲什麼你不試着解決他們說的話?順便說一句,你說VB.NET作爲標記,但你只是做過程式編程,而你在地方使用舊的或奇怪的語法。 – MarioDS