## Search in the document preview

Artificial Intelligence

A Simple Game

Me vs. a devil I have two possible moves: A or B If I make move A, devil pays me $4. If I make move B, I pay devil $3. Which move should I make?

Game Trees

Devil pays me

$4

I pay devil $3

A B

Game Trees

Devil pays me

$4

I pay devil $3

A B

Two Rounds

Devil pays me

$4

I pay devil $3

C D

I pay devil $1

I pay devil $2

E F

A B

Which move should I make?

Two Rounds

Devil pays me

$4

I pay devil $3

C D

I pay devil $1

I pay devil $2

E F

A B

Two Rounds

Devil pays me

$4

I pay devil $3

C D

I pay devil $1

I pay devil $2

E F

A B

Two Rounds

+4 -3 -1 -2

Three Rounds

-8 +4 -7 +5+6 -1 -3 +2

Three Rounds

-8 +4 -7 +5+6 -1 -3 +2

Three Rounds

-8 +4 -7 +5+6 -1 -3 +2

Three Rounds

-8 +4 -7 +5+6 -1 -3 +2

Minimax

This is called the **minimax** algorithm.

I'm trying to maximize my winnings, given that I expect my opponent will try to minimize my winnings.

Connect Four

Let's play Connect Four

Game Tree for Connect Four

1

7

49

343

Roughly how many possible game states after n moves? 7n

Why "roughly"? Some columns may be full. Someone may win/lose.

Connect Four

How long would it take a computer to determine the best starting move?

Too long!!!

After 20 moves, roughly 80,000,000,000,000,000 states!

A More Practical Algorithm

1. Look just a few moves ahead.

2. Then estimate how good the board appears.

Looking 2 moves ahead

Which of these boards is best for the red player?

Probably the ones on the ends.

Simplest estimation function

value of board for player = 1000 (if player has 4-in-a-row) -1000 (if other player has 4-in-a-row) 0 (otherwise)

Using our estimation function

What are the values of these boards for the red player?

They would all have a value of **zero**.

Implementing minimax ...

Looking ahead 1 move

How should the computer behave?

It should win when it can, and otherwise play randomly.

Looking ahead 2 moves

How should the computer behave?

It should win when it can, and block my wins, and otherwise play randomly.

Implementation Move bestMove = null; for (int col = 0; col < grid.getNumCols(); col++) {

if (!isFull(col)) { dropChecker(checker, col); int value; if (hasWon(checker)) value = 1000; else if (movesAhead == 1) value = (int)(Math.random() * 100); else {

Move move = **getBestMoveFor**(otherChecker(checker),
movesAhead - 1);

value = -move.getValue(); } if (bestMove == null || value > bestMove.getValue())

bestMove = new Move(col, value); removeChecker(col);

} } return bestMove;

Recursive Call

How would we have to change this to play chess instead?

Interlude: A Bad Joke

**Q**: Why are iPhone owners so good at
games?

**A**: Because they use mini Macs.

no comments were posted

be the one to write the first!