Checkers board in TikZ.

The difficulty with Caramdir's approach is that it is too regular so it would be hard, for example, to display the layout of the board in an actual game. Stefan's is better at that, but doesn't have the grid. However, a grid is very regular so Caramdir's approach can be adapted to that.

So here's my version which is an amalgamation of the two. First, the result:

checker board

Some things to note:

  1. These are meant to be checkers pieces, after all!
  2. Using the "between origins" option in the matrix spacing means that the checkers (aka nodes) are placed very precisely on the squares.
  3. As in Stefan's answer, the syntax for actually placing the checkers on their squares is very intuitive.
  4. The only thing left is a simple syntax for changing the colour of the pieces!
\documentclass{article} 
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes.geometric}

\begin{document}
\tikzstyle{checker} = [cylinder, minimum width=.8cm, 
 shape border rotate=90,cylinder end fill=gray!50!white,
 cylinder body fill=gray, cylinder uses custom fill]
\begin{tikzpicture}
\draw[ultra thick] (0,0) rectangle (8,-8);
    \foreach \row in {0,1, ..., 7} {
        \foreach \column in {0, ..., 3} {
    \fill ({2*\column + mod(\row,2)}, -\row) rectangle +(1,-1);
        }
    }
\matrix (m) at (0,0) [matrix of nodes,nodes=checker,
  anchor=north west,column sep={1cm,between origins},
  row sep={1cm,between origins}] {
 {} & {} & {} &    & {} &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
    & {} & {} &    &    &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
 {} &    &    & {} & {} &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
    &    & {} &    &    &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
};
\end{tikzpicture}
\end{document}

Update 2012-03-09: As this has just returned to the front page, I decided to see if I could fix the colours and the "kings". Thanks to Ryan Reich's trace-pgfkeys package, I figured out a way to set the colours and size.

Here's the new code; I've slightly cleaned up the original and added the colour checking. The complicated bit (which Ryan's code was invaluable for) was figuring out how to set the colour from any ambient colour that might have been set.

\documentclass{article} 
%\url{https://tex.stackexchange.com/a/1911/86}
\usepackage{tikz}
%\usepackage{trace-pgfkeys}
\usetikzlibrary{matrix,shapes.geometric}

\makeatletter
\colorlet{checkertint}{gray!70!white}
\tikzset{
  checker/.style={
    draw,
    cylinder,
    minimum width=.8cm, 
    shape border rotate=90,
    set checker colour=#1,
    cylinder uses custom fill,
  },
  king/.style={
    minimum height=.8cm,
  },
  checker default colour/.initial=gray!50!white,
  set checker colour/.code={%
    \tikz@addoption{%
      \def\ch@color{#1}%
      \def\@tempa{\pgfkeysnovalue}%
      \ifx\ch@color\@tempa
       \ifx\tikz@fillcolor\pgfutil@empty
        \ifx\tikz@strokecolor\pgfutil@empty
         \ifx\tikz@textcolor\pgfutil@empty
          \pgfkeysgetvalue{/tikz/checker default colour}{\ch@color}%
         \else
          \let\ch@color\tikz@textcolor
         \fi
        \else
         \let\ch@color\tikz@strokecolor
        \fi
       \else
        \let\ch@color\tikz@fillcolor
       \fi
      \fi
      \pgfsetstrokecolor{\ch@color!50!checkertint}%
      \pgfkeys{
       /tikz/cylinder end fill=\ch@color!80!checkertint,
       /tikz/cylinder body fill=\ch@color!50!checkertint
      }
    }
  }
}
\makeatother
\begin{document}

\begin{tikzpicture}
\draw[ultra thick] (0,0) rectangle (8,-8);
    \foreach \row in {0,1, ..., 7} {
        \foreach \column in {0, ..., 3} {
    \fill ({2*\column + mod(\row,2)}, -\row) rectangle +(1,-1);
        }
    }
\matrix (m) at (0,0) [
  matrix of nodes,
  checker default colour=white,
  nodes=checker,
  anchor=north west,
  column sep={1cm,between origins},
  row sep={1cm,between origins}
] {
 |[black]| {} & {} & {} &    & {} &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
    &|[black,king]| {} & {} &    &    &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
|[king]| {} &    &    & {} & {} &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
    &    & {} &    &    &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
};
\end{tikzpicture}
\end{document}

Here's a new image.

Checkers with colours and kings


I suggest to use a matrix of nodes from the tikz matrix library.

A small example using the style devloped in the other topic:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\tikzstyle{ball} = [circle, shading=ball,
    ball color=black!80!white, minimum size=1cm]
\begin{tikzpicture}
\matrix (m) [matrix of nodes,nodes=ball] {
 {} &    & {} &    & {} &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
 {} &    & {} &    & {} &    & {} & \\};
\end{tikzpicture}
\end{document}

Output:

alt text


TikZ usually has more than one way to produce a result. Stefan's solution is perfectly fine, but here is another way: TikZ has some programming constructs and many math functions built in that can be used to quickly generate all kinds of diagrams:

\begin{tikzpicture}
    \foreach \row in {0, 1, 2} {
        \foreach \column in {0, 1, ..., 3} {
            \fill[shading=ball, ball color=black!80]
                ({2*\column+mod(\row,2)}, -\row) circle (0.5cm);
        }
    }
\end{tikzpicture}

This produces exactly the same picture as Stefan's solution does (well, except for a slightly changed bounding box because of the matrix padding). The advantage is that you can easily change the size by changing a few numbers and that you can put anything you like inside the foreach loops.

Tags:

Tikz Pgf