Читаем Справочник Жаркова по проектированию и программированию искусственного интеллекта. Том 6: Программирование на Visual Basic искусственного интеллекта. Продолжение 2 полностью

' and push the rest of the

' blocks up one column.

For row As Integer = matrix.GetLength(0) – 1 To 1 Step -1

matrix(row, column) = matrix(row – 1, column)

Next

matrix(0, column) = newBlock

Next

End Sub

'''

''' Draw the grid of blocks

'''

'''

'''

'''

Public Sub Draw(ByVal graphics As Graphics, _

ByVal backColor As Color)

graphics.Clear(backColor)

Dim row As Integer

Dim column As Integer

Dim theBlock As Block

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

Dim pointA As New Point( _

column * Block.BlockSize, _

row * Block.BlockSize)

matrix(row, column).Draw(graphics, pointA)

End If

Next

Next

End Sub

'''

''' This method responds to a click event in the UI.

'''

'''

''' The number of blocks removed from the grid.

'''

Public Function Click(ByVal point As Point) As Integer

' Figure out row and column.

Dim total As Integer

Dim transPt As Point = PointTranslator.TranslateToTL(point)

Dim selectedRow As Integer = transPt.Y \ Block.BlockSize

Dim selectedColumn As Integer = transPt.X \ Block.BlockSize

Dim selectedBlock As Block = matrix(selectedRow, _

selectedColumn)

If Not selectedBlock Is Nothing Then

selectedBlock.MarkedForDeletion = True

' Determine if any of the neighboring blocks are

' the same color.

FindSameColorNeighbors(selectedRow, selectedColumn)

' Determine how many blocks would be eliminated.

total = Me.CalculateScore

If total > 1 Then

Me.CollapseBlocks

Else

Me.ClearMarkedForDeletion

End If

End If

Return total

End Function

Private Sub ClearMarkedForDeletion

Dim row As Integer

Dim column As Integer

For column = matrix.GetLength(1) – 1 To 0 Step -1

' If column is completely empty, then move everthing

' down one.

For row = 0 To matrix.GetLength(0) – 1

If Not matrix(row, column) Is Nothing Then

matrix(row, column).MarkedForDeletion = False

End If

Next

Next

End Sub

'''

''' Find out how many blocks will be eliminated.

'''

'''

'''

Private Function CalculateScore As Integer

Dim row As Integer

Dim column As Integer

Dim total As Integer = 0

For column = matrix.GetLength(1) – 1 To 0 Step -1

' If column is completely empty, then move everthing

' down one.

For row = 0 To matrix.GetLength(0) – 1

If Not matrix(row, column) Is Nothing Then

If matrix(row, column).MarkedForDeletion Then

total += 1

End If

End If

Next

Next

Return total

End Function

'''

''' After the blocks are removed from the columns, there may be

''' columns that are empty. Move columns from right to left to

''' fill in the empty columns.

'''

'''

Public Sub CollapseColumns

Dim row As Integer

Dim column As Integer

For column = matrix.GetLength(1) – 1 To 0 Step -1

' If column is completely empty, then all the columns

' over one.

Dim noBlocks As Boolean = True

For row = 0 To matrix.GetLength(0) – 1

If Not matrix(row, column) Is Nothing Then

noBlocks = False

End If

Next

If noBlocks Then

Dim newcol As Integer

For newcol = column To matrix.GetLength(1) – 2

For row = 0 To matrix.GetLength(0) – 1

matrix(row, newcol) = matrix(row, newcol + 1)

Next

Next

newcol = matrix.GetLength(1) – 1

For row = 0 To matrix.GetLength(0) – 1

matrix(row, newcol) = Nothing

Next

End If

Next


End Sub

'''

''' Remove all the blocks from the grid.

'''

'''

Public Sub CollapseBlocks

Dim theBlock As Block

Dim column As Integer

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

'''

''' Provides access into the grid.

'''

'''

'''

'''

'''

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

'''

''' Set MarkedForDeletion to True for each neighboring block

''' of the same color.

'''

'''

'''

'''

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

'''

''' Look to the blocks on each side.

'''

'''

Private Sub FindNeighbors

' Take the first block out of the arraylist and examine it.

Перейти на страницу:

Похожие книги

Внедрение SAP R/3: Руководство для менеджеров и инженеров
Внедрение SAP R/3: Руководство для менеджеров и инженеров

Это практическое всеобъемлющие руководство было написано специально для тех, кто выбирает стратегию внедрения SAP в организации. «Внедрение SAP R/3: руководство для менеджеров и инженеров» объясняет, что означает понятие «эпоха ERP», почему информация является одним из ключевых ресурсов предприятия, как SAP способствует росту конкурентоспособности компании, а также преимущества методологии ASAP в планировании и использовании ресурсов при внедрении SAP. Подход к ERP-системам, используемый в данной книге, будет крайне полезен менеджерам и специалистам, которым необходимо представить высшему руководству своих компаний основания для внедрения SAP; кроме того, данная книга будет весьма полезной тем, кто занимается проектами SAP или планирует такой проект в ближайшем будущем. Для тех читателей, кто непосредственно занят в проектах SAP, эта книга станет надежным руководством и поможет внести существенный вклад в развитие проекта.

Вивек Кале

Зарубежная компьютерная, околокомпьютерная литература / Прочая компьютерная литература / Книги по IT