My aim is to program Conway's Game of Life in LaTeX, and make the output into an animated PDF. I intend to use PGF/TikZ for this, especially pgfmath
, but I'm stuck at the moment because I need to assign values to array elements, and I don't know how to do that, if it is even possible.
Here is my first approach, which outputs a single generation:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\def\grid{{{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,1,0,0,0,0},%
{0,0,0,0,0,1,0,0,0},%
{0,0,0,1,1,1,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0}}}
\def\temp{{{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0},%
{0,0,0,0,0,0,0,0,0}}}
\foreach \z in \grid {
\foreach[count=\xi] \x in \z {
\foreach[count=\yi] \y in \x {
\pgfmathtruncatemacro{\i}{\xi - 1}
\pgfmathtruncatemacro{\iminus}{mod(mod(\xi - 2, 9) + 9, 9)}
\pgfmathtruncatemacro{\iplus}{mod(\xi, 9)}
\pgfmathtruncatemacro{\j}{\yi - 1}
\pgfmathtruncatemacro{\jminus}{mod(mod(\yi - 2, 9) + 9, 9)}
\pgfmathtruncatemacro{\jplus}{mod(\yi, 9)}
\pgfmathtruncatemacro{\value}{\grid[\i][\j]}
\pgfmathtruncatemacro{\topleft}{\grid[\iminus][\jminus]}
\pgfmathtruncatemacro{\top}{\grid[\iminus][\j]}
\pgfmathtruncatemacro{\topright}{\grid[\iminus][\jplus]}
\pgfmathtruncatemacro{\left}{\grid[\i][\jminus]}
\pgfmathtruncatemacro{\right}{\grid[\i][\jplus]}
\pgfmathtruncatemacro{\bottomleft}{\grid[\iplus][\jminus]}
\pgfmathtruncatemacro{\bottom}{\grid[\iplus][\j]}
\pgfmathtruncatemacro{\bottomright}{\grid[\iplus][\jplus]}
\pgfmathtruncatemacro{\neighbourcount}{\topleft + \top + \topright + \left + \right + \bottomleft + \bottom + \bottomright}
\pgfmathtruncatemacro{\nextvalue}{(\value == 1 && (\neighbourcount == 2 || \neighbourcount == 3)) || (\value == 0 && \neighbourcount == 3) ? 1 : 0}
\node at (\yi, -\xi) {\value};
%\node at (\yi, -\xi) {\value, \neighbourcount};
\node at ($ (0, -10) + (\yi, -\xi) $) {\nextvalue};
}
}
}
\end{tikzpicture}
\end{document}
I only need help for how to assign values to array elements, because I want to solve other things myself.
Edit #1
Question's original title was Assign value to array element (PGF/TikZ), but I think that this title does not suite the question anymore given the answers below.
Best Answer
I ran your code but it appeared to be very slow, I suspect from all the
\pgfmathtruncatemacro
. But here we can do all calculations with\numexpr
easily. This code is based on theTeX
primitives\ifnum
,\ifcase
and\csname..\endcsname
.I have used
\foreach
loops in the first two code samples as I wanted to stay close to your original framework. In the third code sample I use\xintFor
from package xinttools. As\xintFor
does not create groups, it is easier to use in such contexts.Update: amazed by JLDiaz's animation of the Gosper Gun, I have done it too with
TeX
"rules" in aLaTeX
picture.Update: based on Mark Wibrow's remark in a related question I have added a version of the initial code which only updates changed cells.
Final update: the third code sample (which produces the Gosper Gun below) has also been changed to only update cells when they actually do change. No temporary array of the entire universe.
Improved version which only modifies the modified (sic) cells:
Here is the code used for the Gosper Gun. Uses
\xintFor
rather than\foreach
. So there is no problem with groups now. Also updated to only modify the modified cells (sic).