Search code examples
vb.netmatrixminesweeper

Verify Gamefield VB.NET


So I'm developing a minesweeper game and im assigning the mines, but I've got to check where are the mines now, in order to generate the numbers. The problem is that when I'm verifying the columns and lines I need the program not to get out of the game field.

Here's how my code looks like now:

Public Sub avisinhos(ByVal line, ByVal column)
    If mat(line, column) = 0 Then
        mat(line, column) = -1
        numbandeiras = numbandeiras + 1
    End If
    For auxlinha = -1 To 1
        For auxcolumn = -1 To 1

        Next
    Next
End Sub

How do I create a IF function to verify that I don't get out of the game field? Best regards, joao.


Solution

  • pseudo code

    int linestart = -1;
    int lineend = 1;
    int colstart = -1;
    int colend = 1;
    
    Assuming a 10 x 10 grid (zero based)
    
    if line < 2 linestart = 0
    if line > 8 lineend = 0
    if column < 2 colstart = 0
    if column > 8 colend = 0
    
    For auxlinha = linestart To lineend
       For auxcolumn = colstart To colend
         // check
       Next     
    Next 
    

    Personally though I wouldn't bother with the loops, they add very little to nothing

    HasMineAbove = (line > 1) and (gamefield[line -1,column] = MinePresentValue

    would be my approach, do it all in one.

    Not to mention the huge potential confusion when auxlinha and auxcolumn are both zero...