# Game tree - can my code be improved?

I've produced this game tree:

MWE for the output above:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\tikzset{
% Two node styles for game trees: solid and hollow
solid/.style={circle,draw,inner sep=1,fill=black},
hollow/.style={circle,draw,inner sep=1}
}

\begin{center}
\begin{tikzpicture}[font=\scriptsize]
\newcommand{\payoff}[4][below]{\node[#1]at(#2){$(#3,#4)$};}
\tikzstyle{level 1}=[level distance=15mm,sibling distance=72mm]
\tikzstyle{level 2}=[level distance=15mm,sibling distance=36mm]
\tikzstyle{level 3}=[level distance=15mm,sibling distance=18mm]
\tikzstyle{level 4}=[level distance=15mm,sibling distance=9mm]

\node(0)[solid]{}
child{node(1)[solid]{}
child{node[solid]{}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[left]{C}}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[right]{D}}
edge from parent node[left]{C}}
child{node[solid]{}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[left]{C}}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[right]{D}}
edge from parent node[right]{D}}
edge from parent node[left,xshift=-3]{C}
}
child{node(2)[solid]{}
child{node[solid]{}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[left]{C}}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[right]{D}}
edge from parent node[left]{C}}
child{node[solid]{}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[left]{C}}
child{node[solid]{}
child{node[solid]{} edge from parent node[left]{C}}
child{node[solid]{} edge from parent node[right]{D}}
edge from parent node[right]{D}}
edge from parent node[right]{D}}
edge from parent node[right,xshift=3]{D}
};
\draw[rounded corners=7,dashed]($(1-1)+(-.2,.2)$)rectangle($(2-2)+(.2,-.2)$);
\draw[rounded corners=7,dashed]($(0)+(-.2,.2)$)rectangle($(0)+(.2,-.2)$);
\payoff{1-1-1-1}44
\payoff{1-1-1-2}15
\payoff{1-1-2-1}51
\payoff{1-1-2-2}22
\payoff{1-2-1-1}15
\payoff{1-2-1-2}{-2}6
\payoff{1-2-2-1}22
\payoff{1-2-2-2}{-1}3
\payoff{2-1-1-1}51
\payoff{2-1-1-2}22
\payoff{2-1-2-1}6{-2}
\payoff{2-1-2-2}3{-1}
\payoff{2-2-1-1}22
\payoff{2-2-1-2}{-1}3
\payoff{2-2-2-1}3{-1}
\payoff{2-2-2-2}00
\end{tikzpicture}
\end{center}

\end{document}


I have no trouble producing similar game trees with the techniques demonstrated here, but if possible, I'd like to know if there is anything at all I'm doing inefficiently, which I can improve on.

In particular, are there are easier ways to:

1. Produce multi-level game trees (> 4 levels) without a ridiculously deep series of nested arguments.
2. Automatically handle sibling distance as I add more levels - in this case, I've had to manually make the distance between siblings on previous levels wider every time I added a new level.

Much appreciated!

Here is one way to improve it: switch to forest. Then you are less likely to go insane because of all the nesting. And you can set the edge labels programmatically. You could also set the numbers in the lowest line programmatically if there a formula that determines them. I was unable to guess it.

\documentclass{article}
\usepackage[edges]{forest}
\usepackage[outline]{contour}
\usetikzlibrary{fit,shapes.misc,arrows.meta}
\begin{document}
\begin{forest}
for tree={s sep=0cm,l sep=1.2cm,font=\scriptsize,
where n children=0{edge={thick,-{Circle}}}{circle,fill,inner sep=0pt,minimum size=2mm},
where n=1{edge label={node[midway,above left=0pt,font=\scriptsize]{C}}}{edge
label={node[midway,above right=0pt,font=\scriptsize]{D}}},
edge={thick}
}
[,alias=T
[
[,alias=L
[
[{$(4,4)$}]
[{$(1,5)$}]
]
[
[{$(5,1)$}]
[{$(2,2)$}]
]
]
[
[
[{$(1,5)$}]
[{$(-2,6)$}]
]
[
[{$(2,2)$}]
[{$(-1,3)$}]
]
]
]
[
[
[
[{$(5,1)$}]
[{$(2,2)$}]
]
[
[{$(6,-2)$}]
[{$(3,-1)$}]
]
]
[,alias=R
[
[{$(2,2)$}]
[{$(-1,3)$}]
]
[
[{$(3,-1)$}]
[{$(0,0)$}]
]
]
]
]
\node[draw,dashed,rounded rectangle,fit=(L) (R)]{};
\node[draw,dashed,circle,fit=(T)]{};
\end{forest}
\end{document}


