9.1. b oard g ames and a iming ai common board game ai approaches and aiming ai

18
9.1. BOARD GAMES AND AIMING AI Common board game AI approaches and Aiming AI

Upload: victor-crawford

Post on 26-Dec-2015

233 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

9.1. BOARD GAMES AND AIMING AICommon board game AI approaches and Aiming AI

Page 2: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

BOARD GAME AICommon AI approaches of use to board games

Execution Management

World Interface

Movement

S t r a t e g y

D e c i s i o n M a k i n g

Animation Physics ...

Page 3: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

The first applications of AI in computer games was applied to playing board games (e.g. Chess, Tic-Tac-Toe, Go, etc.).

Most board game algorithms assume two-players (but often can be extended to cater for more players). Depending upon the game, players can make moves or take their turn.

Board game AI

Page 4: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

A board game in which the win of one player is equivalent to the loss of the other player(s) is known as a zero-sum game (e.g. chess, but not some casino games where players compete against the house).

In a zero-sum game the focus can be on trying to win or make the opponent lose (the outcome is the same). In a non-zero-sum game, the focus should be on the player winning.

The complexity increases for games with more than two players (even in zero-sum games) as the best strategy may involve temporary alliances against a stronger opponent, etc.

Board game AI (zero-sum games)

Page 5: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

In many board games the entire state of the board is known (i.e. nothing is hidden) – known as perfect information.

This entails that every possible move is known and the outcome of that move can also be determined.

Games involving dice rolls or other forms of uncertainty are known as imperfect information.

Board game AI (perfect/imperfect information)

Aside: Most common board game algorithms are for two-player, perfect information, zero-sum games.

Page 6: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

A game tree represents the game evolution. Each node defines one possible board layout, with each branch representing a valid move.

A terminal node represents an end-of-game state.

Each terminal position assigns a score to each player (e.g. +1 for a win, -1 for a loss, 0 for a draw, or some other numeric measure of the size of win).

In zero-sum games, the total combined score will be zero.

Board game AI (the game tree)

O

X

O

X

X

O

X

X

O O

X

X

O O

X

X

O O

X

X

... O

X O

X

...

X O

X

...

Aside: The number of branches from each node is called the branching factor. Games with a high branching factor tend to be more challenging to model using board game AI.

Page 7: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

Board game AI should, given a defined board state, select the best move that is likely to lead towards a win. This is the job of a heuristic static evaluation function which scores a board position from the perspective of one player.

Given a terminal node, the returned score will be the final score for the game. In non-terminal nodes, the score reflects how likely the player is to win from the position (returning an increasing score relative to the strength of the position).

The heuristic function is game specific.

Board game AI (move evaluation)

Aside: In the game of Reversi the best mid-game strategy is to favour moves that minimise the number of own-coloured counters to maximize control (and hence counter count) in the end-game.

In Chess aspects measured within the static evaluation function can include tempo, board control, piece count, etc. Different AI personalities can be easily embedded, e.g. scoring bishops higher than knights, etc.

Page 8: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

The minimaxing approach operates by assuming each move of a player will be to maximize their score, whilst each move of the opponent will be to effectively minimise the player score (assuming a zero-sum game).

Hence the minimax search alternates between maximising the player score on their turn and minimizing the player score on the opponents turn.

By recording the net scores and looking a number of turns ahead the minimax search can then select the best move the player should make.

Board game AI (minimaxing approach)

Page 9: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

A recursive minimax is defined below, a maximum search depth is used to determine when the search will stop.

Minimaxing algorithm

