0

私は掃海艇ゲームを開発していて、地雷を割り当てましたが、それらの周りの数字を生成する必要があります。私はすでにコードを開発しましたが、機能していないようです。私が 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

私のエラーは、数値を生成する場所のどこかにあると思います。よろしく、ジョアオ。

4

2 に答える 2

2

あなたはあなたのループでauxlinhaandを使用しています:auxcolumn

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

しかし、ループ内の項目をチェックするためにlineandを使用しています。column

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

また、それを修正しても、アルゴリズムは依然として惨めに失敗します。読んでいるのと同じ配列に書き込んでおり、読んだ位置の前に書き込んでいます。Aは最初の配列では地雷を意味しますが、地雷の周囲のマスを増やし始めると、地雷を意味するのか、そのマスの近くに地雷があることを意味するの1かわからなくなります。1

また、1 から 10 までループしているため、エッジに沿った地雷は考慮されません。すべての正方形をループし、周囲の正方形を増やすときに境界にいるかどうかを確認する必要があります。

于 2012-05-07T08:29:50.033 に答える
0

最初のコメント:プログラムは英語で!

コードを読み取ることができません。識別子が重要でない場合は、何か間違ったことをしています。コードを理解するのに役立つはずです。英語はプログラミング言語です。これを受け入れてください。

次に、1 ~ 8 の代わりに 2 ~ 9 を使用するのは、少し直感的ではなく、エラーが発生しやすいと思いませんか?

考えられるすべての値を数値でエンコードすることに固執する場合は、mines を -1 にすると、数値がそれ自体を表します。

第 3に、デバッグ時: 数値の生成が機能していないと思われます。これは簡単にテストできます。いくつかの数値をハードコードして、表示が機能するかどうかを確認するだけです。または、逆の方法で数値を計算し、デバッグ ウィンドウに出力します。

第四に、スタイルについて。の前ではなく、最初の使用時に変数を宣言することで、作業が楽になります。先頭に変数宣言のブロックがあっても、コードが明確になるわけではありません。また、 と を有効にする必要があると思いOption ExplicitますOption Strict。また、VB6 スタイルの残骸も削除します。たとえばCall、不要でありStrVal.NET メソッドを優先して削除する必要があります。

于 2012-05-07T08:19:44.507 に答える