0
\documentclass{article} 
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,...,8} 
   \foreach \y in {1,...,8} 
     if (\pgfmathparse{mod(\x+\y,2)==0}) then
       \draw [fill=gray!15] (\x,\y) rectangle (\x+1,\y+1);

\draw [line width=.5mm] (1,1) -- (9,1) -- (9,9) -- (1,9) -- (1,1);

\foreach \x in {1,...,8}
  \foreach \y in {1,...,2}
     \draw [fill=red!50](\x+0.5,\y+0.5) circle (0.4cm);

\foreach \x in {1,...,8}
  \foreach \y in {7,...,8}
     \draw [fill=blue!50](\x+0.5,\y+0.5) circle (0.4cm);
\end{tikzpicture}
\end{document}

3 Answers3

1

I'd rather do the following:

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    \foreach \x in {1,...,8} {
        \foreach \y [evaluate=\y as \z using {int(mod(\x+\y,2))}] in {1,...,8} {
            \ifnum\z=0\relax
                \draw[fill=gray!15] (\x,\y) rectangle (\x+1,\y+1);
            \fi
        }
    }
\draw[line width=.5mm] (1,1) rectangle (9,9);

\foreach \x in {1,...,8}
    \foreach \y in {1,2}
        \draw[fill=red!50] (\x+0.5,\y+0.5) circle[radius=0.4];

\foreach \x in {1,...,8}
    \foreach \y in {7,8}
        \draw[fill=blue!50] (\x+0.5,\y+0.5) circle[radius=0.4];
\end{tikzpicture}

\end{document}

But actually, you don't really need any if-else statement:

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    \foreach \x in {1,3,...,7} 
        \foreach \y in {1,...,8} 
            \draw[fill=gray!15] ({\x+mod(\x+\y,2)},\y) rectangle ++(1,1);
\draw[line width=.5mm] (1,1) rectangle (9,9);

\foreach \x in {1,...,8}
    \foreach \y in {1,2}
        \draw[fill=red!50] (\x+0.5,\y+0.5) circle[radius=0.4];

\foreach \x in {1,...,8}
    \foreach \y in {7,8}
        \draw[fill=blue!50] (\x+0.5,\y+0.5) circle[radius=0.4];
\end{tikzpicture}

\end{document}

enter image description here

1

If you want an implementation following your template you could use \tikzmath as in How to skip a value in a \foreach in TikZ?

\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[radius=.4]
\tikzmath{
  for \x in {1, ..., 8}{
    for \y in {1, ..., 8} {
      if mod(\x+\y,2)==0 then {
        { \draw [fill=gray!15] (\x, \y) rectangle +(1, 1); };
      };
    };
  };
}

\draw [line width=.5mm] (1,1) rectangle (9,9);

\foreach \x in {1, ..., 8} \foreach \y in {1, 2}{ \draw [fill=red!50] (\x+0.5, \y+0.5) circle[]; \draw [fill=blue!50] (\x+0.5, 9-\y+0.5) circle[]; } \end{tikzpicture} \end{document}

But instead of going throug all \x we can just skip every second right from the start:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[radius=.4]
\foreach \x in {1, ..., 4}
  \foreach \y in {1, ..., 8}
    \draw[shift=(right:iseven \y), fill=gray!15] (2*\x-1, \y) rectangle +(1, 1);

\draw [line width=.5mm] (1,1) rectangle (9,9);

\foreach \x in {1, ..., 8} \foreach \y in {1, 2}{ \draw [fill=red!50] (\x+0.5, \y+0.5) circle[]; \draw [fill=blue!50] (\x+0.5, 9-\y+0.5) circle[]; } \end{tikzpicture} \end{document}

I've also taken the liberty to use radius = .4 instead of .4cm to not mix the xyz and the canvas coordinate system.

Qrrbrbirlbel
  • 119,821
1

With {NiceArray} of nicematrix (you need several compilations).

\documentclass{article} 
\usepackage{nicematrix,tikz}

\begin{document}

\NewDocumentCommand{\Blue}{} {\tikz [baseline] \draw [fill=blue!50] circle (0.4cm) ; }

\NewDocumentCommand{\Red}{} {\tikz [baseline] \draw [fill=red!50] circle (0.4cm) ; }

$\begin{NiceArray}{>{\rule[-16pt]{0pt}{32pt}}cccccccc}[hvlines,rounded-corners] \CodeBefore \chessboardcolors{gray!10}{} \Body \Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue \ \Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue \ \ \ \ \ \Red & \Red & \Red & \Red & \Red & \Red & \Red & \Red \ \Red & \Red & \Red & \Red & \Red & \Red & \Red & \Red \ \end{NiceArray}$

\end{document}

Output of the above code

F. Pantigny
  • 40,250