所以我正在开发一个扫雷游戏,我已经分配了地雷,但我必须生成它们周围的数字。我已经开发了代码,但它似乎没有工作。重要的是要提到我正在使用 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
将正确的图像归因于 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
不同的数字可能性:
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
我确实相信我的错误是在我生成数字的地方。最好的问候,乔奥。