How can add another row to this tree? One option is to arrange the integers in the last row vertically. Among other things, this will allow you to add an explanation to the numbers, I just used mystical number since I have no clue how the numbers derive from the position, but of course the answer may be complicated or that there is no relation, so please replace mystical number by something more appropriate.

\documentclass{article}
\usepackage[edges]{forest}
\usepackage[outline]{contour}
\usetikzlibrary{fit,shapes.misc,arrows.meta,positioning}
\begin{document}
\begin{forest}
for tree={s sep=0.1cm,l sep=1.2cm,font=\scriptsize,
where n children=0{align=center,draw,edge={thick,-{Circle}}}{circle,fill,inner sep=0pt,minimum size=2mm},
where n=1{edge label={node[midway,above left=0pt,font=\scriptsize]{C}}}{edge
label={node[midway,above right=0pt,font=\scriptsize]{D}}},
edge={thick}
}
[,alias=T
[
[,alias=L
[
[{$4$\\ $4$},alias=very left]
[{$1$\\ $5$}]
]
[
[{$5$\\ $1$}]
[{$2$\\ $2$}]
]
]
[
[
[{$1$\\ $5$}]
[{$-2$\\ $6$}]
]
[
[{$2$\\ $2$}]
[{$-1$\\ $3$}]
]
]
]
[
[
[
[{$5$\\ $1$}]
[{$2$\\ $2$}]
]
[
[{$6$\\ $-2$}]
[{$3$\\ $-1$}]
]
]
[,alias=R
[
[{$2$\\ $2$}]
[{$-1$\\ $3$}]
]
[
[{$3$\\ $-1$}]
[{$0$\\ $0$}]
]
]
]
]
\node[draw,dashed,rounded rectangle,fit=(L) (R)]{};
\node[draw,dashed,circle,fit=(T)]{};
\node[left=1mm of very left,align=right,font=\scriptsize]{mystical \# 1\\
mystical \# 2};
\end{forest}
\end{document}


In addition you may consider doing some of the standard tricks like replacing -1 by \overline{1}, as is sometimes done to save horizontal space.

And you may wonder if all this can be replaced by something simpler. All you really need to draw the tree is the number of levels. And the answer is: yes, there is the so-called "Lindenmayer System Drawing Library", which is described in section 55 of the pgfmanual. This allows you to draw self-repeating structures with less effort. However, unless you ask a (separate) question on this, I refrain from spelling this out.

• Thank you very much. Apologies for the delay - I was taking some time to go through the forest documentation, and go through your code in detail, since the package was new to me. – Thev Oct 6 '18 at 6:43

### Edit:

The macro \xtInfosetO is enhanced in the istgame version 2, resulting in a better output. So the resulting image has been changed in this editing.

Here is another way using the istgame package, with which you can simply connect parent-child trees to draw a whole tree.

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\scriptsize]
\xtShowEndPoints
\def\mysibdist{72mm}
\xtdistance{15mm}{\mysibdist}
\istroot(0)
\istb{C}[al] \istb{D}[ar] \endist
\xtInfosetO(0)(0)
\xtdistance{15mm}{\mysibdist/2}
\istroot(a)(0-1)
\istb{C}[al] \istb{D}[ar] \endist
\istroot(b)(0-2)
\istb{C}[al] \istb{D}[ar] \endist
\xtdistance{15mm}{\mysibdist/4}
\istroot(a1)(a-1)
\istb{C}[al] \istb{D}[ar] \endist
\istroot(a2)(a-2)
\istb{C}[al] \istb{D}[ar] \endist
\istroot(b1)(b-1)
\istb{C}[al] \istb{D}[ar] \endist
\istroot(b2)(b-2)
\istb{C}[al] \istb{D}[ar] \endist
\xtInfosetO(a1)(b2)
\xtdistance{15mm}{\mysibdist/8}
\istroot(a11)(a1-1)
\istb{C}[l]{(4,4)} \istb{D}[r]{(1,5)} \endist
\istroot(a12)(a1-2)
\istb{C}[l]{(5,1)} \istb{D}[r]{(2,2)} \endist
\istroot(a21)(a2-1)
\istb{C}[l]{(1,5)} \istb{D}[r]{(-2,6)} \endist
\istroot(a22)(a2-2)
\istb{C}[l]{(2,2)} \istb{D}[r]{(-1,3)} \endist
\istroot(b11)(b1-1)
\istb{C}[l]{(5,1)} \istb{D}[r]{(2,2)} \endist
\istroot(b12)(b1-2)
\istb{C}[l]{(6,-2)} \istb{D}[r]{(3,-1)} \endist
\istroot(b21)(b2-1)
\istb{C}[l]{(2,2)} \istb{D}[r]{(-1,3)} \endist
\istroot(b22)(b2-2)
\istb{C}[l]{(3,-1)} \istb{D}[r]{(0,0)} \endist
\end{istgame}

\end{document}