diff --git a/Sources/CoreLibrary/Code.cs b/Sources/CoreLibrary/Code.cs index d48f42d..4c00021 100644 --- a/Sources/CoreLibrary/Code.cs +++ b/Sources/CoreLibrary/Code.cs @@ -1,117 +1,117 @@ -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 jetons) - { - lesJetons = new Jeton?[NbJetons]; - 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--] = null; - } - - 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 Jetons() - { - return lesJetons; - } - - public bool EstComplet() - { - return NbJetons == lesJetons.Length; - } - - public int TailleMaximale() - { - return lesJetons.Length; - } - - public IEnumerable 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; - } - } -} - - +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 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--] = null; + } + + 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 Jetons() + { + return lesJetons; + } + + public bool EstComplet() + { + return NbJetons == lesJetons.Length; + } + + public int TailleMaximale() + { + return lesJetons.Length; + } + + public IEnumerable 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; + } + } +} + +