%%%
% Ronde infernale
%%%
\def\filedateRondeI{2025/05/26}%
\def\fileversionRondeI{0.1a}%
\message{-- \filedateRondeI\space v\fileversionRondeI}%
%
\newcommand\TestNombrePremier[1]{%
  % #1 le nombre \`a tester
  \newcount\anp\newcount\bnp\newcount\cnp%
  \anp=#1\relax
  \bnp=2\relax
  \premier=-1\relax
  \whiledo{\anp > 1}{%
    \modulo{\the\anp}{\the\bnp}
    \ifnum\remainder=0\relax%
      \global\premier=\numexpr\premier+1\relax%
      \cnp=\numexpr\anp/\bnp\relax%
      \anp=\cnp\relax%
    \else%
      \bnp=\numexpr\bnp+1\relax%
    \fi%
  }%
}%

\newcommand\PfCRIListeDiviseur[1]{%#1 : nombre entier
  \newcount\anp\newcount\bnp%
  \anp=\fpeval{abs(#1)}%
  \bnp=2\relax%
  \xdef\PfCRIRetiensListeDiviseurs{}%
  \ifnum#1=0%
  \xdef\PfCRIRetiensListeDiviseurs{\fpeval{randint(2,5)},\fpeval{randint(6,9)}}%
  \else
  \whiledo{\bnp<\anp}{%
    \modulo{\the\anp}{\the\bnp}{}%
    \ifnum\remainder=0%
    \ifnum#1>0
    \xdef\PfCRIRetiensListeDiviseurs{\PfCRIRetiensListeDiviseurs \the\bnp,}%
    \else
    \xdef\PfCRIRetiensListeDiviseurs{\PfCRIRetiensListeDiviseurs \the\bnp,-\the\bnp,}%
    \fi
    \fi%
    \bnp=\numexpr\bnp+1%
  }%
  \ifnum#1>0
  \xdef\PfCRIRetiensListeDiviseurs{\PfCRIRetiensListeDiviseurs \the\anp,1}
  \else
  \xdef\PfCRIRetiensListeDiviseurs{\PfCRIRetiensListeDiviseurs \the\anp,-\the\anp}
  \fi
  \fi
}%

\newcommand\ChercheLesDiviseurs[1]{%
  \PfCRIListeDiviseur{#1}%
}%

\setKVdefault[RondeInfernale]{Solution=false,Relatifs=false,Etapes=4,Perso=false,%Cles=false,
  Vide,Rayon=2.5cm,Rectangle=false,Div=false,CDot=false,Graine={},Cle={},ListeOperations={},ListeNombres={}}%
\defKV[RondeInfernale]{%
  %Graine=\ifempty{#1}{}{\PfCGraineAlea{#1}},%
  %Cle=\ifempty{#1}{}{\setKV[RondeInfernale]{Cles}\setKV[RondeInfernale]{Vide=false}},%
  ListeOperations=\ifempty{#1}{}{\setKV[RondeInfernale]{Perso}},%
  ListeNombres=\ifempty{#1}{}{\setKV[RondeInfernale]{Perso}}
}% 

\NewDocumentCommand\RondeInfernale{ }{%Mis en place pour les germanistes : le " est actif :(
  \begingroup
  \catcode`\"12
  \RondeInfernaleaux
}%

\NewDocumentCommand\RondeInfernaleaux{o}{%
  \endgroup
  \useKVdefault[RondeInfernale]%
  \setKV[RondeInfernale]{#1}%
  % Graine
  \ifemptyKV[RondeInfernale]{Graine}{}{\PfCGraineAlea{\useKV[RondeInfernale]{Graine}}}%
  % Nombre clé
  \ifemptyKV[RondeInfernale]{Cle}{%
    \ifboolKV[RondeInfernale]{Relatifs}{%
      \edef\PfCRIListeNegatifs{-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9,10}%
      \MelangeListe{\PfCRIListeNegatifs}{1}%
      \readlist*\PfCRIListeCle{\faa}%
      \edef\PfCRINombreCle{\PfCRIListeCle[1]}%
    }{%
      \edef\PfCRINombreCle{\fpeval{randint(2,9)}}%
    }%
  }{%
    \edef\PfCRINombreCle{\useKV[RondeInfernale]{Cle}}%
    \setKV[RondeInfernale]{Vide=false}%
  }%
  \ifboolKV[RondeInfernale]{Perso}{%
    \edef\PfCRIRecupOperations{\useKV[RondeInfernale]{ListeOperations}}%
    \edef\PfCRIRecupNombres{\useKV[RondeInfernale]{ListeNombres}}%
  }{%
    % On définit les éléments nécessaires
    \edef\PfCRIListeToutesOperations{+,-,*,/}%
    \edef\PfCRIListeOperationsSansDiv{+,-,*}%
    \edef\PfCRIListeOperationsSansMul{+,-,/}%
    \edef\PfCRIListeOperationsSimple{+,-}%
    \edef\PfCRIRecupOperations{}%
    \edef\PfCRIRecupNombres{}%
    \edef\PfCRIRecupResultats{}%
    \edef\PfCRINombreATester{\PfCRINombreCle}%
    \edef\PfCRIRetiensNbEtapes{\useKV[RondeInfernale]{Etapes}}%
    \setsepchar{,}\ignoreemptyitems%
%    % Liste des opérations jusqu'à l'étape n-1
    \xintFor* ##1 in{\xintSeq{1}{\fpeval{\useKV[RondeInfernale]{Etapes}-1}}}\do{%
      \TestNombrePremier{\PfCRINombreATester}\relax%
      \xintifboolexpr{\premier==0}{%
%        % Définition des opérations
        \MelangeListe*{\PfCRIListeOperationsSansDiv}{1}%
        \readlist*\PfCRIRetiensLoperation{\faa}%
        \edef\PfCRIRecupOperations{\PfCRIRecupOperations,\faa}%
        \edef\PfCRIOperateur{\fpeval{randint(2,9)}}%
        \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRIOperateur,}%
        \edef\PfCRIResultat{\fpeval{\PfCRINombreATester\PfCRIRetiensLoperation[1]\PfCRIOperateur}}%
        \edef\PfCRIRecupResultats{\PfCRIRecupResultats \PfCRIResultat,}%
        \edef\PfCRINombreATester{\PfCRIResultat}%
      }{%
        \MelangeListe*{\PfCRIListeToutesOperations}{1}%
        \readlist*\PfCRIRetiensLoperation{\faa}%
        \edef\PfCRIRecupOperations{\PfCRIRecupOperations,\faa}%
        \IfStrEq{\PfCRIRetiensLoperation[1]}{/}{%
          \PfCRIListeDiviseur{\PfCRINombreATester}%
          \MelangeListe{\PfCRIRetiensListeDiviseurs}{1}%
          \readlist*\PfCRIRetiensLeDiviseur{\faa}%
          \edef\PfCRIOperateur{\PfCRIRetiensLeDiviseur[1]}%
        }{%
          \edef\PfCRIOperateur{\fpeval{randint(2,9)}}%
        }%
        \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRIOperateur,}%
        \edef\PfCRIResultat{\fpeval{\PfCRINombreATester\PfCRIRetiensLoperation[1]\PfCRIOperateur}}%
        \edef\PfCRIRecupResultats{\PfCRIRecupResultats \PfCRIResultat,}%
        \edef\PfCRINombreATester{\PfCRIResultat}%
      }%
    }%
    % Dernière opération
    \edef\PfCRIEcartFinal{\fpeval{abs(\PfCRINombreATester)-abs(\PfCRINombreCle)}}%
    \xintifboolexpr{\PfCRIEcartFinal>0}{%
      \modulo{\PfCRINombreATester}{\PfCRINombreCle}%
      \xintifboolexpr{\remainder==0}{%
        \MelangeListe*{\PfCRIListeOperationsSansMul}{1}%
        \edef\PfCRIRecupOperations{\PfCRIRecupOperations,\faa}%
        \ignoreemptyitems%
        \readlist*\PfCRIRetiensLoperation{\faa}%
        \IfStrEq{\PfCRIRetiensLoperation[1]}{/}{%
          \edef\PfCRINombreATester{\fpeval{\PfCRINombreATester/\PfCRINombreCle}}%
        }{%
          \IfStrEq{\PfCRIRetiensLoperation[1]}{-}{%
            \edef\PfCRINombreATester{\fpeval{\PfCRINombreATester-\PfCRINombreCle}}%
          }{%
            \edef\PfCRINombreATester{\fpeval{\PfCRINombreCle-\PfCRINombreATester}}%
          }%
        }%
        \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRINombreATester}%
      }{%
        \MelangeListe*{\PfCRIListeOperationsSimple}{1}%
        \edef\PfCRIRecupOperations{\PfCRIRecupOperations,\faa}%
        \ignoreemptyitems%
        \readlist*\PfCRIRetiensLoperation{\faa}%
        \IfStrEq{\PfCRIRetiensLoperation[1]}{-}{%
          \edef\PfCRINombreATester{\fpeval{\PfCRINombreATester-\PfCRINombreCle}}%
        }{%
          \edef\PfCRINombreATester{\fpeval{\PfCRINombreCle-\PfCRINombreATester}}%
        }%
        \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRINombreATester}%
      }%
    }{%
      \xintifboolexpr{\PfCRIEcartFinal==0}{%
        \edef\PfCRIRecupOperations{\PfCRIRecupOperations *}%
        \ignoreemptyitems%
        \readlist*\PfCRIRIListeOperations{\PfCRIRecupOperations}%
        \edef\PfCRIRecupNombres{\PfCRIRecupNombres 1}%
      }{%
        \xintifboolexpr{\PfCRINombreATester==0}{%
          \MelangeListe*{\PfCRIListeOperationsSimple}{1}%
          \edef\PfCRIRecupOperations{\PfCRIRecupOperations,\faa}%
          \ignoreemptyitems%
          \readlist*\PfCRIRetiensLoperation{\faa}%
          \IfStrEq{\PfCRIRetiensLoperation[1]}{-}{%
            \edef\PfCRINombreATester{\fpeval{\PfCRINombreATester-\PfCRINombreCle}}%
          }{%
            \edef\PfCRINombreATester{\fpeval{\PfCRINombreCle-\PfCRINombreATester}}%
          }%
          \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRINombreATester}%
        }{%
          \modulo{\PfCRINombreCle}{\PfCRINombreATester}{}%
          \xintifboolexpr{\remainder==0}{%-1
            \MelangeListe*{\PfCRIListeToutesOperations}{1}%
            \edef\PfCRIRecupOperations{\PfCRIRecupOperations,\faa}%
            \readlist*\PfCRIRetiensLoperation{\faa}%
            \ignoreemptyitems%
            \readlist*\PfCRIRIListeOperations{\PfCRIRecupOperations}%
            \IfStrEq{\PfCRIRetiensLoperation[1]}{*}{%
              \edef\PfCRINombreATester{\fpeval{\PfCRINombreCle/\PfCRINombreATester}}%
              \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRINombreATester}%
            }{
              \IfStrEq{\PfCRIRetiensLoperation[1]}{-}{%
                \edef\PfCRINombreATester{\fpeval{\PfCRINombreATester-\PfCRINombreCle}}%
              }{%
                \edef\PfCRINombreATester{\fpeval{\PfCRINombreCle-\PfCRINombreATester}}%
              }%
              \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRINombreATester}%
            }%
          }{%
            \MelangeListe*{\PfCRIListeOperationsSimple}{1}%
            \edef\PfCRIRecupOperations{\PfCRIRecupOperations,\faa}%
            \ignoreemptyitems%
            \readlist*\PfCRIRIListeOperations{\PfCRIRecupOperations}%
            \IfStrEq{\PfCRIRIListeOperations[\PfCRIRetiensNbEtapes]}{-}{%
              \edef\PfCRINombreATester{\fpeval{\PfCRINombreATester-\PfCRINombreCle}}%
            }{%
              \edef\PfCRINombreATester{\fpeval{\PfCRINombreCle-\PfCRINombreATester}}%
            }%
            \edef\PfCRIRecupNombres{\PfCRIRecupNombres \PfCRINombreATester}%
          }%
        }%
      }%
    }%
  }%
  \ignoreemptyitems%
  \readlist*\PfCRIRIOperations{\PfCRIRecupOperations}%
  \readlist*\PfCRIRINombres{\PfCRIRecupNombres}%
  \reademptyitems%
  % On prépare le dessin
  \edef\PfCRINombresDessin{}%
  \edef\PfCRIOperationsDessin{}%
  \foreachitem\compteur\in\PfCRIRINombres{%
    \xintifboolexpr{\PfCRIRINombres[\compteurcnt]<0}{%
      \edef\PfCRINombresDessin{\PfCRINombresDessin "(\num{\PfCRIRINombres[\compteurcnt]})",}%
    }{%
      \edef\PfCRINombresDessin{\PfCRINombresDessin "\num{\PfCRIRINombres[\compteurcnt]}",}%
    }%
  }%
  \foreachitem\compteur\in\PfCRIRIOperations{%
    \edef\PfCRIOperationsDessin{\PfCRIOperationsDessin "\PfCRIRIOperations[\compteurcnt]",}%
  }%
  % On dessine
  \BuildRondeInfernale{\PfCRINombresDessin}{\PfCRIOperationsDessin}%
}%

\NewDocumentCommand\BuildRondeInfernale{m m}{%
  \mplibforcehmode%
  \begin{mplibcode}
    boolean Vide,Rectangle,Perso,Div,CDot;
    Vide=\useKV[RondeInfernale]{Vide};
    Div=\useKV[RondeInfernale]{Div};
    CDot=\useKV[RondeInfernale]{CDot};
    Rectangle=\useKV[RondeInfernale]{Rectangle};
    Perso=\useKV[RondeInfernale]{Perso};
    if Perso:
    string Depart;
    Depart=\useKV[RondeInfernale]{Cle};
    else:
    numeric Depart;
    Depart=\PfCRINombreCle;
    fi;

    numeric Rayon;
    Rayon=\useKV[RondeInfernale]{Rayon};

    string Nombre[];
    
    vardef Compte(text t)=%On compte le nb d'étapes et on retient les nombres
    nbcase:=0;
    for p_=t:
    nbcase:=nbcase+1;
    Nombre[nbcase]=p_;
    endfor;
    enddef;

    vardef AfficheRonde(text t)=%On fait l'affichage des opérations
    n:=0;
    for k=1 upto nbcase:
    fill cercles(rotation(A[k],O,0.5*(360/nbcase)),5mm) withcolor white;
    endfor;
    for p_=t:
    n:=n+1;
    if p_="*":
    if CDot:
    label(TEX("$\cdot"&Nombre[n]&"$"),rotation(A[nbcase-n],O,0.5*(360/nbcase)));
    else:
    label(TEX("$\times"&Nombre[n]&"$"),rotation(A[nbcase-n],O,0.5*(360/nbcase)));
    fi;
    elseif p_="/":
    if Div:
    label(TEX("$:"&Nombre[n]&"$"),rotation(A[nbcase-n],O,0.5*(360/nbcase)));
    else:
    label(TEX("$\div"&Nombre[n]&"$"),rotation(A[nbcase-n],O,0.5*(360/nbcase)));
    fi;
    else:
    label(TEX("$"&p_&Nombre[n]&"$"),rotation(A[nbcase-n],O,0.5*(360/nbcase)));
        fi;
    endfor;
    enddef;

    vardef AfficheRectangle(text t)=%On fait l'affichage des opérations
    n:=0;
    for p_=t:
    n:=n+1;
    if n=nbcase:
    if p_="*":
    if CDot:
    label.bot(TEX("$\cdot"&Nombre[n]&"$"),1/2[A[n-1],A[0]]+u*(0,-1));
    else:
    label.bot(TEX("$\times"&Nombre[n]&"$"),1/2[A[n-1],A[0]]+u*(0,-1));
    fi;
    elseif p_="/":
    if Div:
    label.bot(TEX("$:"&Nombre[n]&"$"),1/2[A[n-1],A[0]]+u*(0,-1));
    else:
    label.bot(TEX("$\div"&Nombre[n]&"$"),1/2[A[n-1],A[0]]+u*(0,-1));
    fi;
    else:
    label.bot(TEX("$"&p_&Nombre[n]&"$"),1/2[A[n-1],A[0]]+u*(0,-1));
    fi;
    else:
    if p_="*":
    if CDot:
    label.top(TEX("$\cdot"&Nombre[n]&"$"),1/2[A[n-1],A[n]]);
    else:
    label.top(TEX("$\times"&Nombre[n]&"$"),1/2[A[n-1],A[n]]);
    fi;
    elseif p_="/":
    if Div:
    label.top(TEX("$:"&Nombre[n]&"$"),1/2[A[n-1],A[n]]);
    else:
    label.top(TEX("$\div"&Nombre[n]&"$"),1/2[A[n-1],A[n]]);
    fi;
    else:
    label.top(TEX("$"&p_&Nombre[n]&"$"),1/2[A[n-1],A[n]]);
    fi;
    fi;
    endfor;
    enddef;

    vardef BaseRonde=
    pair O,A[];
    O=(0,0);
    path cc;
    cc=cercles(O,Rayon);
    A[0]=pointarc(cc,0);
    A[nbcase+1]=A[0];
    for k=1 upto nbcase:
    A[k]=rotation(A[0],O,k*(360/nbcase));
    endfor;
    for k=0 upto nbcase:
    drawarrow reverse(arccercle(A[k],A[k+1],O) cutbefore cercles(A[k],5mm) cutafter cercles(A[k+1],5mm)) withpen pencircle scaled 1.5;
    endfor;
    for k=0 upto nbcase:
    fill cercles(A[k],5mm) withcolor white;
    trace cercles(A[k],5mm) withpen pencircle scaled 1.5;
    endfor;
    enddef;

    vardef BoiteRectangle=
    save $;
    path $;
    $=u*(0.9,-0.5){dir 0}..{dir 90}(u*(1,-0.4))--(u*(1,0.4)){dir 90}..{dir180}(u*(0.9,0.5))--u*(-0.9,0.5){dir 180}..{dir-90}(u*(-1,0.4))--(u*(-1,-0.4)){dir-90}..{dir0}(u*(-0.9,-0.5))--cycle;
    $
    enddef;
    
    vardef BaseRectangle=
    pair O,A[];
    O=(0,0);
    A[0]=O;
    for k=1 upto nbcase:
    A[k]-A[k-1]=(Rayon+2cm,0);
    endfor;
    for k=0 upto nbcase-2:
    drawarrow (A[k]--A[k+1]) cutbefore (BoiteRectangle shifted A[k]) cutafter (BoiteRectangle shifted A[k+1]) withpen pencircle scaled 1.5;
    endfor;
    drawarrow (A[nbcase-1]--(A[nbcase-1]+u*(0,-1))--(A[0]+u*(0,-1))--A[0]) cutbefore (BoiteRectangle shifted A[nbcase-1]) cutafter (BoiteRectangle shifted A[0]) withpen pencircle scaled 1.5;
    for k=0 upto nbcase-1:
    trace (BoiteRectangle shifted A[k]) withpen pencircle scaled 1.5;
    endfor;
    enddef;
    
    Compte(#1);
    
    if Rectangle:
    BaseRectangle;
    AfficheRectangle(#2);
    else:
    BaseRonde;
    AfficheRonde(#2);
    fi;
    if Vide=false:
    if Perso:
    label(TEX("$"&Depart&"$"),A[0]);
    else:
    label(TEX("$"&decimal(Depart)&"$"),A[0]);
    fi;
    fi;
  \end{mplibcode}%
}%