Справочник Жаркова по проектированию и программированию искусственного интеллекта. Том 6: Программирование на Visual Basic искусственного интеллекта. Продолжение 2
Шрифт:
Аналогично добавляется и проектируется следующая форма, которая должна выводиться после выбора команды “О программе” в меню Помощь.
Свойства всех элементов управления можно стандартно изменять, как описано ранее.
21.4. Код программы
Открываем файл Form1.vb (например, так: File, Open, File) и вверху записываем директиву для подключения требуемого
Imports System.IO 'Для класса StreamWriter.
Напомним, что эту строку можно и не записывать, но тогда нам придётся перед каждым классом записывать это пространство имён.
Теперь в классе Form1 нашего проекта записываем следующие переменные и методы.
Листинг 21.1. Переменные и методы.
Const intBaseX As Integer = 10
Const intBaseY As Integer = 120
Dim Rand As New Random
'***
Dim playerName As String
Dim playerScore As Double
Dim playerTime As Integer
Dim DDScore As New DPaint
Dim DDTime As New DPaint
'***
Dim intFlag As Integer = -1
Dim flagMadeNew = 0
Dim posMoveTo As Integer
Dim MPBoxes(80) As MotionPic
Dim ThreeBI(2) As Integer
Dim ThreeBP(2) As Integer
Dim prePic(2) As PictureBox
Private Sub InitBoard(ByVal plName As String, _
ByVal plScore As Double, ByVal plTime As Integer)
playerName = plName
playerScore = plScore
playerTime = plTime
If flagMadeNew = 0 Then
Dim i As Integer
Dim intX = intBaseX + 2
Dim intY = intBaseY + 2
For i = 0 To 80
Dim MP As New MotionPic(New Size(36, 36), _
New Point(intX, intY))
MP.SizeMode = PictureBoxSizeMode.StretchImage
intX += 45
If (i + 1) Mod 9 = 0 Then
intY += 45
intX = intBaseX + 2
End If
AddHandler MP.Click, AddressOf Ball_Click
MPBoxes(i) = MP
Next
Me.Controls.AddRange(MPBoxes)
DDScore.width = lblScore.Height / 2 – 6
DDScore.thick = DDScore.width / 4
DDScore.position = New Point(lblScore.Width – _
(DDScore.width + 2) * 9, lblScore.Height / 2)
DDTime.width = lblTime.Height / 2 – 6
DDTime.thick = DDTime.width / 4
DDTime.position = New Point(lblTime.Width – _
(DDTime.width + 2) * 9, lblTime.Height / 2)
AddHandler lblScore.Paint, AddressOf LabelScore_Paint
lblScore.Refresh
AddHandler lblTime.Paint, AddressOf LabelTime_Paint
lblTime.Refresh
For i = 0 To 2
prePic(i) = New PictureBox
prePic(i).SizeMode = PictureBoxSizeMode.StretchImage
prePic(i).Size = New Size(16, 16)
prePic(i).Visible = False
Me.Controls.Add(prePic(i))
AddHandler prePic(i).Click, AddressOf PrePic_Click
prePic(i).BringToFront
Next
Else
ResetBoard
End If
lblNameShow.Text = playerName
If playerName.Length > 8 Then
lblNameShow.Text += " "
tmr1.Enabled = True
End If
tmr2.Enabled = True
DDScore.number = plScore
lblScore.Refresh
DDTime.number = plTime
lblTime.Refresh
PreShow
End Sub
Private Sub FindSol(ByVal i As Integer)
If MPBoxes(i).Tag <> "" Or MPBoxes(i).Tag = "Here" Then
Return
Else
MPBoxes(i).Tag = "Here"
End If
Select Case TestABox(i)
Case 1
FindSol(1)
FindSol(9)
Case 2
FindSol(7)
FindSol(17)
Case 3
FindSol(71)
FindSol(79)
Case 4
FindSol(63)
FindSol(73)
Case 5
FindSol(i + 1)
FindSol(i + 9)
FindSol(i – 1)
Case 6
FindSol(i – 9)
FindSol(i – 1)
FindSol(i + 9)
Case 7
FindSol(i – 1)
FindSol(i – 9)
FindSol(i + 1)
Case 8
FindSol(i – 9)
FindSol(i + 1)
FindSol(i + 9)
Case Else
FindSol(i – 9)
FindSol(i + 9)
FindSol(i + 1)
FindSol(i – 1)
End Select
End Sub
Private Sub ResetAllTag
For Each Pic As MotionPic In MPBoxes
If Pic.Tag = "Here" Then
Pic.Tag = ""
End If
Next
End Sub
Private Function TestABox(ByVal val As Integer)
Select Case val
Case 0 : Return 1
Case 8 : Return 2
Case 80 : Return 3
Case 72 : Return 4
Case 1 To 7 : Return 5
Case 73 To 79 : Return 7
Case 17, 26, 35, 44, 53, 62, 71 : Return 6
Case 9, 18, 27, 36, 45, 54, 63 : Return 8
Case Else : Return 0
End Select
End Function
'Serious trouble happened – think more
Private Function GiveThreeBalls As Boolean
If ThreeBI(1) = -1 Then 'Review for Game over
Return False
Else
For i As Integer = 0 To 2
If ThreeBI(i) = -1 Then
Exit For
Else
If MPBoxes(ThreeBP(i)).MPState = BallState. _
NO_BALL And ThreeBP(i) <> posMoveTo Then
MPBoxes(ThreeBP(i)).Init(ThreeBI(i))