Game Development Reference
In-Depth Information
Implementing the AI
Switch to Person.vb. We will sub-class the parent class for each different person.
This will make the code easy to understand. We start by working on the parent
class. Add the MustInherit keyword to the class definition:
Public MustInherit Class Person
That forces us to make child classes that inherit from this parent class. The parent
class will carry code that is common to all the child classes. Add the following to
the class:
'Everybody picks the same way; do it here in the parent class
Public Function Pick(ByVal Cash As Double, _
ByVal Occupations As Collection) As Job
'Prime the loop
Dim bestJob As Job = CType(Occupations(1), Job)
Dim bestValue As Double = Me.Evaluate(bestJob, Cash)
'Loop values:
Dim otherJob As Job
Dim otherValue As Double
For Each otherJob In Occupations
'Can I afford 2 days of this job?
If 2.0 * otherJob.Cost <= Cash Then
'How much do I like it?
otherValue = Me.Evaluate(otherJob, Cash)
'More than what I have?
If otherValue > bestValue Then
bestJob = otherJob
bestValue = otherValue
End If
End If
Return bestJob
End Function
'Everybody evaluates jobs their own way.
Public MustOverride Function Evaluate(ByVal Task As Job, _
ByVal Cash As Double) As Double
The last line tells any child classes to provide a way to evaluate a given job. This is
the member that will use the equations we developed for each person that gives a
Search Nedrilad ::

Custom Search