240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri...

16
240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison

Transcript of 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri...

Page 1: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

1

-240492

Games Programming with Java

Montri Karnjanadecha Andrew Davison

Page 2: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

2 240-492 Games Programming with Java

::: AI :::

Chapter 2

Introduction to Artificial Intelligence

Page 3: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

3

Outline

• Definition of Artificial Intelligence

• Uninformed Search Methods

• Informed Search Methods

• Game AI

Page 4: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

4

What is AI?

• Systems that think like humans– “The exciting new effort to make computer think ….. m

achine with minds, in the full an literal sense”

– “Activities that we associate with human thinking, activities such as decision-making, problem-solving, learning…”

• Systems that think rationally– “The study of mental faculties through the use of comp

utational models”

– “The study of the computations that make it possible to perceive, reason, and act”

Page 5: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

5

That is AI? (cont’d)

• Systems that act like humans– “The art of creating machines that perform functions

that require intelligence when performed by people”

– “The study of how to make computers do things at which, at the moment, people are better”

• Systems that act rationally– “A field of study that seeks to explain and emulate

intelligent behavior in terms of computational processes”

– “The branch of computer science that is concerned with the automation of intelligent behavior”

Page 6: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

6

What is AI? (cont’d)

To make the computer think

Page 7: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

7

Simple AI Stuff

A board with tokens

Page 8: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

8

Closing the gap

A board with tokens

Page 9: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

9

Chase Algorithm

if (lx!=dx) // check for non-matching x{

if (lx>dx){

lx--; // higher x, so decrease it}else{

lx++; // lower x, so increase it}

}if (ly!=dy) // check for non-matching y{

if (ly>dy){

ly--; // higher y, so decrease it}else{

ly++; // lower y, so increase it}

}

Page 10: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

10

Evade Algorithm

if (lx!=dx) // check for non-matching x{

if (lx>dx){

lx++; // higher x, so increase it}else{

lx--; // lower x, so decrease it}

}if (ly!=dy) // check for non-matching y{

if (ly>dy){

ly++; // higher y, so increase it}else{

ly--; // lower y, so decrease it}

}

Page 11: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

11

Chasing Struck

The simple chase algorithm fails when unmovable blocks are presented

Page 12: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

12

Evading the Obstacle

Page 13: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

13

More Advanced Pathfinding

• A* pathfinding algorithm

-1 -1 -1 -1 -1 -1 -1 -1

-1 0 -1 -1 -1 -1 -1 -1

-1 -1 -1 255 255 255 -1 -1

-1 -1 -1 255 -1 -1 -1 -1

-1 -1 -1 255 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1

1. Create a 2-D integer array as large as the map size

2. Fill all cells with -1 (-1 means not tested yet)

3. For any “blocked” cell, put a large number (e.g. 255)

4. Place a 0 in the target cell

Page 14: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

14

A* pathfinding algorithm

-1 -1 -1 -1 -1 -1 -1 -1

-1 0 -1 -1 -1 -1 -1 -1

-1 -1 -1 255 255 255 -1 -1

-1 -1 -1 255 -1 -1 -1 -1

-1 -1 -1 255 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1

5. Start at the target cell (the known distance cell)

6. Determine valid adjacent cells

7. Give the adjacent cells a known distance value

8. Repeat step 5

Stop Citeria

1. No adjacent cells

2. The cell of origin gets filled in

• Path finding

Page 15: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

15

When It’s All Done

1 1 1 2 3 4 5 6

1 0 1 2 3 4 5 6

1 1 1 255 255 255 6 6

2 2 2 255 6 6 6 6

3 3 3 255 5 6 7 7

4 4 4 4 5 6 7 8

5 5 5 5 5 6 7 8

6 6 6 6 6 6 7 8

5. Start at the target cell (the known distance cell)

6. Determine valid adjacent cells

7. Give the adjacent cells a known distance value

8. Repeat step 5

Stop Criteria

1. No adjacent cells

2. The cell of origin gets filled in

• Path finding

Page 16: 240-492 Games Programming with Java ::: AI ::: 1 240-492 Games Programming with Java Montri Karnjanadecha Andrew Davison.

240-492 Games Programming with Java ::: AI :::

16

Backtracking

• In order for the red piece to capture the green piece, the red piece must move to lower-values cells-- from 7->6, 6-> 5, 5->4, 4->3, 3->2, 2->1 and 1->0