% Author                         : C. Pierquet
% licence                        : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txtf
% Copyright                      : Scrabble, TradeMark by Hasbro or Mattel
% Inspiration                    : Mark Wibrow in https://tex.stackexchange.com/questions/194780/tikz-drawing-a-rectangle-with-spikes-on-borders

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{Scrabble}[2023/01/09 v0.1.3 Scrabble]
% 0.1.3		Refonte des commandes fr/en + gestion mot
% 0.1.2		Clé <EchelleLabels> pour les labels des cases
% 0.1.1		Commandes en/fr
% 0.1		Version initiale

%------Packages utiles
\RequirePackage{tikz}
\RequirePackage{pgf,pgffor}
\RequirePackage{xstring}
\RequirePackage{xparse}
\RequirePackage{simplekv}
\RequirePackage{listofitems}
%librairies tikz
\usetikzlibrary{calc,shapes.geometric}

%score par lettre, codes pays par ISO 639-1
\def\AlphabetMajuscule{ABCDEFGHIJKLMNOPQRSTUVWXYZ}
\def\AlphabetMinuscule{abcdefghijklmnopqrstuvwxyz}
\def\PointsScrabbleFR{1,3,3,2,1,4,2,4,1,8,10,1,1,1,1,3,8,1,1,1,1,4,10,10,10,10}
\readlist*{\PtsScrbFR}{\PointsScrabbleFR}
\def\PointsScrabbleEN{1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}
\readlist*{\PtsScrbEN}{\PointsScrabbleEN}
\def\PointsScrabbleDE{1,3,4,1,1,2,2,2,2,1,6,4,2,3,2,4,10,1,1,1,1,6,3,8,10,3}
\readlist*{\PtsScrbDE}{\PointsScrabbleDE}
\def\PointsScrabbleES{1,3,3,2,1,4,2,4,1,8, ,1,3,1,1,3,5,1,1,1,1,4, ,8,4,10}
\readlist*{\PtsScrbES}{\PointsScrabbleES}

