# Building game trees

Theodore L. Turocy
University of East Anglia

### EC'16 Workshop 24 July 2016

In :
import gambit


One can build up extensive games from scratch and manipulate them. This example shows one way to do that for the simple one-card poker game.

The primitives for doing this remain rather low-level, as I have never settled on a good way to write higher-level ones that apply broadly. It has always seemed to me that different games have different "natural" ways to build them out. It is nevertheless likely that having some more powerful idioms could be useful - suggestions are welcomed.

In :
g = gambit.Game.new_tree()
g.title = "A simple poker example"


Define the players; save them as variables for convenience laster.

In :
alice = g.players.add("Alice")

In :
bob = g.players.add("Bob")


The root node is a chance move, between Ace and King. Chance moves default to uniform randomisation, but I set the probabilities here anyway just for explicitness.

In :
move = g.root.append_move(g.players.chance, 2)

In :
move.actions.label = "A"
move.actions.prob = gambit.Rational(1, 2)
move.actions.label = "K"
move.actions.prob = gambit.Rational(1, 2)


After an Ace, Alice can Raise or Fold.

In :
move = g.root.children.append_move(alice, 2)
move.label = 'a'
move.actions.label = "R"
move.actions.label = "F"


She can also Raise or Fold after the King. Calling append_move with the player, rather than the previously created move, creates a new information set for Alice.

In :
move = g.root.children.append_move(alice, 2)
move.label = 'k'
move.actions.label = "R"
move.actions.label = "F"


After Alice raises with the Ace, Bob can Meet or Pass.

In :
move = g.root.children.children.append_move(bob, 2)
move.label = 'b'
move.actions.label = "M"
move.actions.label = "P"


Likewise after Alice raises with the King. Using the same move here adds the move to the same information set for Bob.

In :
g.root.children.children.append_move(move)

Out:
<Infoset  'b' for player 'Bob' in game 'A simple poker example'>

The game so far, as an .efg file. We see the tree structure is in place; now to deal with payoffs.

In :
g

Out:
EFG 2 R "A simple poker example" { "Alice" "Bob" }
""

c "" 1 "" { "A" 1/2 "K" 1/2 } 0
p "" 1 1 "a" { "R" "F" } 0
p "" 2 1 "b" { "M" "P" } 0
t "" 0
t "" 0
t "" 0
p "" 1 2 "k" { "R" "F" } 0
p "" 2 1 "b" { "M" "P" } 0
t "" 0
t "" 0
t "" 0
In :
alice_big = g.outcomes.add("Alice wins big")
alice_big = 2
alice_big = -2

In :
alice_small = g.outcomes.add("Alice wins")
alice_small = 1
alice_small = -1

In :
bob_small = g.outcomes.add("Bob wins")
bob_small = -1
bob_small = 1

In :
bob_big = g.outcomes.add("Bob wins big")
bob_big = -2
bob_big = 2


Attach the outcomes to the corresponding terminal nodes. Notice how we can re-use the outcomes to denote that different terminal nodes may correspond to the same logical outcome.

In :
g.root.children.children.children.outcome = alice_big
g.root.children.children.children.outcome = alice_small
g.root.children.children.outcome = bob_small

In :
g.root.children.children.children.outcome = bob_big
g.root.children.children.children.outcome = alice_small
g.root.children.children.outcome = bob_small


Here's the game we've just built

In :
g

Out:
EFG 2 R "A simple poker example" { "Alice" "Bob" }
""

c "" 1 "" { "A" 1/2 "K" 1/2 } 0
p "" 1 1 "a" { "R" "F" } 0
p "" 2 1 "b" { "M" "P" } 0
t "" 1 "Alice wins big" { 2, -2 }
t "" 2 "Alice wins" { 1, -1 }
t "" 3 "Bob wins" { -1, 1 }
p "" 1 2 "k" { "R" "F" } 0
p "" 2 1 "b" { "M" "P" } 0
t "" 4 "Bob wins big" { -2, 2 }
t "" 2 "Alice wins" { 1, -1 }
t "" 3 "Bob wins" { -1, 1 }

For comparison, here's the game we started with for the Poker example.

In :
gambit.Game.read_game("poker.efg")

Out:
EFG 2 R "A simple poker example" { "Alice" "Bob" }
""

c "" 1 "" { "A" 1/2 "K" 1/2 } 0
p "" 1 1 "a" { "R" "F" } 0
p "" 2 1 "b" { "M" "P" } 0
t "" 1 "Alice wins big" { 2, -2 }
t "" 2 "Alice wins" { 1, -1 }
t "" 3 "Bob wins" { -1, 1 }
p "" 1 2 "k" { "R" "F" } 0
p "" 2 1 "b" { "M" "P" } 0
t "" 4 "Bob wins big" { -2, 2 }
t "" 2 "Alice wins" { 1, -1 }
t "" 3 "Bob wins" { -1, 1 }

We can confirm they're the same:

In :
g.write() == gambit.Game.read_game("poker.efg").write()

Out:
True