% pas-chess V0.1
% date: 2022-03-14

% Informations générales
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pas-chess}[2022/03/14]

\@ifpackageloaded{tikz}{}{\RequirePackage{tikz}}
\@ifpackageloaded{xstring}{}{\RequirePackage{xstring}}

% dimension des cases par défaut

\newlength\chesswidth
\setlength{\chesswidth}{1.5cm}

% couleurs par défaut 

\newcommand{\bgcolorboardwhite}{white}
\newcommand{\bgcolorboardblack}{gray!50}
\newcommand{\borderchessboard}{black}

% définition des images des pièces

\newcommand{\reineN}{%
	\includegraphics[width=\chesswidth]{reine.png}
}
\newcommand{\roiN}{%
	\includegraphics[width=\chesswidth]{roi.png}
}
\newcommand{\fouN}{%
	\includegraphics[width=\chesswidth]{fou.png}
}
\newcommand{\tourN}{%
	\includegraphics[width=\chesswidth]{tour.png}
}
\newcommand{\cavN}{%
	\includegraphics[width=\chesswidth]{cavalier.png}
}
\newcommand{\pionN}{%
	\includegraphics[width=\chesswidth]{pion.png}
}
\newcommand{\reineB}{%
	\includegraphics[width=\chesswidth,decodearray={1 0 1 0 1 0}]{reine.png}
}
\newcommand{\cavB}{%
	\includegraphics[width=\chesswidth,decodearray={1 0 1 0 1 0}]{cavalier.png}
}
\newcommand{\pionB}{%
	\includegraphics[width=\chesswidth,decodearray={1 0 1 0 1 0}]{pion.png}
}
\newcommand{\roiB}{%
	\includegraphics[width=\chesswidth,decodearray={1 0 1 0 1 0}]{roi.png}
}
\newcommand{\tourB}{%
	\includegraphics[width=\chesswidth,decodearray={1 0 1 0 1 0}]{tour.png}
}
\newcommand{\fouB}{%
	\includegraphics[width=\chesswidth,decodearray={1 0 1 0 1 0}]{fou.png}
}

%%%%%%%%%%%%%%%%%%%%
% \dessin du tableau
%%%%%%%%%%%%%%%%%%%%

\newcommand{\drawchessboard}{%
	\fill[\bgcolorboardwhite] (0,0) rectangle ({8*\chesswidth},{8*\chesswidth});

	\foreach\x in {0,2,4,6} {
		\foreach \y in {0,2,4,6} {
			\fill[\bgcolorboardblack] ({\x*\chesswidth},{\y*\chesswidth}) rectangle +(\chesswidth,\chesswidth);
			\fill[\bgcolorboardblack] ({(\x+1)*\chesswidth},{(\y+1)*\chesswidth}) rectangle +(\chesswidth,\chesswidth);
		}
	}
	
	\draw[\borderchessboard] (0,0) grid[step=\chesswidth] ({8*\chesswidth},{8*\chesswidth});
	
	\foreach \l/\x/\c in {a/0.5/1,b/1.5/2,c/2.5/3,d/3.5/4,e/4.5/5,f/5.5/6,g/6.5/7,h/7.5/8}
	{
		\node[below,text=\borderchessboard] at ({\x*\chesswidth},0) {$\l$};
		\node[left,text=\borderchessboard] at (0,{\x*\chesswidth}) {$\c$};
	}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%
% positionnement des pièces
%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\putpiece}[3]{%
	\IfEq{#1}{a}{\node at ({0.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
	\IfEq{#1}{b}{\node at ({1.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
	\IfEq{#1}{c}{\node at ({2.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
	\IfEq{#1}{d}{\node at ({3.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
	\IfEq{#1}{e}{\node at ({4.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
	\IfEq{#1}{f}{\node at ({5.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
	\IfEq{#1}{g}{\node at ({6.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
	\IfEq{#1}{h}{\node at ({7.5*\chesswidth},{(#2-0.5)*\chesswidth}) {#3};}{}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%
% début du jeu
%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\start}{%
\putpiece{a}{1}{\tourB{}}
\putpiece{h}{1}{\tourB{}}
\putpiece{b}{1}{\cavB{}}
\putpiece{g}{1}{\cavB{}}
\putpiece{c}{1}{\fouB{}}
\putpiece{f}{1}{\fouB{}}
\putpiece{d}{1}{\reineB{}}
\putpiece{e}{1}{\roiB{}}
\foreach \p in {a,b,c,d,e,f,g,h} {
\putpiece{\p}{2}{\pionB{}}
}
%
\putpiece{a}{8}{\tourN{}}
\putpiece{h}{8}{\tourN{}}
\putpiece{b}{8}{\cavN{}}
\putpiece{g}{8}{\cavN{}}
\putpiece{c}{8}{\fouN{}}
\putpiece{f}{8}{\fouN{}}
\putpiece{d}{8}{\reineN{}}
\putpiece{e}{8}{\roiN{}}
\foreach \p in {a,b,c,d,e,f,g,h} {
\putpiece{\p}{7}{\pionN{}}
}
}