Artificial Intelligence for Games by Ian Millington

Artificial Intelligence for Games by Ian Millington

Author:Ian Millington [Millington, Ian & Funge, John]
Language: eng
Format: epub
Published: 2014-05-04T04:00:00+00:00


438 Chapter 5 Decision Making

5

def matchesItem(item, bindings):

6

7

# Is the item of the same type?

8

if not item insistence Datum: return false

9

10

# Does the identifier match?

11

if identifier.isWildcard() and

12

identifier != item.identifier: return false

13

14

# Does the value fit?

15

if minValue <= item.value <= maxValue:

16

17

# Do we need to add to the bindings list?

18

if identifier.isWildcard():

19

20

# Add the binding

21

bindings.appendBinding(identifier, item)

22

23

# Return true, since we matched

24

return true

25

else: return false

The isWildcard method should return true if the identifier is a wild card. If you used strings as

identifiers and wanted to use LISP-style wild-card names, you could check that the first character

is a question mark, for example. The implementation on the website uses a 4-byte number as

an identifier and reserves the first bit to indicate if the identifier is a wild card. The isWildcard

method simply checks this bit.

Library

The bindings list has been given an appendBinding method that adds an identifier (which is

always a wild card) and the database item it was matched to. If we are using an STL list in C++,

for example, we could have it be a list of pair templates and append a new identifier, item pair.

Alternatively, we could use a hash table indexed by identifier.

Data Group Matching

A test data group will match a database data group if its identifier matches and if all its children

match at least one child of the database data group. Not all the children of the database data group

need to be matched to something.

For example, if we are searching for a match to:

1

(?anyone (Health 0-54))

5.8 Rule-Based Systems

439

we would like it to match:

1

(Captain (Health 43) (Ammo 140))

even though ammo isn’t mentioned in the test data group.

The matchesItem function for data groups has the following form:

1

struct DataGroupMatch(DataNodeMatch):

2

3

# ... Member data as before

4

5

def matchesItem(item, bindings):

6

7

# Is the item of the same type?

8

if not item insistence DataGroup: return false

9

10

# Does the identifier match?

11

if identifier != WILDCARD and

12

identifier != item.identifier: return false

13

14

# Is every child present

15

for child in self.children:

16

17

# Use the children of the item as if it were a

18

# database and call matches recursively

19

if not child.matches(item.children):

20

return false

21

22

# We must have matched all children

23

24

# Do we need to add to the bindings list?

25

if identifier.isWildcard():

26

27

# Add the binding

28

bindings.appendBinding(identifier, item)

29

30

return true

Summary

Figure 5.47 shows all our classes and interfaces in one diagram.

440 Chapter 5 Decision Making

Figure 5.47

UML of the matching system



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.