diff --git a/CoreLibrary/Combinaison.cs b/CoreLibrary/Combinaison.cs new file mode 100644 index 0000000..ffedd46 --- /dev/null +++ b/CoreLibrary/Combinaison.cs @@ -0,0 +1,16 @@ +namespace BibliothequeClasses +{ + public abstract class Combinaison + { + + protected static readonly int taillePhysique = 4; + public int TailleLogique { get; private set; } = 0; + private readonly Jeton[] lesJetons = new Jeton[taillePhysique]; + + protected void AjouterJeton(Jeton jeton) => lesJetons[TailleLogique++] = jeton; + + public Jeton GetJeton(int indice) => lesJetons[indice]; + + protected void SupprimerDernierJeton() => --TailleLogique; + } +} diff --git a/CoreLibrary/CombinaisonIndicateur.cs b/CoreLibrary/CombinaisonIndicateur.cs new file mode 100644 index 0000000..59d5073 --- /dev/null +++ b/CoreLibrary/CombinaisonIndicateur.cs @@ -0,0 +1,72 @@ +namespace BibliothequeClasses +{ + public class CombinaisonIndicateur : Combinaison + { + public CombinaisonIndicateur(CombinaisonJoueur combinaisonJoueur, CombinaisonSecrete combinaisonSecrete) + { + if(!combinaisonJoueur.EstComplete()) + { + throw new ArgumentException("Combinaison non complète"); + } + + JetonJoueur?[] lesJetonsJoueur = new JetonJoueur?[combinaisonJoueur.TailleLogique]; + for(int i = 0; i < combinaisonJoueur.TailleLogique; ++i) + { + lesJetonsJoueur[i] = (JetonJoueur) combinaisonJoueur.GetJeton(i); + } + + JetonJoueur?[] lesJetonsSecret = new JetonJoueur?[combinaisonSecrete.TailleLogique]; + for (int i = 0; i < combinaisonSecrete.TailleLogique; ++i) + { + lesJetonsSecret[i] = (JetonJoueur)combinaisonSecrete.GetJeton(i); + } + + for (int i = 0; i < Combinaison.taillePhysique; ++i) + { + JetonJoueur? jetonJoueur = lesJetonsJoueur[i]; + JetonJoueur? jetonSecret = lesJetonsSecret[i]; + + if (jetonJoueur is not null && jetonSecret is not null && ComparerJeton(jetonJoueur, jetonSecret)) + { + AjouterJetonBienPlace(); + lesJetonsJoueur[i] = null; + lesJetonsSecret[i] = null; + } + } + + for (int i = 0; i < Combinaison.taillePhysique; ++i) + { + JetonJoueur? jetonJoueur = lesJetonsJoueur[i]; + + if (jetonJoueur is not null && ContientJeton(jetonJoueur, lesJetonsSecret)) + { + AjouterJetonBonneCouleur(); + lesJetonsJoueur[i] = null; + lesJetonsSecret[i] = null; + } + } + } + + private void AjouterJetonBienPlace() + { + JetonIndicateur jeton = new JetonIndicateur(Couleur.Noir); + base.AjouterJeton(jeton); + } + + private void AjouterJetonBonneCouleur() + { + JetonIndicateur jeton = new JetonIndicateur(Couleur.Blanc); + base.AjouterJeton(jeton); + } + + private bool ComparerJeton(JetonJoueur jetonJoueur, JetonJoueur jetonSecret) + { + return jetonJoueur.Couleur == jetonSecret.Couleur; + } + + private bool ContientJeton(JetonJoueur jetonJoueur, JetonJoueur?[] jetonsSecret) + { + return Array.IndexOf(jetonsSecret, jetonJoueur) != -1; + } + } +} diff --git a/CoreLibrary/CombinaisonJoueur.cs b/CoreLibrary/CombinaisonJoueur.cs new file mode 100644 index 0000000..88ba4d8 --- /dev/null +++ b/CoreLibrary/CombinaisonJoueur.cs @@ -0,0 +1,12 @@ +namespace BibliothequeClasses +{ + public class CombinaisonJoueur : Combinaison + { + + public void AjouterJeton(JetonJoueur jetonJoueur) => base.AjouterJeton(jetonJoueur); + + public new void SupprimerDernierJeton() => base.SupprimerDernierJeton(); + + public bool EstComplete() => base.TailleLogique == Combinaison.taillePhysique; + } +} diff --git a/CoreLibrary/CombinaisonSecrete.cs b/CoreLibrary/CombinaisonSecrete.cs new file mode 100644 index 0000000..9b45ba6 --- /dev/null +++ b/CoreLibrary/CombinaisonSecrete.cs @@ -0,0 +1,47 @@ +namespace BibliothequeClasses +{ + public class CombinaisonSecrete : Combinaison + { + private static readonly Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); + private static readonly Random rand = new Random(); + + public CombinaisonSecrete() + { + for(int i = 0; i < Combinaison.taillePhysique; i++) { + this.AjouterJetonAleatoire(); + } + } + + private void AjouterJetonAleatoire() + { + JetonJoueur jeton = CombinaisonSecrete.GenererJetonAleatoire(); + base.AjouterJeton(jeton); + } + + private static JetonJoueur GenererJetonAleatoire() + { + int indice = rand.Next(couleurs.Length); + Couleur couleur = couleurs[indice]; + + return new JetonJoueur(couleur); + } + + public bool EstEgale(CombinaisonJoueur combinaisonJoueur) + { + if(!combinaisonJoueur.EstComplete()) + { + throw new Exception("Combinaison imcomplete"); + } + + for(int indice = 0; indice < CombinaisonSecrete.taillePhysique; ++indice) + { + if(this.GetJeton(indice).Couleur != combinaisonJoueur.GetJeton(indice).Couleur) + { + return false; + } + } + + return true; + } + } +}