%%%
% KenKen
%%%
\setKVdefault[ClesKK]{Solution=false,Taille=3,Largeur=2em,Aide=false}%
\defKV[ClesKK]{Nombre=\setKV[ClesKK]{Aide}}%

\newlength\PfCKenKen

\NewDocumentCommand\KenKen{o m}{%
  \useKVdefault[ClesKK]%
  \setKV[ClesKK]{#1}%
  \setlength{\PfCKenKen}{\useKV[ClesKK]{Largeur}+\tabcolsep}%
  \setsepchar[*]{,*/}%
  \readlist*\ListeCasesKK{#2}%
  \savecomparemode%
  \comparestrict%
  \begin{NiceTabular}{*{\useKV[ClesKK]{Taille}}{m{\useKV[ClesKK]{Largeur}}}}%
    \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}%
        \StrCompare{\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},1]}{b}[\PfCTestb]%\PfCTestb
        \StrCompare{\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},1]}{l}[\PfCTestl]%\PfCTestr
        \StrCompare{\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},1]}{lb}[\PfCTestlb]%\PfCTestbr
        \xintifboolexpr{\PfCTestb==0}{%
          \Block[borders={bottom},line-width=2pt]{1-1}{%
            \ifboolKV[ClesKK]{Solution}{%
              \Large\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]%
            }{%
              \ifboolKV[ClesKK]{Aide}{%
                \xintifboolexpr{\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]==\useKV[ClesKK]{Nombre}}{%
                  \cellcolor{LightGray}\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]%
                }{}%
              }{}%
            }%
          }%
        }{%
          \xintifboolexpr{\PfCTestl==0}{%
            \Block[borders={left},line-width=2pt]{1-1}{\ifboolKV[ClesKK]{Solution}{\Large\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]}{%
                \ifboolKV[ClesKK]{Aide}{%
                  \xintifboolexpr{\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]==\useKV[ClesKK]{Nombre}}{%
                    \cellcolor{LightGray}\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]%
                  }{}%
                }{}%
              }%
            }%
          }{%
            \xintifboolexpr{\PfCTestlb==0}{%
              \Block[borders={bottom,left},line-width=2pt]{1-1}{\ifboolKV[ClesKK]{Solution}{\Large\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]}{%
                  \ifboolKV[ClesKK]{Aide}{%
                    \xintifboolexpr{\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]==\useKV[ClesKK]{Nombre}}{%
                      \cellcolor{LightGray}\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]%
                    }{}%
                  }{}%
                }%
              }%
            }{%
              \Block{1-1}{\ifboolKV[ClesKK]{Solution}{\Large\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]}{%
                  \ifboolKV[ClesKK]{Aide}{%
                    \xintifboolexpr{\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]==\useKV[ClesKK]{Nombre}}{%
                      \cellcolor{LightGray}\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*##1+##2},3]%
                    }{}%
                  }{}%
                }%
              }%
            }%
          }%
        }%
        \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$\ListeCasesKK[\fpeval{\useKV[ClesKK]{Taille}*(##1-1)+##2},2]$};%
      }%
    }%
  \end{NiceTabular}
  \restorecomparemode%
}%