Справочник Жаркова по проектированию и программированию искусственного интеллекта. Том 6: Программирование на Visual Basic искусственного интеллекта. Продолжение 2
Шрифт:
LinearGradientBrush
Dim transLocation As Point = _
PointTranslator.TranslateToBL(location)
Dim brushpt1 As Point = transLocation
Dim brushpt2 As New Point(transLocation.X + Block.BlockSize _
+ 4, transLocation.Y – BlockSize – 4)
Dim brush As New LinearGradientBrush(brushpt1, _
brushpt2, Me.Color, System.Drawing.Color.White)
Return brush
End Function
End Class
По второму варианту, в панели Solution Explorer выполняем правый
Листинг 20.17. Новый файл.
''' <summary>
''' This class represents the grid of blocks. It handles most of
''' the game play.
''' </summary>
''' <remarks></remarks>
Public Class Grid
' The grids is 12 columns and 15 rows of Block objects.
Dim matrix(11, 14) As Block
''' <summary>
''' Creates a few rows of blocks to start the game.
''' Game starts with Red, Blue, and Green blocks.
''' </summary>
''' <param name="nrows">Number of rows of blocks to create
''' to start the game.</param>
''' <remarks></remarks>
Public Sub New(ByVal nrows As Integer)
If nrows > matrix.GetLength(0) Then
Throw New Exception("Must start with " & _
matrix.GetLength(0) & " or fewer rows.")
End If
Dim row As Integer
Dim column As Integer
For row = 0 To nrows – 1
For column = 0 To matrix.GetLength(1) – 1
matrix(row, column) = New Block( _
New Color {Color.Red, Color.Blue, Color.Green})
Next
Next
For row = nrows To matrix.GetLength(0) – 1
For column = 0 To matrix.GetLength(1) – 1
matrix(row, column) = Nothing
Next
Next
End Sub
''' <summary>
''' A new row may be added at any time. New rows have Gray
''' blocks in addition
''' to Red, Blue, and Green. This makes the game more difficult.
''' </summary>
''' <remarks></remarks>
Public Sub AddRow
Dim column As Integer
' Add a new block to each column.
For column = 0 To matrix.GetLength(1) – 1
Dim newBlock As New Block(New Color _
{Color.Red, Color.Blue, Color.Green, Color.Gray})
' Add the new block at the botttom of the column,
' 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
''' <summary>
''' Draw the grid of blocks
''' </summary>
''' <param name="graphics"></param>
''' <param name="backColor"></param>
''' <remarks></remarks>
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
''' <summary>
''' This method responds to a click event in the UI.
''' </summary>
''' <param name="point"></param>
''' <returns>The number of blocks removed from the grid.</returns>
''' <remarks></remarks>
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
''' <summary>
''' Find out how many blocks will be eliminated.