[Score, Move] minimax(Board boardState, Player player, int maxDepth, int currentDepth) {

if(boardState.gameOver() || currentDepth == maxDepth )return [boardState.evaluate(player) , None]

Move bestMove;float bestScore = (board.currentPlayer() == player ? -maxScore:maxScore);

foreach( Move move in boardState.getValidMoves() ) {

Board newBoardState = boardState.makeMove(move);[int currentScore, Move currentMove] =

minimax( newBoardState, player, maxDepth, currentDepth+1);

if( boardState.currentPlayer() == player ) {if( currentScore > bestScore ) {

bestScore = currentScore; bestMove = move; } } else if( currentScore < bestScore ) {

bestScore = currentScore; bestMove = move; }

}

return [bestScore, bestMove];

Stop the recursion if maximum depth reached or terminal node foundElse, find best move

The code assumes the minimax search returns a best move and score. In Java/C# either return a wrapper or pass move by reference

Update current best score depending on if we will maximise or minimise

Consider each possible move

Perform recursion

Move getBestMove(Board boardState, Player player) {

[Score score, Move move] =

minimax(boardState, player,

maxDepth, 0);

return move}

Page 10: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

A wide range of other techniques can be used within board game AI, including:

• Use of alpha-beta pruning (AB pruning) to prune out sections of the search tree that cannot hold the optimal solution

• Use of transposition tables to avoid creating duplicate game states and improving search times

• Use of opening books and other databases of expert encoded knowledge, including set plays.

Board game AI (other approaches)

Page 11: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

AIMING AND FIRINGAiming and firing solutions

Execution Management

World Interface

Movement

S t r a t e g y

D e c i s i o n M a k i n g

Animation Physics ...

Page 12: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

A lot of games involve projectiles of some sort (be they arrows, missiles, footballs, etc.).

It can be important for an AI controlled character to ‘understand’ the projectile’s trajectory in order to aim or react to incoming projectiles

Aiming and Firing

Page 13: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

A projectile fired with an initial force that is subject to a gravitational force (ignoring air resistance and powered projectiles, e.g. rockets) will follow a curved trajectory (a parabola).

The path can be defined as:

is the position at time t

is the initial position

is the initial direction of firing

is the speed of firing

is the gravitational vector

Aiming and Firing (projectile trajectory)

Aside: Earth’s gravitational force is 9.81 ms-2 ‘downwards’. This may not be the best value to use in a game, i.e. experiment with different values.

Initial position

movement due to firing

movement due to gravity

Page 14: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

The landing spot is found by solving for a given ‘floor’ height for the target (i.e. the y vector component).

is the y component of the direction of firing, gravity, position at time 0 and time t respectively.

There can be 0, 1 or 2 solutions. If zero, the projectile never reaches the target. If one, it reaches it at the apex of travel.

If there are two solutions, target height is reached on way up and way down (the later projectile descending is of interest).

Aiming and Firing (landing position)

Aside: The previous equation is a quadratic equation (i.e. involving x2). Quadratic equations can be solved using the following (which provides two solutions – not necessarily distinct, and maybe not real).

Page 15: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

Aside: As gravity is often only in the y (downward) direction, the eq. can be simplified to:

Substituting the solution time into the trajectory equation of the point, the point of collision is found by (in 3D, for 2D remove z component):

Aiming and Firing (landing position)

Page 16: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

In order to hit a target at a specific location we need to solve the previous equations for a given target location.

The firing location will be known and often gravitational attraction and initial firing speed are assumed to be fixed, i.e. the direction of firing is varied in order to hit the target location.

Aiming and Firing (firing direction)

Short-term trajectory

Long-term trajectory

Target

Aside: Usually there will be two solutions representing different firing arcs to the target. Typically the arc with the lower time is selected.

Page 17: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

Aiming AlgorithmVector? calculateFiringSolution(

Vector start, Vector target, Vector gravity, float speed ) {

Vector delta = start – end;

float a = gravity ● gravity;float b = -4 * (gravity ● delta + speed * speed)float c = 4 * delta ● delta

if( 4*a*c > b*b ) return null;

float t1 = Math.sqrt((-b + sqrt(b*b-4*a*c)) / (2*a));float t2 = Math.sqrt((-b - sqrt(b*b-4*a*c)) / (2*a));

float t;if( t1 < 0 ) {

if( t2 < 0 ) return null; else t = t1;} else {

if( t2 < 0 ) t = t1; else t = Math.min( t1, t2 );}

return (2 * delta – gravity * t*t) / (2 * speed * t);

Calculate (a,b,c) coefficients of the normal quadratic equation

Return if no real solutions can be found

The scalar product of two vectors, i.e. A●B is defined as AxBx + AyBy +...

Determine the two solutions

Return null if no positive time solutions were found

Return firing vector

Page 18: 9.1. B OARD G AMES AND A IMING AI Common board game AI approaches and Aiming AI

Summary

To do:Consider use of

minimax or aiming and

firing algorithms if

applicable to game

Work towards the

alpha-handin at the end

of this week.

Today we explored:

Board game AI and the use of the minimax algorithm

Aiming and firing algorithms for simple trajectories