Description
The code above is a LaTeX document that produces a game tree diagram using the TikZ and Forest packages. The game tree diagram represents the game of tic-tac-toe with the use of a ternary tree.
The Forest package is used to define the styles of the nodes in the tree, such as the content of each node, the label positions, and the edge styles. The make tab style defines a tabular environment for the content of each node. The label position style is used to specify the position of the label for each node. The TTT and TTT* styles define the content and edge styles for the tree, with TTT* being a special style for empty nodes.
The code defines a root node with no content and three children, each representing a possible first move in tic-tac-toe. The children of each of these nodes represent the second move, and so on until the end of the game is reached. The content of each node in the tree represents the state of the game at that point, with "x" and "o" representing the players' moves. The label of each node shows the score for the corresponding player, given as an integer value. The edges represent the possible moves that can be made from one state to another.
The resulting diagram shows all possible game states and their corresponding scores for the two players. It is a helpful visual representation of the possible outcomes of the game.
Keywords
forest, tree, tikz, game, tic-tac-toe, chart, table
Source Code
\documentclass[tikz]{standalone}
\usepackage{forest,calc}
\forestset{
make tab/.style args={#1:#2:#3/#4:#5:#6/#7:#8:#9}{%
content={%
\tabcolsep=.4\tabcolsep
\begin{tabular}{p{\widthof{x}}|p{\widthof{x}}|p{\widthof{x}}}
#1 & #2 & #3\\\hline#4\\\hline#7	
\end{tabular}}},
label position r/.initial=right,
label position b/.initial=below
}
\begin{document}
\begin{forest}
TTT/.style args={#1:#2}{
make tab/.expanded=\forestove{content},
label={\pgfkeysvalueof{/forest/label position #1}:$#2$},
edge={->}
},
TTT*/.style={
make tab=::/::/::,
content/.expand once=%
\expandafter\vphantom\expandafter{\romannumeral-`0\forestov{content}},
draw=none,
append after command={(\tikzlastnode.north) edge (\tikzlastnode.south)},
for descendants={before computing xy={l*=1.8}},
},
th/.style=very thick,
for tree={node options=draw, inner sep=+0pt, parent anchor=south, child anchor=north}
%
[::/::/::, TTT=r:0
[x::/::/::, TTT=r:-1
[x:o:/::/::, TTT=b: 1, th]
[x::/:o:/::, TTT=b:-1]
[x::o/::/::, TTT=b: 0, th]
[x::/::/::o, TTT=b: 0, th]
[x::/::o/::, TTT=b: 1, th]
]
[::/::x/::, TTT=r:0,
[,TTT* % this is just cheating :(
[o::/::x/::, TTT=b:1]
[:o:/::x/::, TTT=b:0]
]
]
[:x:/::/::, TTT=r:-2
[:x:o/::/::, TTT=b:-1, th]
[:x:/:o:/::, TTT=b:-2]
[:x:/::/:o:, TTT=b: 0, th]
[:x:/::/o::, TTT=b:-1, th]
[:x:/o::/::, TTT=b: 0, th]
]
]
\end{forest}
\end{document}