\newcommand\scrabblescorelettre[2][EN]{%avec gestion du Joker par *
	\IfStrEq{#2}{*}%
		{}%
		{%
			\IfSubStr{\AlphabetMajuscule}{#2}%MAJ ou min
				{\StrPosition{\AlphabetMajuscule}{#2}[\pos]}%index MAJ
				{\StrPosition{\AlphabetMinuscule}{#2}[\pos]}%index min
			\IfStrEq{#1}{FR}%
				{\PtsScrbFR[\pos]}{}%scoreFR
			\IfStrEq{#1}{EN}%
				{\PtsScrbEN[\pos]}{}%scoreEN
			\IfStrEq{#1}{DE}%
				{\PtsScrbDE[\pos]}{}%scoreDE
			\IfStrEq{#1}{ES}%
				{\PtsScrbES[\pos]}{}%scoreSP
		}%
}

%commandes localisées des cases
\newcommand\SCRBMCT[1]{%mot x3
	\IfStrEq{#1}{FR}{MOT\\COMPTE\\TRIPLE}{}%
	\IfStrEq{#1}{EN}{TRIPLE\\WORD\\SCORE}{}%
	\IfStrEq{#1}{DE}{\scalebox{2}[2]{3W}}{}%
	\IfStrEq{#1}{ES}{TRIPLE\\TANTO\\DE\\PALABRA}{}%
}
\newcommand\SCRBMCD[1]{%mot x2
	\IfStrEq{#1}{FR}{MOT\\COMPTE\\DOUBLE}{}%
	\IfStrEq{#1}{EN}{DOUBLE\\WORD\\SCORE}{}%
	\IfStrEq{#1}{DE}{\scalebox{2}[2]{2W}}{}%
	\IfStrEq{#1}{ES}{DOBLE\\TANTO\\DE\\PALABRA}{}%
}
\newcommand\SCRBLCT[1]{%lettre x3
	\IfStrEq{#1}{FR}{LETTRE\\COMPTE\\TRIPLE}{}%
	\IfStrEq{#1}{EN}{TRIPLE\\LETTER\\SCORE}{}%
	\IfStrEq{#1}{DE}{\scalebox{2}[2]{3B}}{}%
	\IfStrEq{#1}{ES}{TRIPLE\\TANTO\\DE\\LETRA}{}%
}
\newcommand\SCRBLCD[1]{%lettre x2
	\IfStrEq{#1}{FR}{LETTRE\\COMPTE\\DOUBLE}{}%
	\IfStrEq{#1}{EN}{DOUBLE\\LETTER\\SCORE}{}%
	\IfStrEq{#1}{DE}{\scalebox{2}[2]{2B}}{}%
	\IfStrEq{#1}{ES}{DOBLE\\TANTO\\DE\\LETRA}{}%
}

%styles case + jetons
%idées de https://tex.stackexchange.com/questions/194780/tikz-drawing-a-rectangle-with-spikes-on-borders
\tikzset{%fr
	scorelettre/.style={
		font=\sffamily\bfseries\tiny,align=center,scale=\fpeval{\PLSCRBBLEechelleLabel*0.8},draw=none,fill=none
	},
	pics/.cd,
	pointes/.style args={#1#2#3}{
		code={
			\fill [#3,scale=0.5*0.9] (-1,1) {\foreach \i in {0,1,2,3}{%
			[rotate=-\i*90] -- (-#1*#2,1) {\foreach \j in {1,...,#1} {-- ++(#2,#2) -- ++(#2,-#2)} -- (1,1)}}} -- cycle;
		}
	},
	casecentrale/.style={
		code={
			\path pic {pointes={2}{0.15}{red!25}};
			\node [star,fill,star point ratio=2.35] {};%
		}
	},
	jeton/.style={
		code={
			\IfStrEq{#1}{*}{\node[fill=yellow!40,minimum size=0.9 cm,rounded corners=\fpeval{\PLSCRBBLEechelle*0.15} cm,font=\Large\sffamily\bfseries] (JETON) {};}%
				{%
					\node[fill=yellow!40,minimum size=0.9 cm,rounded corners=\fpeval{\PLSCRBBLEechelle*0.15} cm,font=\Large\sffamily\bfseries] (JETON) {\expandafter\MakeUppercase\expandafter{#1}};%
					\node[inner sep=2pt,font=\sffamily\scriptsize,anchor=south east] at (JETON.south east) { \scrabblescorelettre[\PLSCRBBLElangue]{#1} };%
				}%
		}
	},
	placemotH/.style={
		code={
			\StrLen{#1}[\lgmot]%
			\foreach \l in {1,2,...,\lgmot} {\StrChar{#1}{\l}[\ll] \path ({\l-2},-1) pic {jeton={\ll}};}
		}
	},
	placemotV/.style={
		code={
			\StrLen{#1}[\lgmot]%
			\foreach \l in {1,2,...,\lgmot} {\StrChar{#1}{\l}[\ll] \path (-1,-{\l}) pic {jeton={\ll}};}
		}
	},
	motcomptetriplefr/.style={
		code={
			\path pic {pointes={3}{0.15}{red!75}};%
			\ifboolKV[PLSCRABBLETIKZFR]{Labels}%
				{\node [scorelettre] { \SCRBMCT{\PLSCRBBLElangue} };}{}
		}
	},
	motcomptedoublefr/.style={
		code={
			\path pic {pointes={2}{0.15}{red!25}};%
			\ifboolKV[PLSCRABBLETIKZFR]{Labels}%
				{\node [scorelettre] { \SCRBMCD{\PLSCRBBLElangue} };}
				{}%
		}
	},
	lettrecomptetriplefr/.style={
		code={
			\path pic {pointes={3}{0.15}{blue!50!cyan!75}};%
			\ifboolKV[PLSCRABBLETIKZFR]{Labels}%
				{\node [scorelettre] { \SCRBLCT{\PLSCRBBLElangue} };}{}
		}
	},
	lettrecomptedoublefr/.style={
		code={
			\path pic {pointes={2}{0.15}{blue!50!cyan!50}};%
			\ifboolKV[PLSCRABBLETIKZFR]{Labels}%
				{\node [scorelettre] { \SCRBLCD{\PLSCRBBLElangue} };}{}
		}
	},
	motcomptetripleen/.style={
		code={
			\path pic {pointes={3}{0.15}{red!75}};%
			\ifboolKV[PLSCRABBLETIKZEN]{Labels}%
				{\node [scorelettre] { \SCRBMCT{\PLSCRBBLElangue} };}{}
		}
	},
	motcomptedoubleen/.style={
		code={
			\path pic {pointes={2}{0.15}{red!25}};%
			\ifboolKV[PLSCRABBLETIKZEN]{Labels}%
				{\node [scorelettre] { \SCRBMCD{\PLSCRBBLElangue} };}
				{}%
		}
	},
	lettrecomptetripleen/.style={
		code={
			\path pic {pointes={3}{0.15}{blue!50!cyan!75}};%
			\ifboolKV[PLSCRABBLETIKZEN]{Labels}%
				{\node [scorelettre] { \SCRBLCT{\PLSCRBBLElangue} };}{}
		}
	},
	lettrecomptedoubleen/.style={
		code={
			\path pic {pointes={2}{0.15}{blue!50!cyan!50}};%
			\ifboolKV[PLSCRABBLETIKZEN]{Labels}%
				{\node [scorelettre] { \SCRBLCD{\PLSCRBBLElangue} };}{}
		}
	}
}

%commande pour placer un mot fr/en
\newcommand\ScrabblePlaceMot[3][H]{%
	\IfStrEq{#1}{H}%
		{\path (#3) pic {placemotH={#2}};}
		{\path (#3) pic {placemotV={#2}};}
}
\newcommand\ScrabblePutWord[3][H]{%
	\IfStrEq{#1}{H}%
		{\path (#3) pic {placemotH={#2}};}
		{\path (#3) pic {placemotV={#2}};}
}

%clés en/fr
\defKV[PLSCRABBLETIKZFR]{%
	Echelle=\xdef\PLSCRBBLEechelle{#1},%
	EchelleLabels=\xdef\PLSCRBBLEechelleLabel{#1}
}
\defKV[PLSCRABBLETIKZEN]{%
	Scale=\xdef\PLSCRBBLEechelle{#1},%
	ScaleLabels=\xdef\PLSCRBBLEechelleLabel{#1}
}
\setKVdefault[PLSCRABBLETIKZFR]{%
	Echelle=1,%
	EchelleLabels=1,%
	Cadre=true,%
	Labels=true,%
	Aide=false
}
\setKVdefault[PLSCRABBLETIKZEN]{%
	Scale=1,%
	ScaleLabels=1,%
	Border=true,%
	Labels=true,%
	Help=false
}

%commandes génériques autonomes pour plateau
\NewDocumentCommand\PlateauScrabble{ D<>{FR} O{} }{%1=lng,2=options
	\xdef\PLSCRBBLElangue{#1}%
	\useKVdefault[PLSCRABBLETIKZFR]%
	\setKV[PLSCRABBLETIKZFR]{#2}%
	\begin{tikzpicture}[scale=\PLSCRBBLEechelle,every node/.style={scale=\PLSCRBBLEechelle}]%
		%plateau
		\fill[gray!25] (-.5,-.5) rectangle ++(15,15);%
		\draw[white,line width=\fpeval{\PLSCRBBLEechelle*0.1} cm, shift={(-.5,-.5)}] grid (15,15);%
		\foreach \scrcase in{(\x,\y),(\y,14-\x),(14-\x,14-\y),(14-\y,\x)}{%
			\foreach \x/\y in {0/0,7/0}{\path \scrcase pic {motcomptetriplefr};}
			\foreach \x/\y in {1/1,2/2,3/3,4/4}{\path \scrcase pic {motcomptedoublefr};}
			\foreach \x/\y in {1/5,5/1,5/5}{\path \scrcase pic {lettrecomptetriplefr};}
			\foreach \x/\y in {3/0,0/3,2/6,6/2,6/6}{\path \scrcase pic {lettrecomptedoublefr};}
		}%
		\path (7,7) pic {casecentrale};%
		\ifboolKV[PLSCRABBLETIKZFR]{Cadre}{\draw[line width=\fpeval{\PLSCRBBLEechelle*0.075} cm] (-0.6,-0.6) rectangle (14.6,14.6) ;}{}
	\end{tikzpicture}%
}
\NewDocumentCommand\ScrabbleBoard{ D<>{EN} O{} }{%1=lng,2=options
	\xdef\PLSCRBBLElangue{#1}%
	\useKVdefault[PLSCRABBLETIKZEN]%
	\setKV[PLSCRABBLETIKZEN]{#2}%
	\begin{tikzpicture}[scale=\PLSCRBBLEechelle,every node/.style={scale=\PLSCRBBLEechelle}]%
		%plateau
		\fill[gray!25] (-.5,-.5) rectangle ++(15,15);%
		\draw[white,line width=\fpeval{\PLSCRBBLEechelle*0.1} cm, shift={(-.5,-.5)}] grid (15,15);%
		\foreach \scrcase in{(\x,\y),(\y,14-\x),(14-\x,14-\y),(14-\y,\x)}{%
			\foreach \x/\y in {0/0,7/0}{\path \scrcase pic {motcomptetripleen};}
			\foreach \x/\y in {1/1,2/2,3/3,4/4}{\path \scrcase pic {motcomptedoubleen};}
			\foreach \x/\y in {1/5,5/1,5/5}{\path \scrcase pic {lettrecomptetripleen};}
			\foreach \x/\y in {3/0,0/3,2/6,6/2,6/6}{\path \scrcase pic {lettrecomptedoubleen};}
		}%
		\path (7,7) pic {casecentrale};%
		\ifboolKV[PLSCRABBLETIKZEN]{Border}{\draw[line width=\fpeval{\PLSCRBBLEechelle*0.075} cm] (-0.6,-0.6) rectangle (14.6,14.6) ;}{}%
	\end{tikzpicture}%
}

%environnement FR pour plateau, pour placer des mots
\NewDocumentEnvironment{EnvScrabbleFR}{ D<>{FR} O{} }%1=LANG,2=options
{%
	\xdef\PLSCRBBLElangue{#1}%
	\useKVdefault[PLSCRABBLETIKZFR]%
	\setKV[PLSCRABBLETIKZFR]{#2}%
	\begin{tikzpicture}[scale=\PLSCRBBLEechelle,every node/.style={scale=\PLSCRBBLEechelle}]%
		%plateau
		\fill[gray!25] (-.5,-.5) rectangle ++(15,15);%
		\draw[white,line width=\fpeval{\PLSCRBBLEechelle*0.1} cm, shift={(-.5,-.5)}] grid (15,15);%
		\foreach \scrcase in{(\x,\y),(\y,14-\x),(14-\x,14-\y),(14-\y,\x)}{%cases spéciales
			\foreach \x/\y in {0/0,7/0} {\path \scrcase pic {motcomptetriplefr};}
			\foreach \x/\y in {1/1,2/2,3/3,4/4} {\path \scrcase pic {motcomptedoublefr};}
			\foreach \x/\y in {1/5,5/1,5/5} {\path \scrcase pic {lettrecomptetriplefr};}
			\foreach \x/\y in {3/0,0/3,2/6,6/2,6/6} {\path \scrcase pic {lettrecomptedoublefr};}
		}%
		\path (7,7) pic {casecentrale};%
		\ifboolKV[PLSCRABBLETIKZFR]{Cadre}%
			{\draw[line width=\fpeval{\PLSCRBBLEechelle*0.075} cm] (-0.6,-0.6) rectangle (14.6,14.6) ;}%
			{}%
		\ifboolKV[PLSCRABBLETIKZFR]{Aide}%
			{%
				\foreach \nb in {1,2,...,15}
					{\draw[gray] ({\nb-1},-0.65) node[below,font=\sffamily] {\nb};\draw[gray] (-0.65,{\nb-1}) node[left,font=\sffamily] {\nb};}%
			}%
			{}%
}%
{%
	\end{tikzpicture}%
}

%environnement EN pour plateau, pour placer des mots
\NewDocumentEnvironment{EnvScrabble}{ D<>{EN} O{} }%1=LANG,2=options
{%
	\xdef\PLSCRBBLElangue{#1}%
	\useKVdefault[PLSCRABBLETIKZEN]%
	\setKV[PLSCRABBLETIKZEN]{#2}%
	\begin{tikzpicture}[scale=\PLSCRBBLEechelle,every node/.style={scale=\PLSCRBBLEechelle}]%
		%plateau
		\fill[gray!25] (-.5,-.5) rectangle ++(15,15);%
		\draw[white,line width=\fpeval{\PLSCRBBLEechelle*0.1} cm, shift={(-.5,-.5)}] grid (15,15);%
		\foreach \scrcase in{(\x,\y),(\y,14-\x),(14-\x,14-\y),(14-\y,\x)}{%cases spéciales
			\foreach \x/\y in {0/0,7/0} {\path \scrcase pic {motcomptetripleen};}
			\foreach \x/\y in {1/1,2/2,3/3,4/4} {\path \scrcase pic {motcomptedoubleen};}
			\foreach \x/\y in {1/5,5/1,5/5} {\path \scrcase pic {lettrecomptetripleen};}
			\foreach \x/\y in {3/0,0/3,2/6,6/2,6/6} {\path \scrcase pic {lettrecomptedoubleen};}
		}%
		\path (7,7) pic {casecentrale};%
		\ifboolKV[PLSCRABBLETIKZEN]{Border}%
			{\draw[line width=\fpeval{\PLSCRBBLEechelle*0.075} cm] (-0.6,-0.6) rectangle (14.6,14.6) ;}%
			{}%
		\ifboolKV[PLSCRABBLETIKZEN]{Help}%
			{%
				\foreach \nb in {1,2,...,15}
					{\draw[gray] ({\nb-1},-0.65) node[below,font=\sffamily] {\nb};\draw[gray] (-0.65,{\nb-1}) node[left,font=\sffamily] {\nb};}%
			}%
			{}%
}%
{%
	\end{tikzpicture}%
}

\endinput