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

Шрифт
Фон

По второму варианту, в панели Solution Explorer выполняем правый щелчок по имени проекта и в контекстном меню выбираем Add, New Item, в панели Add New Item выделяем шаблон Code File, в окне Name записываем имя Block.vb и щёлкаем кнопку Add. В проект (и в панель Solution Explorer) добавляется этот файл, открывается пустое окно редактирования кода, в которое записываем код со следующего листинга.

Листинг 20.16. Новый файл.

Imports System.Drawing.Drawing2D

'''

''' This class represents one of the balls in the game grid.

'''

'''

Public Class Block

Public Const BlockSize As Integer = 25

Private colorValue As Color

Private deletionValue As Boolean = False

Private Shared rand As New Random

Public Property Color() As Color

Get

Return colorValue

End Get

Set(ByVal Value As Color)

colorValue = Value

End Set

End Property

Public Property MarkedForDeletion() As Boolean

Get

Return deletionValue

End Get

Set(ByVal Value As Boolean)

deletionValue = Value

End Set

End Property

Public Sub New(ByVal newColor As Color)

colorValue = newColor

End Sub

Public Sub New(ByVal colors() As Color)

Dim ncolors As Integer = colors.Length

Dim pickedColor As Integer

pickedColor = rand.Next(0, ncolors)

colorValue = colors(pickedColor)

End Sub

Public Sub Draw(ByVal graphics As Graphics, ByVal point As Point)

Dim brush As System.Drawing.Drawing2D.LinearGradientBrush = _

CreateTheBrush(point)

DrawTheCircle(graphics, brush, point)

End Sub

Private Sub DrawTheCircle(ByVal graphics As Graphics, _

ByVal brush As LinearGradientBrush, ByVal location As Point)

Dim topleft As Point = location

Dim bottomright As Point = New Point(location.X + _

BlockSize, location.Y + BlockSize)

Dim transTopLeft As Point = PointTranslator.TranslateToBL( _

topleft)

Dim transBottomRight As Point = _

PointTranslator.TranslateToBL(bottomright)

Dim transwidth As Integer = transBottomRight.X transTopLeft.X

Dim transheight As Integer = _

transBottomRight.Y transTopLeft.Y

graphics.FillEllipse(brush, New Rectangle(transTopLeft, _

New Size(transwidth, transheight)))

End Sub

Private Function CreateTheBrush(ByVal location As Point) As _

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 выполняем правый щелчок по имени проекта и в контекстном меню выбираем Add, New Item, в панели Add New Item выделяем шаблон Code File, в окне Name записываем имя Grid.vb и щёлкаем кнопку Add. В проект (и в панель Solution Explorer) добавляется этот файл, открывается пустое окно редактирования кода, в которое записываем код со следующего листинга.

Листинг 20.17. Новый файл.

'''

''' This class represents the grid of blocks. It handles most of

''' the game play.

'''

'''

Public Class Grid

' The grids is 12 columns and 15 rows of Block objects.

Dim matrix(11, 14) As Block

'''

''' Creates a few rows of blocks to start the game.

''' Game starts with Red, Blue, and Green blocks.

'''

''' Number of rows of blocks to create

''' to start the game.

'''

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

'''

''' 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.

'''

'''

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

'''

''' 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.

Ваша оценка очень важна

0
Шрифт
Фон

Помогите Вашим друзьям узнать о библиотеке