%%%
% Defi Table
%%%

\setKVdefault[DefiTable]{Solution=false,Math=false,LargeurT=5mm,Graines=false,Creation=false,ValeurMin=1,ValeurMax=10}
\defKV[DefiTable]{Graine=\setKV[DefiTable]{Graines}}%

\newcommand\MelangeListeNew[2]{%
  % #1 Liste à mélanger
  % #2 Nombre d'éléments à conserver
  \setsepchar[*]{/}\ignoreemptyitems%
  \readlist*\ListeInter{#1}%
  \xdef\faa{}% Liste construite
  \xdef\fii{}% Liste détruite
  % on crée les #2 premieres solutions.
  \xintFor* ##1 in {\xintSeq{1}{#2}}\do{%
    \xintifboolexpr{\ListeInterlen>1}{%
      \xdef\Alea{\fpeval{randint(\ListeInterlen)}}%
      \xdef\faa{\faa \ListeInter[\Alea]/}%
      \xdef\fii{}%
      \xintFor* ##2 in {\xintSeq{1}{\ListeInterlen}}\do{%
        \xintifboolexpr{##2 == \Alea}{%
        }{%
          \xdef\fii{\fii \ListeInter[##2]/}%
        }%
      }%
    }{%
      \xdef\faa{\faa \ListeInter[1]}%
    }%
    \readlist*\ListeInter{\fii}%
  }%
  \reademptyitems%
}%

\NewDocumentCommand\JeConstruisLesProduits{mm}{%
  \xintFor* ##1 in{\xintSeq{#1}{#2}}\do{%
    \xintifForFirst{%
      \xdef\ListeDesProduitsFoo{\fpeval{##1}}%
      \xintFor* ##2 in{\xintSeq{2}{10}}\do{%
        \xdef\ListeDesProduitsFoo{\ListeDesProduitsFoo/\fpeval{##1*##2}}%
      }%
    }{%
      \xintFor* ##2 in{\xintSeq{1}{10}}\do{%
        \xdef\JeCoupe{0}%
        \xintFor* ##3 in{\xintSeq{#1}{\fpeval{##1-1}}}\do{%
          \xintifboolexpr{\fpeval{##1*##2}>\fpeval{##3*10}}{}{%
            \modulo{\fpeval{##1*##2}}{##3}\relax%
            \xintifboolexpr{\remainder==0}{\xdef\JeCoupe{1}\xintBreakFor}{}%
          }%
        }%
        \xintifboolexpr{\JeCoupe==1}{}{\xdef\ListeDesProduitsFoo{\ListeDesProduitsFoo/\fpeval{##1*##2}}}%
      }%
    }%
  }%
  \setsepchar[*]{/}%
  \readlist*\ListeDesProduits{\ListeDesProduitsFoo}%
  \xdef\NombreDeProduitATester{\ListeDesProduitslen}%
}%

\newcommand\DefiTableNombreLettreduCode[1]{%
  \JeConstruisLesProduits{\useKV[DefiTable]{ValeurMin}}{\useKV[DefiTable]{ValeurMax}}%
  \MelangeListeNew{\ListeDesProduitsFoo}{\NombreDeProduitATester}
%  Les produits mélangés sont :\par
  \readlist*\ListeDesProduits{\faa}%
  \xdef\ListeDesCaracteresFoo{a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/à/é/è/ê/ï/î/ô/ö/ù/ç/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/À/É/È/,/$?$/$;$/./$!$/$:$/-}
  \savecomparemode%
  \comparestrict%
  \xdef\PfCFooDepart{}
  \StrLen{#1}[\LongueurMot]%
  \xintFor* ##1 in{\xintSeq{1}{\LongueurMot}}\do{%
    \StrChar{#1}{##1}[\LettreMot]
    \xdef\PfCFooDepart{\PfCFooDepart \LettreMot/}
  }%
  \setsepchar[*]{/}\reademptyitems%
  \readlist*\ListeDesLettres{\PfCFooDepart}%
  \xdef\PfCFooArrivee{\ListeDesLettres[1]}%
  \xintFor* ##1 in{\xintSeq{2}{\LongueurMot}}\do{%
    \StrCompare{\ListeDesLettres[##1]}{\\}[\PfCRetiensEtoile]
    \StrCompare{\ListeDesLettres[##1]}{ }[\PfCRetiensPara]
    \xintifboolexpr{\PfCRetiensEtoile==0 || \PfCRetiensPara==0}{}{%
      \xdef\PfCTotal{0}%
      \xintFor* ##2 in{\xintSeq{1}{\fpeval{##1-1}}}\do{%
        \StrCompare{\ListeDesLettres[##1]}{\ListeDesLettres[##2]}[\PfCRetiens]
        \xdef\PfCTotal{\fpeval{\PfCTotal+\PfCRetiens}}%
      }%
      \xintifboolexpr{\PfCTotal==\fpeval{##1-1}}{\xdef\PfCFooArrivee{\PfCFooArrivee/\ListeDesLettres[##1]}}{}%
    }%
  }%
  %Arrivee = \PfCFooArrivee\par
  \setsepchar[*]{/}\ignoreemptyitems%
  \readlist*\ListeDesLettresUniques{\PfCFooArrivee}%
  %La liste des lettres uniques ainsi créée :\par
  %\showitems\ListeDesLettresUniques[]%
  % Il faut retirer les lettres uniques de la liste des caracteres
  \readlist*\ListeTotaleDesCaracteres{\ListeDesCaracteresFoo}%
  %\par
  %La liste totale des caractères est :\par
  %\showitems\ListeTotaleDesCaracteres[]
  %\par
  \xdef\ListeCaracteresUniques{}%
  \xintFor* ##1 in{\xintSeq{1}{\ListeTotaleDesCaractereslen}}\do{%
    %Le caractère testé est \ListeTotaleDesCaracteres[##1]. On le compare à :%
    \xdef\PfCTotal{0}%
    \xintFor* ##2 in{\xintSeq{1}{\ListeDesLettresUniqueslen}}\do{%
      \StrCompare{\ListeTotaleDesCaracteres[##1]}{\ListeDesLettresUniques[##2]}[\PfCRetiens]
      \xdef\PfCTotal{\fpeval{\PfCTotal+\PfCRetiens}}%
    }%
    \xintifboolexpr{\PfCTotal==\ListeDesLettresUniqueslen}{\xdef\ListeCaracteresUniques{\ListeCaracteresUniques\ListeTotaleDesCaracteres[##1]/}}{}%
  }%
  %La liste des caractères uniques à ajouter
  \MelangeListeNew{\ListeCaracteresUniques}{\fpeval{\NombreDeProduitATester-\ListeDesLettresUniqueslen}}%
  %\par La liste des éléments à mélanger est :\par
  \xdef\ListeDesCaracteresAUtiliser{}
  \xintFor* ##1 in{\xintSeq{1}{\ListeDesLettresUniqueslen}}\do{%
    \xdef\ListeDesCaracteresAUtiliser{\ListeDesCaracteresAUtiliser \ListeDesLettresUniques[##1]/}%
  }%
  \xdef\ListeDesCaracteresAUtiliser{\ListeDesCaracteresAUtiliser \faa}
  \MelangeListeNew{\ListeDesCaracteresAUtiliser}{\NombreDeProduitATester}
  %Finalement, on utilise ces caractères :\par
  \ignoreemptyitems%
  \readlist*\ListeFinaleDesCaracteres{\faa}
  \restorecomparemode
  \reademptyitems
}

\newcommand\DefiTable[2][]{%
  % 1 les clés
  % 2 la table de décodage
  \useKVdefault[DefiTable]%
  \setKV[DefiTable]{#1}%
  \ifboolKV[DefiTable]{Creation}{%
    \ifboolKV[DefiTable]{Graines}{\PfCGraineAlea{\useKV[DefiTable]{Graine}}}{}%
    \DefiTableNombreLettreduCode{#2}%
    \begin{center}
      \begin{tabular}{|>{\columncolor{gray!15}}c|*{10}{c|}}
        \hline
        \rowcolor{gray!15}$\times$&\xintFor* ##1 in {\xintSeq {1}{10}}\do{%
                                    \xintifForFirst{}{&}##1%
                                                        }\\\hline%
        \xintFor* ##1 in{\xintSeq{1}{10}}\do{%
        ##1\xintFor* ##2 in{\xintSeq{1}{10}}\do{%
                                  &\xintFor* ##3 in{\xintSeq{1}{\NombreDeProduitATester}}\do{%
                                    \xintifboolexpr{\fpeval{##1*##2}==\ListeDesProduits[##3] 'and' any(\useKV[DefiTable]{ValeurMin}<=##2<=\useKV[DefiTable]{ValeurMax},\useKV[DefiTable]{ValeurMin}<=##1<=\useKV[DefiTable]{ValeurMax})}{\ListeFinaleDesCaracteres[##3]}{}%
                                    }%
                                    }\\\hline%
        }%
      \end{tabular}%
    \end{center}
  }{%
  \setsepchar[*]{§* }%
  \readlist*\ListeDefiTableCode{#2}%
  \begin{NiceTabular}{>{\columncolor{gray!15}}{c}*{10}{c}}[hvlines]
    \rowcolor{gray!15}$\times$&\xintFor* ##1 in {\xintSeq {1}{10}}\do{%
      \xintifForFirst{}{&}##1}
    \\
    1\xintFor* ##1 in {\xintSeq {1}{10}}\do{%
      &\ListeDefiTableCode[1,##1]%
    }\\
    \xintFor* ##1 in {\xintSeq {2}{9}}\do{%
      ##1\xintFor* ##2 in {\xintSeq {1}{##1}}\do{%
        &\ListeDefiTableCode[##2,\fpeval{##1-##2+1}]%
      }\xintFor* ##2 in {\xintSeq {1}{\fpeval{10-##1}}}\do{%
        &\ListeDefiTableCode[##1,\fpeval{##2+1}]%
        }%
      \\
    }%
    10&\ListeDefiTableCode[1,10]&\ListeDefiTableCode[2,9]&\ListeDefiTableCode[3,8]&\ListeDefiTableCode[4,7]&\ListeDefiTableCode[5,6]&\ListeDefiTableCode[6,5]&\ListeDefiTableCode[7,4]&\ListeDefiTableCode[8,3]&\ListeDefiTableCode[9,2]&\ListeDefiTableCode[10,1]\\
  \end{NiceTabular}%
  }%
}%

\newcommand\DefiTableTexte[3][]{%
  \useKVdefault[DefiTable]%
  \setKV[DefiTable]{#1}%
  \ifboolKV[DefiTable]{Creation}{%
    \setsepchar[*]{\\* }%
    \readlist*\ListeDefiTableTableau{#3}%
    \xdef\ListeDefiTableMax{0}%
    \xintFor* ##1 in{\xintSeq{1}{\ListeDefiTableTableaulen}}\do{%
      \StrLen{\ListeDefiTableTableau[##1]}[\PfCDTLongueur]%
      \xintifboolexpr{\ListeDefiTableMax<\PfCDTLongueur}{\xdef\ListeDefiTableMax{\fpeval{\PfCDTLongueur}}}{}%%
    }%
    %\par Le max est \ListeDefiTableMax
    \begin{NiceTabular}{*{\fpeval{\ListeDefiTableMax}}{>{\centering\arraybackslash}m{\useKV[DefiTable]{LargeurT}}}}
      \xintFor* ##1 in {\xintSeq {1}{\fpeval{\ListeDefiTableTableaulen}}}\do{%
        \StrLen{\ListeDefiTableTableau[##1]}[\PfCDTLongueur]%
        \xintFor* ##2 in {\xintSeq {1}{\PfCDTLongueur}}\do{%
          \xintifForFirst{}{&}%
          \StrMid{\ListeDefiTableTableau[##1]}{##2}{##2}[\DefiTableMaLettre]%
          \IfStrEq{\DefiTableMaLettre}{ }{\Block[]{1-1}{}}{\Block[borders={bottom}]{1-1}{\ifboolKV[DefiTable]{Solution}{\StrMid{\ListeDefiTableTableau[##1]}{##2}{##2}}{}}}%
        }\\
        \StrLen{\ListeDefiTableTableau[##1]}[\PfCDTLongueur]%
        \xintFor* ##2 in {\xintSeq {1}{\PfCDTLongueur}}\do{%
          \xintifForFirst{}{&}%
          \StrMid{\ListeDefiTableTableau[##1]}{##2}{##2}[\DefiTableMaLettre]%
          \IfStrEq{\DefiTableMaLettre}{*}{}{%
            \xintFor* ##3 in{\xintSeq{1}{\NombreDeProduitATester}}\do{%
              \IfStrEq{\DefiTableMaLettre}{\ListeFinaleDesCaracteres[##3]}{\Block{}{\footnotesize\ListeDesProduits[##3]}}{}%
            }%
          }%
        }\\
        \StrLen{\ListeDefiTableTableau[##1]}[\PfCDTLongueur]%
        \xintFor* ##2 in {\xintSeq {1}{\PfCDTLongueur}}\do{%
          \xintifForFirst{}{&}
        }\\
      }%
    \end{NiceTabular}%
  }{%
    \setsepchar[*]{§*/}%
    \readlist*\ListeDefiTableTableau{#2}%
    \xdef\ListeDefiTableMax{0}%
    \setsepchar{§}%
    \readlist*\ListeDefiTablePhrase{#3}%
    \foreachitem\compteur\in\ListeDefiTableTableau{%
      \xintifboolexpr{\ListeDefiTableMax<\listlen\ListeDefiTableTableau[\compteurcnt]}{\xdef\ListeDefiTableMax{\fpeval{\listlen\ListeDefiTableTableau[\compteurcnt]}}}{}%
    }%
    \begin{NiceTabular}{*{\fpeval{\ListeDefiTableMax}}{>{\centering\arraybackslash}m{\useKV[DefiTable]{LargeurT}}}}
      \xintFor* ##1 in {\xintSeq {1}{\fpeval{\ListeDefiTableTableaulen}}}\do{%
        \xintFor* ##2 in {\xintSeq {1}{\listlen\ListeDefiTableTableau[##1]}}\do{%
          \xintifForFirst{}{&}\ifboolKV[DefiTable]{Solution}{%
            \StrMid{\ListeDefiTablePhrase[##1]}{##2}{##2}[\DefiTableMaLettre]%
            \IfStrEq{\DefiTableMaLettre}{*}{\Block[]{2-1}{}}{\Block[borders={bottom}]{2-1}{\StrMid{\ListeDefiTablePhrase[##1]}{##2}{##2}}}%
          }{%
            \IfStrEq{\ListeDefiTableTableau[##1,##2]}{*}{\Block[]{2-1}{}}{\Block[borders={bottom}]{2-1}{}}%
          }%%
        }\\
        \xintFor* ##2 in {\xintSeq {1}{\listlen\ListeDefiTableTableau[##1]}}\do{%
          \xintifForFirst{}{&}
        }\\
        \xintFor* ##2 in {\xintSeq {1}{\listlen\ListeDefiTableTableau[##1]}}\do{%
          \xintifForFirst{}{&}\IfStrEq{\ListeDefiTableTableau[##1,##2]}{*}{}{\footnotesize\ifboolKV[DefiTable]{Math}{\ListeDefiTableTableau[##1,##2]}{\num{\ListeDefiTableTableau[##1,##2]}}}%
        }\\
      }%
    \end{NiceTabular}%
  }%
}%