%%%
% Squaro
%%%
\setKVdefault[Squaro]{Solution=false,Longueur=8,Largeur=8,Echelle=8mm,Nom=Jeu1,Graines=false}
\defKV[Squaro]{Graine=\setKV[Squaro]{Graines}}%

\NewDocumentCommand\SquarO{o}{%
  \useKVdefault[Squaro]%
  \setKVdefault[Squaro]{#1}%
  \ifboolKV[Squaro]{Solution}{\BuildSquaroSolution}{\BuildSquaro}%
}

\NewDocumentCommand\BuildSquaro{}{%
  \ifluatex
  \mplibforcehmode
  \xdef\PfCSquaroNom{\useKV[Squaro]{Nom}}%
  \begin{mplibcode}[\PfCSquaroNom]
    numeric Longueur,Largeur;
    Longueur=\useKV[Squaro]{Longueur};
    Largeur=\useKV[Squaro]{Largeur};
    boolean Solution,Graines;
    Solution=\useKV[Squaro]{Solution};
    Graines:=\useKV[Squaro]{Graines};
    if Graines:
    randomseed:=\useKV[Squaro]{Graine};
    fi;
    
    u:=\useKV[Squaro]{Echelle};
    
    p:=0;
    pair A[];%centre des carrés.
    boolean Allume[][];
    
    for k=0 upto Longueur-1:
    for l=0 upto Largeur-1:
    p:=p+1;
    A[p]=u*(k,-l);
    trace (unitsquare scaled u) shifted A[p];
    endfor;
    endfor;

    for k=0 upto Largeur:
    for l=0 upto Longueur:
    m:=uniformdeviate(1);
    if m<0.5:
    Allume[k][l]=true;
    fill cercles(u*(l,-k+1),1mm) if Solution=false:withcolor white fi;
    else:
    Allume[k][l]=false;
    fill cercles(u*(l,-k+1),1mm) withcolor white;
    fi;
    trace cercles(u*(l,-k+1),1mm);
    endfor;
    endfor;

    for k=0 upto Largeur-1:
    for l=0 upto Longueur-1:
    Retiens:=0;
    if Allume[k][l]:Retiens:=Retiens+1 fi;
    if Allume[k][l+1]:Retiens:=Retiens+1 fi;
    if Allume[k+1][l]:Retiens:=Retiens+1 fi;
    if Allume[k+1][l+1]:Retiens:=Retiens+1 fi;
    label(TEX(decimal(Retiens)),u*(l+0.5,-k+0.5));
    endfor;
    endfor;
 \end{mplibcode}
 \fi
}

\NewDocumentCommand\BuildSquaroSolution{}{%
  \ifluatex
  \mplibforcehmode
  \xdef\PfCSquaroNom{\useKV[Squaro]{Nom}}%
  \begin{mplibcode}[\PfCSquaroNom]
    u:=\useKV[Squaro]{Echelle};
    p:=0;
    for k=0 upto Longueur-1:
    for l=0 upto Largeur-1:
    p:=p+1;
    trace (unitsquare scaled u) shifted A[p];
    endfor;
    endfor;

    for k=0 upto Largeur:
    for l=0 upto Longueur:
    if Allume[k][l]=true:
    fill cercles(u*(l,-k+1),1mm);
    else:
    fill cercles(u*(l,-k+1),1mm) withcolor white;
    fi;
    trace cercles(u*(l,-k+1),1mm);
    endfor;
    endfor;

    for k=0 upto Largeur-1:
    for l=0 upto Longueur-1:
    Retiens:=0;
    if Allume[k][l]:Retiens:=Retiens+1 fi;
    if Allume[k][l+1]:Retiens:=Retiens+1 fi;
    if Allume[k+1][l]:Retiens:=Retiens+1 fi;
    if Allume[k+1][l+1]:Retiens:=Retiens+1 fi;
    label(TEX(decimal(Retiens)),u*(l+0.5,-k+0.5));
    endfor;
    endfor;
 \end{mplibcode}
 \fi
}%