Game Development Reference
In-Depth Information
The Framework
The first rule was a great start. It is time to add the framework so that we can add
another rule. Add a new class to the project and name it FrameWork.vb. The
framework is very easy to code; it depends on the rules being loaded in order of
increasing complexity and needs a place to store the rules. It also needs a routine
to match and then execute the best rule, as well as a routine for loading rules. Add
the following code to the class:
Private Rules As New Collection
Public Sub AddRule(ByVal goodIdea As BasicRule)
'Add it if it is not there
If Not Rules.Contains(goodIdea.GetType.Name) Then
'Use its type name as string
Rules.Add(goodIdea, goodIdea.GetType.Name)
End If
End Sub
Now we need the match and execute routine. It is far less complex than the rules
it invokes. Add the following routine to the FrameWork class:
Public Sub RunAI(ByVal RevealedSquare As Square)
'Keep the best rule and its score
Dim bestRule As BasicRule = Nothing
Dim bestScore As Integer = 0
'We want the playfield so that we can talk
Dim theField As PlayingField = RevealedSquare.Parent
Dim someRule As BasicRule
Dim currentScore As Integer
'Go through the rules we have loaded in order
For Each someRule In Rules
currentScore = someRule.Matches(RevealedSquare)
If currentScore > bestScore Then
'Best idea so far, at lowest cost
bestRule = someRule
bestScore = currentScore
End If
Next
'Did we get a good idea? If so, use it
 
Search Nedrilad ::




Custom Search