%%%
% Tables lacunaires
%%%
\def\filedateTableLacunaire{2026/05/23}%
\def\fileversionTableLacunaire{0.1a}%
\message{-- \filedateTableLacunaire\space v\fileversionTableLacunaire}%
%
\makeatletter
\setKVdefault[TableLacu]{ValeurMin=2,ValeurMax=9,Produits=false,Niveau=50,Sommes=false,Solution=false,Graine={},Lignes=5,Colonnes=4}%
%
\NewDocumentCommand\TableLacunaire{o}{%
  \useKVdefault[TableLacu]%
  \setKV[TableLacu]{#1}%
  \ifemptyKV[TableLacu]{Graine}{}{\PfCGraineAlea{\useKV[TableLacu]{Graine}}}%
  \ifboolKV[TableLacu]{Produits}{\PfC@BuildTableLacuProduitsA}{}%
  \ifboolKV[TableLacu]{Sommes}{\PfC@BuildTableLacuSommesA}{}%
}%

\ExplSyntaxOn
\NewDocumentCommand\PfC@ViderListe{mmm}{%
  % #1 : ListeBase
  % #2 : index des éléments à vider.
  % #3 : Nom de la liste à récupérer
  \clist_set:Ne \l_tmpa_clist {#1}
  \clist_set:Ne \l_tmpb_clist {#2}
  \seq_clear:N \l_tmpa_seq
  \int_set:Nn \l_tmpa_int {\clist_count:N \l_tmpa_clist}
  \int_step_inline:nnn {1} {\l_tmpa_int} {
    \clist_if_in:NnTF \l_tmpb_clist {##1} {
      \seq_put_right:Ne \l_tmpa_seq {A}
    }{
      \seq_put_right:Ne \l_tmpa_seq {\clist_item:Nn \l_tmpa_clist {##1}}
    }
  }
  \clist_set_from_seq:NN \l_tmpb_clist \l_tmpa_seq
  \edef#3{\clist_use:N \l_tmpb_clist}
}

\NewDocumentCommand\PfC@BuildTableLacuSommesA{}{%
  \edef\NombreTotalCases{\fpeval{\useKV[TableLacu]{Colonnes}+(\useKV[TableLacu]{Colonnes}+1)*\useKV[TableLacu]{Lignes}}}%
  \edef\LargeurTableau{\useKV[TableLacu]{Colonnes}}
  \edef\HauteurTableau{\useKV[TableLacu]{Lignes}}
  \ChoixAleaMultiMix{\fpeval{floor(\useKV[TableLacu]{Niveau}*\NombreTotalCases/100)}}{2}{\NombreTotalCases}{0}{\ListeCasesAEffacer}%
  \edef\ValeurMin{\useKV[TableLacu]{ValeurMin}}
  \edef\ValeurMax{\useKV[TableLacu]{ValeurMax}}
  \ChoixAleaMultiMix{\LargeurTableau}{\ValeurMin}{\ValeurMax}{0,1}{\ListeAav}%
  \ChoixAleaMultiMix{\HauteurTableau}{\ValeurMin}{\ValeurMax}{0,1}{\ListeBav}%
  \clist_set:Ne \l_tmpa_clist {\ListeAav}
  \clist_set:Ne \l_tmpb_clist {\ListeBav}
  % Créer la liste des cases:
  \seq_clear:N \l_tmpb_seq
  \seq_put_right:Nn \l_tmpb_seq {+}
  \int_step_inline:nnn {1} {\LargeurTableau} {
    \seq_put_right:Ne \l_tmpb_seq {\clist_item:Nn \l_tmpa_clist {##1}}
  }
  \int_step_inline:nnn {1} {\HauteurTableau} {
    \seq_put_right:Ne \l_tmpb_seq {\clist_item:Nn \l_tmpb_clist {##1}}
    \int_step_inline:nnn {1} {\LargeurTableau} {
      \seq_put_right:Ne \l_tmpb_seq {\fpeval{(\clist_item:Nn \l_tmpb_clist {##1})+(\clist_item:Nn \l_tmpa_clist {####1})}}
    }%
  }%
  \clist_set_from_seq:NN \l_tmpb_clist \l_tmpb_seq
  \edef\ListeCases{\clist_use:N \l_tmpb_clist}
  \PfC@ViderListe{\ListeCases}{\ListeCasesAEffacer}{\Toto}%
  \clist_set:Ne \l_tmpa_clist{\ListeCases}
  \clist_set:Ne \l_tmpb_clist{\Toto}
  % Affichage du tableau
  \begin{tabular}{|*{\fpeval{\LargeurTableau+1}}{m{2em}|}}%
    \hline
    \xintFor* ##1 in{\xintSeq{0}{\HauteurTableau}}\do{%
    \xintFor* ##2 in{\xintSeq{1}{\fpeval{\LargeurTableau+1}}}\do{%
    \edef\Index{\fpeval{##2+(\LargeurTableau+1)*##1}}
    \centering\arraybackslash\xintifboolexpr{\Index==1}{\cellcolor{DarkGray}}{\xintifboolexpr{##1==0}{\cellcolor{LightGray}}{}}\IfStrEq{\clist_item:Nn \l_tmpb_clist {\Index}}{A}{%
    \cellcolor{white}\ifboolKV[TableLacu]{Solution}{\textcolor{Evidence}{\num{\clist_item:Nn \l_tmpa_clist {\Index}}}}{}%
    }{%
    \xintifboolexpr{##1>0}{\xintifboolexpr{##2==1}{\cellcolor{LightGray}}{}}{}\xintifboolexpr{##1==0 'and' ##2==1}{$+$}{\num{\clist_item:Nn \l_tmpb_clist {\Index}}}%
    }%
    \xintifForLast{}{&}
                       }\\
    \hline
    }
  \end{tabular}
}%

\NewDocumentCommand\PfC@BuildTableLacuProduitsA{}{%
  \edef\NombreTotalCases{\fpeval{\useKV[TableLacu]{Colonnes}+(\useKV[TableLacu]{Colonnes}+1)*\useKV[TableLacu]{Lignes}}}%
  \edef\LargeurTableau{\useKV[TableLacu]{Colonnes}}
  \edef\HauteurTableau{\useKV[TableLacu]{Lignes}}
  \ChoixAleaMultiMix{\fpeval{floor(\useKV[TableLacu]{Niveau}*\NombreTotalCases/100)}}{2}{\NombreTotalCases}{0}{\ListeCasesAEffacer}%
  \edef\ValeurMin{\useKV[TableLacu]{ValeurMin}}
  \edef\ValeurMax{\useKV[TableLacu]{ValeurMax}}
  \ChoixAleaMultiMix{\LargeurTableau}{\ValeurMin}{\ValeurMax}{0,1}{\ListeAav}%
  \ChoixAleaMultiMix{\HauteurTableau}{\ValeurMin}{\ValeurMax}{0,1}{\ListeBav}%
  \clist_set:Ne \l_tmpa_clist {\ListeAav}
  \clist_set:Ne \l_tmpb_clist {\ListeBav}
  % Créer la liste des cases:
  \seq_clear:N \l_tmpb_seq
  \seq_put_right:Nn \l_tmpb_seq {$\times$}
  \int_step_inline:nnn {1} {\LargeurTableau} {
    \seq_put_right:Ne \l_tmpb_seq {\clist_item:Nn \l_tmpa_clist {##1}}
  }
  \int_step_inline:nnn {1} {\HauteurTableau} {
    \seq_put_right:Ne \l_tmpb_seq {\clist_item:Nn \l_tmpb_clist {##1}}
    \int_step_inline:nnn {1} {\LargeurTableau} {
      \seq_put_right:Ne \l_tmpb_seq {\fpeval{(\clist_item:Nn \l_tmpb_clist {##1})*(\clist_item:Nn \l_tmpa_clist {####1})}}
    }%
  }%
  \clist_set_from_seq:NN \l_tmpb_clist \l_tmpb_seq
  \edef\ListeCases{\clist_use:N \l_tmpb_clist}
  \PfC@ViderListe{\ListeCases}{\ListeCasesAEffacer}{\Toto}%
  \clist_set:Ne \l_tmpa_clist{\ListeCases}
  \clist_set:Ne \l_tmpb_clist{\Toto}
  % Affichage du tableau
  \begin{tabular}{|*{\fpeval{\LargeurTableau+1}}{m{2em}|}}%
    \hline
    \xintFor* ##1 in{\xintSeq{0}{\HauteurTableau}}\do{%
    \xintFor* ##2 in{\xintSeq{1}{\fpeval{\LargeurTableau+1}}}\do{%
    \edef\Index{\fpeval{##2+(\LargeurTableau+1)*##1}}
    \centering\arraybackslash\xintifboolexpr{\Index==1}{\cellcolor{DarkGray}}{\xintifboolexpr{##1==0}{\cellcolor{LightGray}}{}}\IfStrEq{\clist_item:Nn \l_tmpb_clist {\Index}}{A}{%
    \cellcolor{white}\ifboolKV[TableLacu]{Solution}{\textcolor{Evidence}{\num{\clist_item:Nn \l_tmpa_clist {\Index}}}}{}%
    }{%
    \xintifboolexpr{##1>0}{\xintifboolexpr{##2==1}{\cellcolor{LightGray}}{}}{}\xintifboolexpr{##1==0 'and' ##2==1}{$\times$}{\num{\clist_item:Nn \l_tmpb_clist {\Index}}}%
    }%
    \xintifForLast{}{&}
                       }\\
    \hline
    }
  \end{tabular}
}%

\ExplSyntaxOff
\makeatletter