14

What is the best way to draw checker board in tikz? I'm interested in what is the best way to draw many circular pieces on board. (the checker background is not important)

Caramdir
  • 89,023
  • 26
  • 255
  • 291
Łukasz Lew
  • 8,543
  • 12
  • 43
  • 42

4 Answers4

18

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

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • You're going to report the Markdown issue on meta, right ;-) – Joseph Wright Aug 17 '10 at 21:05
  • And a similarly appealing syntax for marking kings versus normal checkers :) (Possibly as simple as {K}, where the letter is used to increase the height of the node, since they currently have no content...)
  • – Ben Lerner Mar 08 '12 at 23:53
  • @BenLerner: Done. The K also increased their width and made the height non-configurable. Keys seemed a better way to go. – Andrew Stacey Mar 09 '12 at 08:51