Dim row As Integer
Dim aRow As Integer
' First remove the blocks from each column.
For column = 0 To matrix.GetLength(1) – 1
For row = matrix.GetLength(0) – 1 To 0 Step -1
theBlock = matrix(row, column)
If (Not theBlock Is Nothing) Then
If theBlock.MarkedForDeletion Then
For aRow = row To matrix.GetLength(0) – 2
matrix(aRow, column) = _
matrix(aRow + 1, column)
Next
matrix(matrix.GetLength(0) – 1, _
column) = Nothing
End If
End If
Next
Next
' Reset the MarkedForDeletion flags.
For row = 0 To matrix.GetLength(0) – 1
For column = 0 To matrix.GetLength(1) – 1
theBlock = matrix(row, column)
If Not theBlock Is Nothing Then
theBlock.MarkedForDeletion = False
End If
Next
Next
' Remove any columns that are now empty.
CollapseColumns()
End Sub
''' <summary>
''' Provides access into the grid.
''' </summary>
''' <param name="row"></param>
''' <param name="column"></param>
''' <value></value>
''' <remarks></remarks>
Default Public Property Item(ByVal row As Integer, _
ByVal column As Integer) As Block
Get
Return matrix(row, column)
End Get
Set(ByVal Value As Block)
matrix(row, column) = Value
End Set
End Property
Private blocksToExamine As ArrayList
''' <summary>
''' Set MarkedForDeletion to True for each neighboring block
''' of the same color.
''' </summary>
''' <param name="row"></param>
''' <param name="column"></param>
''' <remarks></remarks>
Private Sub FindSameColorNeighbors(ByVal row As Integer, _
ByVal column As Integer)
Dim color As Color = matrix(row, column).Color
blocksToExamine = New ArrayList
blocksToExamine.Add(New Point(row, column))
matrix(row, column).MarkedForDeletion = True
' Each time you find a neighbor, mark it for deletion, and
' add it to the list of blocks to look for neighbors.
' After you
' examine it, remove it from the list. Keep doing this
' until there are no more blocks to look at.
While blocksToExamine.Count > 0
FindNeighbors()
End While
End Sub
''' <summary>
''' Look to the blocks on each side.
''' </summary>
''' <remarks></remarks>
Private Sub FindNeighbors()
' Take the first block out of the arraylist and examine it.
Dim location As Point = CType(blocksToExamine(0), Point)
Dim currentBlock As Block = matrix(location.X, location.Y)
Dim row As Integer = location.X
Dim column As Integer = location.Y
blocksToExamine.RemoveAt(0)
Dim nextRow As Integer
Dim nextCol As Integer
Dim selected As Block
' look up
If row < matrix.GetLength(0) – 1 Then
nextRow = row + 1
selected = matrix(nextRow, column)
ExamineNeighbor(selected, nextRow, column, _
currentBlock.Color)
End If
' look down
If row > 0 Then
nextRow = row – 1
selected = matrix(nextRow, column)
ExamineNeighbor(selected, nextRow, column, _
currentBlock.Color)
End If
' look left
If column > 0 Then
nextCol = column – 1
selected = matrix(row, nextCol)
ExamineNeighbor(selected, row, nextCol, _
currentBlock.Color)
End If
' look right
If column < matrix.GetLength(1) – 1 Then
nextCol = column + 1
selected = matrix(row, nextCol)
ExamineNeighbor(selected, row, nextCol, _
currentBlock.Color)
End If
End Sub
''' <summary>
''' If the neighbor is the same color, add it to the blocks
''' to examine.
''' </summary>
''' <param name="selected"></param>
''' <param name="row"></param>
''' <param name="column"></param>
''' <param name="color"></param>
''' <remarks></remarks>
Private Sub ExamineNeighbor(ByVal selected As Block, _
ByVal row As Integer, ByVal column As Integer, _
ByVal color As Color)
If Not selected Is Nothing Then
If selected.Color.Equals(color) Then
If Not selected.MarkedForDeletion Then
selected.MarkedForDeletion = True
blocksToExamine.Add(New Point(row, column))
End If
End If
End If
End Sub
End Class
По второму варианту, в панели Solution Explorer выполняем правый щелчок по имени проекта и в контекстном меню выбираем