using CoreLibrary.Exceptions; namespace CoreLibrary { public class Code { private readonly Jeton?[] lesJetons; public int NbJetons { get; private set; } = 0; public Code(int tailleCode) { if(tailleCode <= 0) { throw new TailleCodeException(tailleCode); } lesJetons = new Jeton?[tailleCode]; } public Code(IEnumerable jetons) { if (jetons.Count() == 0) { throw new TailleCodeException(jetons.Count()); } lesJetons = new Jeton?[jetons.Count()]; foreach(Jeton jeton in jetons) { AjouterJeton(jeton); } } public void AjouterJeton(Jeton jeton) { if (NbJetons == TailleMaximale()) { throw new CodeCompletException(); } lesJetons[NbJetons++] = jeton; } public void SupprimerDernierJeton() { if(NbJetons == 0) { throw new CodeVideException(); } lesJetons[--NbJetons] = null; } public Jeton RecupererJeton(int indice) { if(indice < 0 || indice > TailleMaximale()) throw new IndiceCodeException(indice, NbJetons-1); Jeton? jeton = lesJetons[indice]; if (!jeton.HasValue) throw new IndiceCodeException(indice, NbJetons-1); 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 Indicateur[] indicateurs = []; if (EstComplet() || !autreCode.EstComplet()) return 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; } } }