correction erreur comparer
continuous-integration/drone/push Build is passing Details

master
Céleste BARBOSA 12 months ago
parent bae481b5e4
commit 0aa0621de4

@ -1,136 +1,136 @@
using CoreLibrary.Exceptions; using CoreLibrary.Exceptions;
namespace CoreLibrary namespace CoreLibrary
{ {
public class Code public class Code
{ {
private readonly Jeton?[] lesJetons; private readonly Jeton?[] lesJetons;
public int NbJetons { get; private set; } = 0; public int NbJetons { get; private set; } = 0;
public Code(int tailleCode) public Code(int tailleCode)
{ {
if(tailleCode <= 0) if(tailleCode <= 0)
{ {
throw new TailleCodeException(tailleCode); throw new TailleCodeException(tailleCode);
} }
lesJetons = new Jeton?[tailleCode]; lesJetons = new Jeton?[tailleCode];
} }
public Code(IEnumerable<Jeton> jetons) public Code(IEnumerable<Jeton> jetons)
{ {
if (jetons.Count() == 0) if (jetons.Count() == 0)
{ {
throw new TailleCodeException(jetons.Count()); throw new TailleCodeException(jetons.Count());
} }
lesJetons = new Jeton?[jetons.Count()]; lesJetons = new Jeton?[jetons.Count()];
foreach(Jeton jeton in jetons) foreach(Jeton jeton in jetons)
{ {
AjouterJeton(jeton); AjouterJeton(jeton);
} }
} }
public void AjouterJeton(Jeton jeton) public void AjouterJeton(Jeton jeton)
{ {
if (NbJetons == TailleMaximale()) if (NbJetons == TailleMaximale())
{ {
throw new CodeCompletException(); throw new CodeCompletException();
} }
lesJetons[NbJetons++] = jeton; lesJetons[NbJetons++] = jeton;
} }
public void SupprimerDernierJeton() public void SupprimerDernierJeton()
{ {
if(NbJetons == 0) if(NbJetons == 0)
{ {
throw new CodeVideException(); throw new CodeVideException();
} }
lesJetons[--NbJetons] = null; lesJetons[--NbJetons] = null;
} }
public Jeton RecupererJeton(int indice) public Jeton RecupererJeton(int indice)
{ {
if(indice < 0 || indice > TailleMaximale()) if(indice < 0 || indice > TailleMaximale())
throw new IndiceCodeException(indice, NbJetons-1); throw new IndiceCodeException(indice, NbJetons-1);
Jeton? jeton = lesJetons[indice]; Jeton? jeton = lesJetons[indice];
if (!jeton.HasValue) if (!jeton.HasValue)
throw new IndiceCodeException(indice, NbJetons-1); throw new IndiceCodeException(indice, NbJetons-1);
return jeton.Value; return jeton.Value;
} }
public IEnumerable<Jeton?> Jetons() public IEnumerable<Jeton?> Jetons()
{ {
return lesJetons; return lesJetons;
} }
public bool EstComplet() public bool EstComplet()
{ {
return NbJetons == lesJetons.Length; return NbJetons == lesJetons.Length;
} }
public int TailleMaximale() public int TailleMaximale()
{ {
return lesJetons.Length; return lesJetons.Length;
} }
public IEnumerable<Indicateur> Comparer(Code autreCode) public IEnumerable<Indicateur> Comparer(Code autreCode)
{ {
// Mon code est le code correct, l'autre code est celui qui teste // Mon code est le code correct, l'autre code est celui qui teste
Indicateur[] indicateurs = []; Indicateur[] indicateurs = [];
if (EstComplet() || !autreCode.EstComplet()) if (!EstComplet() || !autreCode.EstComplet())
return indicateurs; return indicateurs;
Jeton?[] mesJetons = Jetons().ToArray(); Jeton?[] mesJetons = Jetons().ToArray();
Jeton?[] sesJetons = autreCode.Jetons().ToArray(); Jeton?[] sesJetons = autreCode.Jetons().ToArray();
for (int i = 0; i < mesJetons.Length; ++i) for (int i = 0; i < mesJetons.Length; ++i)
{ {
Jeton? monJeton = mesJetons[i]; Jeton? monJeton = mesJetons[i];
Jeton? sonJeton = sesJetons[i]; Jeton? sonJeton = sesJetons[i];
if (monJeton.HasValue && sonJeton.HasValue && monJeton.Value.Couleur.Equals(sonJeton.Value.Couleur)) if (monJeton.HasValue && sonJeton.HasValue && monJeton.Value.Couleur.Equals(sonJeton.Value.Couleur))
{ {
indicateurs = indicateurs.Append(Indicateur.BONNEPLACE).ToArray(); indicateurs = indicateurs.Append(Indicateur.BONNEPLACE).ToArray();
mesJetons[i] = null; mesJetons[i] = null;
sesJetons[i] = null; sesJetons[i] = null;
} }
} }
for (int i = 0; i < sesJetons.Length; ++i) for (int i = 0; i < sesJetons.Length; ++i)
{ {
Jeton? sonJeton = sesJetons[i]; Jeton? sonJeton = sesJetons[i];
if (sonJeton.HasValue) if (sonJeton.HasValue)
{ {
for (int j = 0; j < mesJetons.Length; ++j) for (int j = 0; j < mesJetons.Length; ++j)
{ {
Jeton? monJeton = mesJetons[j]; Jeton? monJeton = mesJetons[j];
if (monJeton.HasValue && sonJeton.Value.Couleur.Equals(monJeton.Value.Couleur)) if (monJeton.HasValue && sonJeton.Value.Couleur.Equals(monJeton.Value.Couleur))
{ {
indicateurs = indicateurs.Append(Indicateur.BONNECOULEUR).ToArray(); indicateurs = indicateurs.Append(Indicateur.BONNECOULEUR).ToArray();
mesJetons[j] = null; mesJetons[j] = null;
sesJetons[i] = null; sesJetons[i] = null;
break; break;
} }
} }
} }
} }
return indicateurs; return indicateurs;
} }
} }
} }

Loading…
Cancel
Save