You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.3 KiB
119 lines
3.3 KiB
namespace CoreLibrary
|
|
{
|
|
public class Code
|
|
{
|
|
private readonly Jeton?[] lesJetons;
|
|
|
|
public int NbJetons { get; private set; } = 0;
|
|
|
|
public Code(int tailleCode)
|
|
{
|
|
lesJetons = new Jeton?[tailleCode];
|
|
}
|
|
|
|
public Code(IEnumerable<Jeton> jetons)
|
|
{
|
|
lesJetons = new Jeton?[jetons.Count()];
|
|
foreach(Jeton jeton in jetons)
|
|
AjouterJeton(jeton);
|
|
}
|
|
|
|
public void AjouterJeton(Jeton jeton)
|
|
{
|
|
if (EstComplet())
|
|
throw new CodeTableauLesJetonsCompletException();
|
|
|
|
lesJetons[NbJetons++] = jeton;
|
|
}
|
|
|
|
public void SupprimerDernierJeton()
|
|
{
|
|
if(NbJetons <= 0)
|
|
throw new CodeTableauLesJetonsVideException();
|
|
|
|
lesJetons[NbJetons-1] = null;
|
|
--NbJetons;
|
|
}
|
|
|
|
public Jeton RecupererJeton(int indice)
|
|
{
|
|
if(indice < 0 || indice > TailleMaximale())
|
|
throw new CodeIndiceHorsDePorteeException();
|
|
|
|
Jeton? jeton = lesJetons[indice];
|
|
|
|
if (!jeton.HasValue)
|
|
throw new CodeJetonNullException();
|
|
|
|
return jeton.Value;
|
|
}
|
|
|
|
public IEnumerable<Jeton?> Jetons()
|
|
{
|
|
return lesJetons;
|
|
}
|
|
|
|
public bool EstComplet()
|
|
{
|
|
return NbJetons == lesJetons.Length;
|
|
}
|
|
|
|
public int TailleMaximale()
|
|
{
|
|
return lesJetons.Length;
|
|
}
|
|
|
|
public IEnumerable<Indicateur> Comparer(Code autreCode)
|
|
{
|
|
// Mon code est le code correct, l'autre code est celui qui teste
|
|
if (!autreCode.EstComplet())
|
|
throw new CodeTableauLesJetonsIncompletException();
|
|
|
|
Indicateur[] indicateurs = [];
|
|
|
|
Jeton?[] mesJetons = Jetons().ToArray();
|
|
Jeton?[] sesJetons = autreCode.Jetons().ToArray();
|
|
|
|
for (int i = 0; i < mesJetons.Length; ++i)
|
|
{
|
|
Jeton? monJeton = mesJetons[i];
|
|
Jeton? sonJeton = sesJetons[i];
|
|
|
|
if (monJeton.HasValue && sonJeton.HasValue && monJeton.Value.Couleur.Equals(sonJeton.Value.Couleur))
|
|
{
|
|
indicateurs = indicateurs.Append(Indicateur.BONNEPLACE).ToArray();
|
|
mesJetons[i] = null;
|
|
sesJetons[i] = null;
|
|
}
|
|
}
|
|
|
|
|
|
for (int i = 0; i < sesJetons.Length; ++i)
|
|
{
|
|
Jeton? sonJeton = sesJetons[i];
|
|
|
|
if (sonJeton.HasValue)
|
|
{
|
|
for (int j = 0; j < mesJetons.Length; ++j)
|
|
{
|
|
Jeton? monJeton = mesJetons[j];
|
|
|
|
if (monJeton.HasValue && sonJeton.Value.Couleur.Equals(monJeton.Value.Couleur))
|
|
{
|
|
indicateurs = indicateurs.Append(Indicateur.BONNECOULEUR).ToArray();
|
|
mesJetons[j] = null;
|
|
sesJetons[i] = null;
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return indicateurs;
|
|
}
|
|
}
|
|
}
|
|
|
|
|