0

I've looked online for a flood fill algorithm in visual basic, but I couldn't find one that made sense to me. I am a beginner at vb, which is probably why.

I have tried to use Wikipedia's algorithm on flood fill, and I understand the logic of how it works, but I just don't know how to program it in Visual Basic.

 Flood-fill (node, target-color, replacement-color):
 1. If target-color is equal to replacement-color, return.
 2. If the color of node is not equal to target-color, return.
 3. Set the color of node to replacement-color.
 4. Perform Flood-fill (one step to the south of node, target-color, replacement-color).
    Perform Flood-fill (one step to the north of node, target-color, replacement-color).
    Perform Flood-fill (one step to the west of node, target-color, replacement-color).
    Perform Flood-fill (one step to the east of node, target-color, replacement-color).
 5. Return.

I am currently recreating the board game of Go, which requires a flood fill to search for the captured stones. Captured stones are defined as pieces which have been completely surrounded on all sides, horizontally and vertically, but not diagonally. When a piece, or stone, that has been placed in a particular spot where the opponents stones are completely surrounded, the surrounded pieces are removed off the board. So the flood fill does not start from the stone you just placed,rather it starts from the opponent's stone which you have just surrounded. The flood fill needs to be a recursion that occurs every time a player places a stone on the board.

I am doing this in Windows forms, and have attempted at this, but it seems very long winded(it isn't complete):

Dim northsouth As Integer
Dim eastwest As Integer

northsouth = gridPosition.X + 1
If placed_stone(northsouth, gridPosition.Y) = 2 Then
    eastwest = gridPosition.Y + 1
    If placed_stone(northsouth, eastwest) = 1 Then
        eastwest = gridPosition.Y - 1
        If placed_stone(northsouth, eastwest) = 1 Then
            northsouth = gridPosition.X + 2
            If placed_stone(northsouth, gridPosition.Y) = 1 Then
                northsouth = gridPosition.X + 1
                Grid(gridPosition.X)(gridPosition.Y) = True 'Place a black stone at Grid(x,y)
                Grid(northsouth)(gridPosition.Y) = Nothing
                placed_stone(northsouth, gridPosition.Y) = 0
                pass = False
                cp = False
                passbtn.BackColor = Color.White 'The passbutton changes colour to white
                passbtn.ForeColor = Color.Black 'The passbutton font changes colour to black
            End If
        End If
    End If
End If

Placed_stone is a 2d array representation of the board. I have used "0", "1" and "2" to represent empty, black and white respectively in this 2d array.

Can you please tell me how to code and implement flood fill to this situation? If anyone would like to look at the whole code, please let me know. I would appreciate any suggestions!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

0 Answers0