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)
[Tex/LaTex] Checkers board in TikZ.
tikz-pgf
Related Solutions
You can use a \foreach
loop that takes the number of positions in each row as the loop list. For the standard chinese checkers boards, that could look like \foreach \m [count=\count] in {1,...,4,13,12,...,9,10,11,...,13,4,3,...,1}
.
If you systematically name the nodes you create in this loop, you've got a very nice framework for drawing Chinese Checkers setups and moves. I've written a couple of macros and styles for this.
The command \checkerboard[checkerboard labels]
will draw a blank board with the fields labeled <row>-<column>
:
You can then place pieces using
\placepieces[<colour>]{<comma separated list of positions>}
and show moves using
\showmove{<sequential list of positions>}
For example,
\checkerboard
\placepieces[red!75!yellow]{1-1,2-1,2-2,3-1,3-2,3-3,4-1,4-2,5-8,7-7}
\placepieces[blue!75]{17-1,16-2,15-1,15-2,15-3,14-3,14-4,13-6,11-6,10-6}
\showmove{2-1,4-3,6-8,8-6}
\showmove{17-1,16-1}
Here's the full code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,decorations}
\begin{document}
\begin{tikzpicture}
\newif\ifcheckerboardlabels
\tikzset{
pieces/.style={
fill,
circle,
minimum size=0.5cm
},
positions/.style={
fill=black!5, draw=gray,
solid,
circle,
minimum size=0.6cm,
inner sep=0pt,
},
checkerboard labels/.is if=checkerboardlabels,
checkerboard labels/.default=true,
move/.style={
-latex,
densely dashed,
very thick,
bend right=45
}
}
\newcommand{\checkerboard}[1][]{
\begin{scope}[#1]
\foreach \m [count=\count] in {1,...,4,13,12,...,9,10,11,...,13,4,3,...,1}{
\foreach \n in {1,...,\m}
\node at (\n-\m/2,\count) [
positions,
name=pos-\count-\n,
outer sep=0.5cm,
label=center:{\ifcheckerboardlabels \count-\n\fi}] {};
}
\begin{pgfonlayer}{background}
\draw [gray,thick,rounded corners=0.4cm,fill=yellow!30] (pos-1-1.240) -- (pos-5-5.240) -- (pos-5-1.240) --
(pos-5-1.180) -- (pos-9-1.180) -- (pos-13-1.180) --
(pos-13-1.120) -- (pos-13-5.120) -- (pos-17-1.120) --
(pos-17-1.60) -- (pos-13-9.60) -- (pos-13-13.60) --
(pos-13-13.0) -- (pos-9-9.0) -- (pos-5-13.0) --
(pos-5-13.300) -- (pos-5-9.300) -- (pos-1-1.300) -- cycle;
\end{pgfonlayer}
\end{scope}
}
\newcommand{\placepieces}[2][]{
\begin{scope}[#1]
\foreach \checker in {#2} {
\node [pieces,#1] at (pos-\checker) {};
}
\end{scope}
}
\newcommand{\showmove}[2][]{
\begin{scope}[#1]
\foreach \position [remember = \position as \previousposition,count=\count] in {#2}{
\ifnum \count=1
\node at (pos-\position.center) [positions,black,ultra thick,fill=none] {};
\else
\draw [move] (pos-\previousposition.center) to (pos-\position.center);
\fi
}
;
\end{scope}
}
\checkerboard
\placepieces[red!75!yellow]{1-1,2-1,2-2,3-1,3-2,3-3,4-1,4-2,5-8,7-7}
\placepieces[blue!75]{17-1,16-2,15-1,15-2,15-3,14-3,14-4,13-6,11-6,10-6}
\showmove{2-1,4-3,6-8,8-6}
\showmove{17-1,16-1}
\end{tikzpicture}
\end{document}
Some ideas, lots of hardcoded magic numbers (found by trial and error):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc, shadows, shadows.blur}
\newcommand\addcurlyshadow[2][]{
% #1: Optional aditional tikz options
% #2: Name of the node to "decorate"
\begin{pgfonlayer}{background}
\path[
rounded corners=1pt,
blur shadow={shadow xshift=0pt,
shadow yshift=-0.3pt,
shadow blur steps=6,
shadow blur radius=.6pt}, #1]
($(#2.north west)+( 0.3pt,0)$) --
($(#2.south west)+( 0.3pt,0)$) --
($(#2.south east)+(-0.3pt,0)$) --
($(#2.north east)+(-0.3pt,0)$) --
cycle;
\path[rounded corners,
blur shadow={shadow xshift=0pt,
shadow yshift=0pt,
shadow blur steps=8,
shadow blur radius=2pt}, #1]
($(#2.north west)+(-1pt,-2pt)$) --
($(#2.south west)+(-1pt, 2pt)$) --
($(#2.south east)+( 1pt, 2pt)$) --
($(#2.north east)+( 1pt,-2pt)$) --
cycle;
\end{pgfonlayer}
}
\begin{document}
\begin{tikzpicture}
\begin{pgfonlayer}{background}
\fill[black!20] (-3,-1) rectangle (3,1);
\end{pgfonlayer}
\node[fill=white, rectangle, minimum width=3cm, minimum height=1cm]
(example) {Test};
\addcurlyshadow{example}
\end{tikzpicture}
\end{document}
Result:
Best Answer
I suggest to use a
matrix of nodes
from the tikz matrix library.A small example using the style devloped in the other topic:
Output: