% \iffalse
% <*chit>
% --------------------------------------------------------------------
% \fi
% \subsubsection{Table of chits}
%
%    \begin{macrocode}
\tikzset{
  chit/cell background/.style={fill=black},
  blank chit/.style={/chit/frame={draw=none,fill=none}},
}
%    \end{macrocode}
% 
% These macros are used when we set tables of chits.  This allows us
% to define blank spaces in the table by giving the element
% \texttt{blank~chit}.
% 
%    \begin{macrocode}
\def\chit@blank{blank chit}
\def\chit@cellbg(#1,#2)#3{%
  \draw[chit/cell background](#1-#3/2,#2-#3/2) rectangle++(#3,#3);
}
%    \end{macrocode}
% 
% \begin{Macro}{\ifchits@reset}
%
%   This `if' controls whether to reset the coordinates to the origin
%   when \cs{chits} is called.  If true, then reset for a new table.
%   
%    \begin{macrocode}
\newif\ifchits@reset\chits@resettrue
%    \end{macrocode}
% \end{Macro}
% 
% \begin{Macro}{\chits,\@chits,\chit@sng@cellupdate}
%    \begin{macrocode}
\def\chit@sng@cellupdate(#1,#2)#3#4{%
  \chit@dbg{2}{Current `#1' vs `#4'*(`#3'+1)}
  \pgfmathparse{ifthenelse(#1>=#4*(#3-1),#2-#4,#2)}%
  \xdef#2{\pgfmathresult}%
  \pgfmathparse{ifthenelse(#1>=#4*(#3-1),0,#1+#4)}%
  \xdef#1{\pgfmathresult}%
}
%    \end{macrocode}
%
% The stared version (\cs{chits*}) of this macro continues the
% previously set chit table.
% 
%    \begin{macrocode}
\def\chits{%
  \@ifstar{\chits@resetfalse\@chits}{\chits@resettrue\@chits}}
%    \end{macrocode}
% 
%    \begin{macrocode}
\def\@chits#1#2#3{
  \ifchits@reset
    \def\r{0}%
    \def\c{0}%
  \fi
  \chit@dbg{1}{Chits to make: #1}%
  \foreach[count=\ti from 0] \t/\x in #1{%
    \chit@dbg{2}{Turn `\t' with option `\x'}
    \ifx\t\empty\else%
      \foreach \u/\m in \t{%
        \ifx\u\empty\else%
          \chit@dbg{2}{Next chit `\u' with possible multiplicity `\m'}%
          \ifx\m\@empty\def\m{1}\fi%
          \ifx\u\m\def\m{1}\fi%
          \chit@dbg{2}{Next chit `\u' multiplicity `\m'}%
          \foreach \n in {1,...,\m}{%
            \ifx\u\chit@blank%
              \chit@dbg{3}{Ignoring blank chit:\u}%
            \else%
              \chit@cellbg(\c,\r){#3}%
              \chit[\u=\ti](\c,\r)%
              \chit@sng@cellupdate(\c,\r){#2}{#3}%
            \fi%
          }%
        \fi%
      }%
    \fi%
  }%
  \@ifnextchar;{\@gobble}{}}
%    \end{macrocode}
% \end{Macro}
%
% \begin{Macro}{\doublechits,
%   \@doublechits,
%   \chit@dbl@cellupdate,
%   \chit@dbl@flip}
% \begin{enumerate}
% \item coordinates
% \item coordinates
% \item cell-size
% \end{enumerate}
% 
%    \begin{macrocode}
\def\chit@dbl@flip(#1,#2)#3{%
  \pgfmathparse{-#1}%
  \xdef\mc{\pgfmathresult}%
}
%    \end{macrocode}
% 
%   \begin{enumerate}
%   \item coordinates
%   \item coordinates
%   \item Number of columns
%   \item cell-size
%   \end{enumerate}
%
%    \begin{macrocode}
\def\chit@dbl@cellupdate(#1,#2)#3#4{%
  \pgfmathparse{ifthenelse(#1<-#4/2,#2,#4+#2)}%
  \xdef#2{\pgfmathresult}%
  \pgfmathparse{ifthenelse(#1<-#4/2,#4+#1,-(#3-.5)*#4)}%
  \xdef#1{\pgfmathresult}%
}
%    \end{macrocode}
%
%   \begin{enumerate}
%   \item List of list of keys
%   \item Number of columns
%   \item size of each cell
%   \end{enumerate}
% 
%   The stared version (\cs{doublechits*}) of this macro continues the
%   previously set chit table.
% 
%    \begin{macrocode}
\def\doublechits{%
  \@ifstar{\chits@resetfalse\@doublechits}{\chits@resettrue\@doublechits}}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@doublechits#1#2#3{%
  \chit@dbg{1}{Setting double-sided chits: #1}
  \ifchits@reset
    \pgfmathparse{-(#2-.5)*#3}
    \xdef\c{\pgfmathresult}
    \def\r{0}
  \fi
  
  \foreach[count=\ti from 0] \t/\x in #1{
    \ifx\t\empty\else%
      \foreach \u/\m in \t{
        \ifx\u\empty\else
          \ifx\m\@empty\def\m{1}\else%
            \ifx\u\m\def\m{1}\fi\fi
          \chit@dbg{2}{`\u'=`\m' (\c,\r)}
          \foreach \n in {1,...,\m}{%
            \ifx\u\chit@blank
              \chit@dbg{3}{Ignoring blank chit:\u}
            \else
              \chit@cellbg(\c,\r){#3}
              \chit[\u=\ti](\c,\r)
              \chit@dbl@flip(\c,\r){#3}
              \chit@cellbg(\mc,\r){#3}
              \chit[\u\space flipped=\ti,zone turn=\t,zone mult=\n](\mc,\r)
              \chit@dbl@cellupdate(\c,\r){#2}{#3}
            \fi
          }
        \fi
      }
    \fi
  }
  \draw[dashed](0,-3*#3/4)--(0,\r-#3/4);%
  \draw[dashed,<-] (#3/5,-2*#3/3)--(#3/2,-2*#3/3) node[transform shape,anchor=west]{Back};%
  \draw[dashed,<-] (-#3/5,-2*#3/3)--(-#3/2,-2*#3/3) node[transform shape,anchor=east]{Front};%
  \@ifnextchar;{\@gobble}{}}  
%    \end{macrocode}
% \end{Macro}
%    
% \iffalse
% </chit>
% --------------------------------------------------------------------
% \fi
