%%%
% Calculs Croises
%%%
\setKVdefault[CalculsCroises]{Largeur=20pt,Solution=false,Inverse=false,Creation=false,Graines=false,Vide=false,Couleur=LightGray,Negatifs=false,Aide=false}
\defKV[CalculsCroises]{Graine=\setKV[CalculsCroises]{Graines}}%
\defKV[CalculsCroises]{ListeNombres=\setKV[CalculsCroises]{Aide}}%

\newlength\PfCCalculsCroises%
\NewDocumentCommand\CalculsCroises{o m}{%
  \useKVdefault[CalculsCroises]%
  \savecomparemode%
  \comparestrict%
  \setKV[CalculsCroises]{#1}%
  \colorlet{PfCCouleurCC}{\useKV[CalculsCroises]{Couleur}}%
  \setlength{\PfCCalculsCroises}{\useKV[CalculsCroises]{Largeur}+\tabcolsep}%
  \ifboolKV[CalculsCroises]{Inverse}{\setKV[CalculsCroises]{Solution}}{}%
  \ifboolKV[CalculsCroises]{Creation}{%
    \ifboolKV[CalculsCroises]{Graines}{\PfCGraineAlea{\useKV[CalculsCroises]{Graine}}}{}%
    \ifboolKV[CalculsCroises]{Negatifs}{%
      \xdef\PfCCCFoo{-1,-2,-3,-4,-5,-6,-7,-8,-9}%
    }{%
      \xdef\PfCCCFoo{1,2,3,4,5,6,7,8,9}%
    }%
    \MelangeListe{\PfCCCFoo}{9}%
    \setsepchar{,}%
    \readlist*\PfCListeCCNb{\faa}%
    \readlist*\PfCListeCCOp{#2}%
    \xdef\PfCCCfoo{\useKV[CalculsCroises]{ListeNombres}}%
    \readlist*\PfCListeCCAide{\PfCCCfoo}%
    \begin{NiceTabular}{*{6}{m{\useKV[CalculsCroises]{Largeur}}}}
      \rule{0pt}{\PfCCalculsCroises}\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[1]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[1]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[1]}}{}%
            }%
          }{}%
        }}&\StrCompare{\PfCListeCCOp[1]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[1]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[2]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[2]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[2]}}{}%
            }%
          }{}%
        }}&\StrCompare{\PfCListeCCOp[2]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[2]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[3]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[3]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[3]}}{}%
            }%
          }{}%
        }}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCNb[1]\PfCListeCCOp[1]\PfCListeCCNb[2]\PfCListeCCOp[2]\PfCListeCCNb[3]}}}}\\
      \rule{0pt}{\PfCCalculsCroises}\StrCompare{\PfCListeCCOp[3]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[3]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[4]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[4]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[5]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[5]$}}\\
      \rule{0pt}{\PfCCalculsCroises}\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[4]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[4]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[4]}}{}%
            }%
          }{}%
        }}&\StrCompare{\PfCListeCCOp[6]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[6]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[5]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[5]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[5]}}{}%
            }%
          }{}%
        }}&\StrCompare{\PfCListeCCOp[7]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[7]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[6]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[6]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[6]}}{}%
            }%
          }{}%
        }}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCNb[4]\PfCListeCCOp[6]\PfCListeCCNb[5]\PfCListeCCOp[7]\PfCListeCCNb[6]}}}}\\
      \rule{0pt}{\PfCCalculsCroises}\StrCompare{\PfCListeCCOp[8]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[8]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[9]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[9]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[10]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[10]$}}\\
      \rule{0pt}{\PfCCalculsCroises}\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[7]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[7]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[7]}}{}%
            }%
          }{}%
        }}&\StrCompare{\PfCListeCCOp[11]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[11]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[8]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[8]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[8]}}{}%
            }%
          }{}%
        }}&\StrCompare{\PfCListeCCOp[12]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[12]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCNb[9]}}{%
          \ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCNb[9]==\PfCListeCCAide[##1]}{\num{\PfCListeCCNb[9]}}{}%
            }%
          }{}%
        }}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCNb[7]\PfCListeCCOp[11]\PfCListeCCNb[8]\PfCListeCCOp[12]\PfCListeCCNb[9]}}}}\\
      \rule{0pt}{\PfCCalculsCroises}\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCNb[1]\PfCListeCCOp[3]\PfCListeCCNb[4]\PfCListeCCOp[8]\PfCListeCCNb[7]}}}}&&\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCNb[2]\PfCListeCCOp[4]\PfCListeCCNb[5]\PfCListeCCOp[9]\PfCListeCCNb[8]}}}}&&\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCNb[3]\PfCListeCCOp[5]\PfCListeCCNb[6]\PfCListeCCOp[10]\PfCListeCCNb[9]}}}}\\
      \CodeAfter
      \tikz\draw (1-|1) rectangle (6-|6);
      \xintFor* ##1 in{\xintSeq{2}{5}}\do{%
        \tikz\draw (1-|##1) -- (6-|##1);
      }%
      \xintFor* ##1 in{\xintSeq{2}{5}}\do{%
        \tikz\draw (##1-|1) -- (##1-|6);
      }%
    \end{NiceTabular}
  }{%
    \ifboolKV[CalculsCroises]{Vide}{%
      \begin{NiceTabular}{*{6}{m{\useKV[CalculsCroises]{Largeur}}}}
        \rule{0pt}{\PfCCalculsCroises}\Block{}{}&\Block{}{}&\Block{}{}&\Block{}{}&\Block{}{}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{}\\
        \rule{0pt}{\PfCCalculsCroises}\Block{}{}&\Block[fill=black]{}{}&\Block{}{}&\Block[fill=black]{}{}&\Block{}{}\\
        \rule{0pt}{\PfCCalculsCroises}\Block{}{}&\Block{}{}&\Block{}{}&\Block{}{}&\Block{}{}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{}\\
        \rule{0pt}{\PfCCalculsCroises}\Block{}{}&\Block[fill=black]{}{}&\Block{}{}&\Block[fill=black]{}{}&\Block{}{}\\
        \rule{0pt}{\PfCCalculsCroises}\Block{}{}&\Block{}{}&\Block{}{}&\Block{}{}&\Block{}{}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{}\\
        \rule{0pt}{\PfCCalculsCroises}\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{}&&\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{}&&\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{}\\
        \CodeAfter
        \tikz\draw (1-|1) rectangle (6-|6);
        \xintFor* ##1 in{\xintSeq{2}{5}}\do{%
          \tikz\draw (1-|##1) -- (6-|##1);
        }%
        \xintFor* ##1 in{\xintSeq{2}{5}}\do{%
          \tikz\draw (##1-|1) -- (##1-|6);
        }%
      \end{NiceTabular}        
    }{%
      \readlist*\PfCListeCCOp{#2}%
      \xdef\PfCCCfoo{\useKV[CalculsCroises]{ListeNombres}}%
      \readlist*\PfCListeCCAide{\PfCCCfoo}%
      \begin{NiceTabular}{*{6}{m{\useKV[CalculsCroises]{Largeur}}}}
        \rule{0pt}{\PfCCalculsCroises}\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[1]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[1]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[1]}}{}%
            }%
          }{}%
}}&\StrCompare{\PfCListeCCOp[2]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[2]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[3]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[3]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[3]}}{}%
            }%
          }{}%
}}&\StrCompare{\PfCListeCCOp[4]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[4]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[5]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[5]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[5]}}{}%
            }%
          }{}%
}}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCOp[1]\PfCListeCCOp[2]\PfCListeCCOp[3]\PfCListeCCOp[4]\PfCListeCCOp[5]}}}}\\
        \rule{0pt}{\PfCCalculsCroises}\StrCompare{\PfCListeCCOp[6]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[6]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[7]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[7]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[8]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[8]$}}\\
        \rule{0pt}{\PfCCalculsCroises}\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[9]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[9]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[9]}}{}%
            }%
          }{}%
}}&\StrCompare{\PfCListeCCOp[10]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[10]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[11]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[11]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[11]}}{}%
            }%
          }{}%
}}&\StrCompare{\PfCListeCCOp[12]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[12]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[13]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[13]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[13]}}{}%
            }%
          }{}%
}}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCOp[9]\PfCListeCCOp[10]\PfCListeCCOp[11]\PfCListeCCOp[12]\PfCListeCCOp[13]}}}}\\
        \rule{0pt}{\PfCCalculsCroises}\StrCompare{\PfCListeCCOp[14]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[14]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[15]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[15]$}}&\Block[fill=black]{}{~}&\StrCompare{\PfCListeCCOp[16]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[16]$}}\\
        \rule{0pt}{\PfCCalculsCroises}\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[17]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[17]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[17]}}{}%
            }%
          }{}%
}}&\StrCompare{\PfCListeCCOp[18]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[18]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[19]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[19]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[19]}}{}%
            }%
          }{}%
}}&\StrCompare{\PfCListeCCOp[20]}{*}[\PfCTestEtoile]\Block{}{\xintifboolexpr{\PfCTestEtoile==0}{$\times$}{$\PfCListeCCOp[20]$}}&\Block{}{\ifboolKV[CalculsCroises]{Solution}{\num{\PfCListeCCOp[21]}}{\ifboolKV[CalculsCroises]{Aide}{%
            \xintFor* ##1 in{\xintSeq{1}{\PfCListeCCAidelen}}\do{%
              \xintifboolexpr{\PfCListeCCOp[21]==\PfCListeCCAide[##1]}{\num{\PfCListeCCOp[21]}}{}%
            }%
          }{}%
}}&\Block[borders={bottom,right,top},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCOp[17]\PfCListeCCOp[18]\PfCListeCCOp[19]\PfCListeCCOp[20]\PfCListeCCOp[21]}}}}\\
        \rule{0pt}{\PfCCalculsCroises}\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCOp[1]\PfCListeCCOp[6]\PfCListeCCOp[9]\PfCListeCCOp[14]\PfCListeCCOp[17]}}}}&&\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCOp[3]\PfCListeCCOp[7]\PfCListeCCOp[11]\PfCListeCCOp[15]\PfCListeCCOp[19]}}}}&&\Block[borders={left,bottom,right},fill=PfCCouleurCC]{}{\ifboolKV[CalculsCroises]{Inverse}{}{\num{\fpeval{\PfCListeCCOp[5]\PfCListeCCOp[8]\PfCListeCCOp[13]\PfCListeCCOp[16]\PfCListeCCOp[21]}}}}\\
        \CodeAfter
        \tikz\draw (1-|1) rectangle (6-|6);
        \xintFor* ##1 in{\xintSeq{2}{5}}\do{%
          \tikz\draw (1-|##1) -- (6-|##1);
        }%
        \xintFor* ##1 in{\xintSeq{2}{5}}\do{%
          \tikz\draw (##1-|1) -- (##1-|6);
        }%
      \end{NiceTabular}
    }%
  }%
  \restorecomparemode%
}%