Artificial intelligence

One of the most critical aspects of many games is the behaviour of computer controlled characters - whether they are rivals of the player, supporters, or simply there to add flavour to the game.

Getting these characters to behave in a way that is both interesting and (from the player point of view) logically consistent with the game world is a tremendous challenge.

For the moment, we'll divide control of the AI characters into two broad areas:


Character Movement

The AI characters generally need to move about the game world in some manner that the player will accept as believable (in the context of the game and situation).

We will consider several approaches to controlling character movement:

Random movement

The easiest of approaches from a code/logic point of view, random movement periodically sets or changes the speed and direction of travel for the AI.

Speed/direction might be reset at fixed intervals, and/or in response to specific triggers (e.g. when the AI reaches an intersection, finds its way blocked, etc).

This can be handy for wandering characters, adding a level of unpredictability and variation to the game, but care must be taken to avoid having the character do things that detract from believability or gameplay, e.g.

Of course, for the character to change speed/direction in response to triggers (e.g. intersections, dead ends, etc) then you must check for such events.

Pattern based movement

Another simple approach to AI movement is to establish fixed paths for the AI to follow.

This might be a circular path the AI repeats over and over, or a point-to-point path, dictating where it starts, where it ends, and how it moves in between.

Path patterns can be time based or point based, e.g.

As with random movement, care must be taken to ensure the planned paths do not create illogical or unduly difficult situations for the player.

Path finding

The idea in path finding is to have the AI assess where it is, where it wants to be, and to have it rationally choose how to get there.

Again, there are a variety of ways to achieve this, with a few of them outlined below.

Even with path finding algorithms, care must still be taken to ensure the paths do not create illogical or unduly difficult situations for the player.

Lab exercises on AI path planning and movement control


Character Behaviour

The AI characters need to respond appropriately to their environment.

Their actions will determine, in large part, how believable the game world is and how interesting and challenging the gameplay is.

If these characters existed in the real world, they would be constantly evaluating the world around them, observing what is taking place, planning for the future, etc. However, we lack the processing power (not to mention the coding skills) to emulate every aspect of this in a game.

As a result, we need some shorthand control approaches that make the character look like they're really 'thinking', but require less code and less processing power to evaluate.

This usually means identifying a small number of key actions, stimuli, goals, and events RELEVANT TO THE IN-GAME SITUATION that will be used to determine each character's actions.

There are several common techniques applied to determine character actions. We'll consider four approaches briefly, using each of them to model AI poker players.

The four techniques we'll look at are (pseudo) random behaviour, neural networks, rule based systems, and state machines. Before we look at the four techniques, let's discuss example a bit.

The idea is to have different AI characters that play poker against the human player(s), hopefully creating an interesting and challenging game for the player.

The general idea of the gameplay is that each hand consists of multiple rounds of drawing or revealing cards and betting based on the information available. (Details would of course depend on the specific flavour of poker being played.)

At each betting stage, the AI has five actions to choose from:

If the AI was another human, the information it would have available would be:

Because it is an AI, we could give the AI additional information (e.g. much more detailed calculations of the odds, possibly even knowledge about the player's hand - giving the AI a cheat to increase its difficulty).

Based on the informations and actions available, let's consider four approaches to determining AI behaviour.

Random behaviour

One of the easiest approaches to program that gives some degree of interest to AI actions is random behaviour.

We could assign each AI player different odds of picking each of the five available actions. For each AI player, generate a random value in the range 0-99 and consult the following table.
Action AI 1 AI 2 AI 3
Fold <=10 <=15 <=20
Call/check 11-30 16-30 21-59
Small raise 31-55 31-60 60-80
Medium raise 56-80 61-89 80-94
Large raise 81+ 90+ 95+

This would make AI 1 appear to be the more aggressive of the three characters, and AI 3 to be the more conservative. Unfortunately, since it ignores the information available to the AI, it will sometimes result in very poor decisions. (For example, an AI with an outstanding hand may fold.)

We could improve on this a bit by adding modifiers to the behaviour, e.g.:

Rule based systems

Rule based systems specify precise conditions for character behaviour.

This tends to give them more logical and consistent behaviour, but can involve intense coding and debugging and can also make the AI too predictable over long periods of gameplay.

A very simple example of rule-based behaviour might look something like:

Note that if you wanted different AI gambling personalities then you would need to either create seperate rule sets for each or include modifiers to the general behaviour rules, and set the modifiers differently for each AI.

In fact, a combination of rule based and random behaviour is often used - giving the AI reasonably logical behaviour but still providing a significant degree of unpredictability. (The modifiers used in the random behaviour section are an example of this blended approach.)

Neural networks

Another approach to try to simplify the decision making process (and code) associated with the AI, yet still create flexibility among AI personas, is to use a simplified neural net approach.

In its simplest form, this works as follows:

For example, the first table below lists the information the AI will take into account, and how it will weight that information.

The second table shows what range of final values will be associated with each action.
Information Weighting
InformationAI 1AI 2AI 3
Number of players "in" -1 1 0
Number of players with better showing hands -2 -1 -3
Cash on hand divided by maximum bet 2 1 0
Number of betting rounds remaining 2 1 0
Amount of cash in the pot 3 2 3
Action Determination
ActionAI 1AI 2AI 3
Fold < 0 <1 < -1
Call/check 0..2 1..4 -1..4
Small raise 2..5 4..6 4..8
Medium raise 5..8 6..10 8..16
Large raise 8+ 10+ 16+

For example, to determine the action for AI 1:

State based systems