17
Daniel Strebel Eduardo Hernández Marquina Advanced Functional Programming PASTURE

Daniel Strebel Eduardo Hernández Marquina Advanced Functional Programming

Embed Size (px)

Citation preview

Daniel StrebelEduardo Hernández Marquina

Advanced Functional Programming

PASTURE

The board:

A grid where the different elements are going to interact.

The Problem

Elements

»Fences:• Static elements

»Grass:• Procreate

The Problem Cont.

Elements Cont:

Abilities:

- Move

- Eat

- Procreate

The Problem Cont.

Design

GameBoard

Design cont.

Fox_1 Rabbit_1

Grass_1

Rabbit_2

{4,3}, rabbit_

1

{3,6}, fox_2

{1,4}, grass_2

{4,8}, grass_3

{7,3}, grass_1

{5,8}, rabbit_

2

{8,5}, fox_1

GameBoard

Todo

Position-PID

Design cont.GameBoard

Initialization

Todo > 0

Build new Todo

Wait for Message

NO

YES

Design cont.GameBoard

GameBoard

tim

e

{start}

{move, 3,2 }{ok}

{req_neighbors}{neighbors, [{5,4,

fox},.]}

Design cont.GameBoard

GameBoard

tim

e

{start}

{move, 3,2 }{conflict, [{5,4,

fox},.]}

{req_neighbors}{neighbors, [{5,4,

fox},.]}

{eat, 4,4}

Request not valid!

{ok}

Demo

• Missing AI- Now action choices are random.

• How to add simple AI?- Need to know the positions around.- Develop action priorities.

1.- Stay alive: scape from predators or eat.2.- To find food: move away from borders.

Limitations

Algorithms

Atoms for element types

Code sharing between animals

Used libraries

Algorithms cont

animal_loop(Position, States, Default_States, Types) -> receive

{die, _} -> urks;{start, Gameboard_PID} -> Gameboard_PID ! {request_neighbors, Position, self()}, receive

{die, _} -> urks;{neighbors, Neighbor_List} -> decide_animal(Neighbor_List, Gameboard_PID,

Position, States, Default_States, Types)

end end.

{Eat_timer, Procreation_timer, Starvation_timer, Move_timer}

{Own_Type, Prey_Type}

Algorithms cont

decide_animal(Neighbor_List, Gameboard_PID, Position, States, Default_States, Types) -> {Own_Type, Prey_Type} = Types, {Eat_timer, Procreation_timer, Starvation_timer, Move_timer} = States, Prey_Cells = [{X_cell, Y_cell} || {_, Type, X_cell, Y_cell}<-Neighbor_List, Type==Prey_Type], Free_Cells = [{X_cell, Y_cell} || {_, Type, X_cell, Y_cell}<-Neighbor_List, Type==none],

case {Free_Cells, Prey_Cells, Move_timer, Eat_timer, Procreation_timer} of

{_, [_|_], _, 0, 0} -> % eat timer and procreation timer are zero and there is at least

one neighboring prey cell [...]{[_|_], _, 0, _, _} -> % move timer is zero and there is at least one neighboring empty

cell [...]{_, _, _, _, _} -> % no possible actions [...]

end.

Why this produces correct results

Gameboard in charge -> consistentElements can always select a possible actions -> no dead locks, single turns will terminate

Order of execution is not guaranteed ->Non-deterministic behaviour

Difficult cases

Find all possible cases of inconsistencyWhat should be done in conflict situations?

Why the heck is there a dead lock???

??!! *?#~!

http://code.google.com/p/pasture-uu-ht11/