%%%
% KenKen
%%%
\def\filedateKenken{2025/05/28}%
\def\fileversionKenken{0.1a}%
\message{-- \filedateKenken\space v\fileversionKenken}%
%
\setKVdefault[ClesKK]{Solution=false,Taille=3,Largeur=2em,Nombre={}}%

\newlength\PfCKenKen

\makeatletter
\NewDocumentCommand\KenKen{o m}{%
  \useKVdefault[ClesKK]%
  \setKV[ClesKK]{#1}%
  \setlength{\PfCKenKen}{\useKV[ClesKK]{Largeur}+\tabcolsep}%
  \setsepchar[*]{,*/}\reademptyitems%
  \readlist*\PfC@ListeCasesKK{#2}%
  \ignoreemptyitems%
  \savecomparemode%
  \comparestrict%
  \begin{NiceTabular}{*{\useKV[ClesKK]{Taille}}{m{\useKV[ClesKK]{Largeur}}}}%[color-inside]%
    \CodeBefore
    \xintFor* ##1 in {\xintSeq{2}{\fpeval{\useKV[ClesKK]{Taille}}}}\do{%
      \tikz\draw[gray] (##1-|1) -- (##1-|last);%
      \tikz\draw[gray] (1-|##1) -- (last-|##1);%
    }%
    \Body
    \xintFor* ##1 in {\xintSeq{0}{\fpeval{\useKV[ClesKK]{Taille}-1}}}\do{%
      \xintFor* ##2 in {\xintSeq{1}{\useKV[ClesKK]{Taille}}}\do{%
        \rule{0pt}{\PfCKenKen}%
        \edef\PfC@KKNumCasesTester{\fpeval{\useKV[ClesKK]{Taille}*##1+##2}}%
        \StrCompare{\PfC@ListeCasesKK[\PfC@KKNumCasesTester,1]}{b}[\PfCTestb]%\PfCTestb
        \StrCompare{\PfC@ListeCasesKK[\PfC@KKNumCasesTester,1]}{l}[\PfCTestl]%\PfCTestr
        \StrCompare{\PfC@ListeCasesKK[\PfC@KKNumCasesTester,1]}{lb}[\PfCTestlb]%\PfCTestbr
        \ifemptyKV[ClesKK]{Nombre}{}{\edef\PfC@TestNombre{\fpeval{\PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]-\useKV[ClesKK]{Nombre}}}}%
        \xintifboolexpr{\PfCTestb==0}{%
          \ifboolKV[ClesKK]{Solution}{%
            \Block[borders={bottom},line-width=2pt]{1-1}{%
              \Large\PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]%
            }%
          }{%
            \ifemptyKV[ClesKK]{Nombre}{%
              \Block[borders={bottom},line-width=2pt]{1-1}{}%
            }{%
              \xintifboolexpr{\PfC@TestNombre==0}{%
                \Block[borders={bottom},line-width=2pt,fill=LightGray]{1-1}{%
                  \PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]%
                }%
              }{%
                \Block[borders={bottom},line-width=2pt]{1-1}{}%
              }%
            }%
          }%
        }{%
          \xintifboolexpr{\PfCTestl==0}{%
            \ifboolKV[ClesKK]{Solution}{%
              \Block[borders={left},line-width=2pt]{1-1}{\Large\PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]}
            }{%
              \ifemptyKV[ClesKK]{Nombre}{%
                \Block[borders={left},line-width=2pt]{1-1}{}%
              }{%
                \xintifboolexpr{\PfC@TestNombre==0}{%
                  \Block[borders={left},line-width=2pt,fill=LightGray]{1-1}{%
                    \PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]%
                  }%
                }{%
                  \Block[borders={left},line-width=2pt]{1-1}{}%
                }%
              }%
            }%
          }{%
            \xintifboolexpr{\PfCTestlb==0}{%
              \ifboolKV[ClesKK]{Solution}{%
                \Block[borders={bottom,left},line-width=2pt]{1-1}{\Large\PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]}
              }{%
                \ifemptyKV[ClesKK]{Nombre}{%
                  \Block[borders={bottom,left},line-width=2pt]{1-1}{}%
                }{%
                  \xintifboolexpr{\PfC@TestNombre==0}{%
                    \Block[borders={bottom,left},line-width=2pt,fill=LightGray]{1-1}{%
                      \PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]%
                    }%
                  }{%
                    \Block[borders={bottom,left},line-width=2pt]{1-1}{}%
                  }%
                }%
              }%
            }{%
              \ifboolKV[ClesKK]{Solution}{%
                \Block{1-1}{\Large\PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]}
              }{%
                \ifemptyKV[ClesKK]{Nombre}{\Block{1-1}{}%
                }{%
                  \xintifboolexpr{\PfC@TestNombre==0}{%
                    \Block[fill=LightGray]{1-1}{%
                      \PfC@ListeCasesKK[\PfC@KKNumCasesTester,3]%
                    }%
                  }{%
                    \Block{1-1}{}%
                  }%
                }%
              }%
            }%
          }%
        }%
        \xintifForLast{\\}{&}%
      }%
    }%
    \CodeAfter%
    \tikz\draw[line width=2pt] (1-|1) rectangle (last-|last);%
    \xintFor* ##1 in {\xintSeq{1}{\fpeval{\useKV[ClesKK]{Taille}}}}\do{%
      \xintFor* ##2 in {\xintSeq{1}{\useKV[ClesKK]{Taille}}}\do{%
        \tikz\node[anchor=west,inner sep=0pt,xshift=1mm,yshift=-0.2\PfCKenKen] at (##1 -|##2) {\scriptsize$\PfC@ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*(##1-1)+##2},2]$};%
      }%
    }%
  \end{NiceTabular}
  \restorecomparemode%
}%
\makeatother