diff --git a/Sources/CoreLibrary/Code.cs b/Sources/CoreLibrary/Code.cs new file mode 100644 index 0000000..29c865f --- /dev/null +++ b/Sources/CoreLibrary/Code.cs @@ -0,0 +1,102 @@ +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 = jetons.ToArray(); + } + + public void AjouterJeton(Jeton jeton) + { + lesJetons[NbJetons++] = jeton; + } + + public void SupprimerDernierJeton() + { + lesJetons[NbJetons--] = null; + } + + public Jeton RecupererJeton(int indice) + { + Jeton? jeton = lesJetons[indice]; + + if (!jeton.HasValue) + throw new Exception(); + + 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 = []; + + 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; + } + } +} diff --git a/Sources/CoreLibrary/Combinaison.cs b/Sources/CoreLibrary/Combinaison.cs deleted file mode 100644 index 9bb0acf..0000000 --- a/Sources/CoreLibrary/Combinaison.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace CoreLibrary -{ - 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/Sources/CoreLibrary/CombinaisonIndicateur.cs b/Sources/CoreLibrary/CombinaisonIndicateur.cs deleted file mode 100644 index 95fc816..0000000 --- a/Sources/CoreLibrary/CombinaisonIndicateur.cs +++ /dev/null @@ -1,72 +0,0 @@ -namespace CoreLibrary -{ - 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/Sources/CoreLibrary/CombinaisonJoueur.cs b/Sources/CoreLibrary/CombinaisonJoueur.cs deleted file mode 100644 index 0ad12e7..0000000 --- a/Sources/CoreLibrary/CombinaisonJoueur.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace CoreLibrary -{ - 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/Sources/CoreLibrary/CombinaisonSecrete.cs b/Sources/CoreLibrary/CombinaisonSecrete.cs deleted file mode 100644 index e22e7b5..0000000 --- a/Sources/CoreLibrary/CombinaisonSecrete.cs +++ /dev/null @@ -1,47 +0,0 @@ -namespace CoreLibrary -{ - 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; - } - } -} diff --git a/Sources/CoreLibrary/Couleur.cs b/Sources/CoreLibrary/Couleur.cs new file mode 100644 index 0000000..214cd87 --- /dev/null +++ b/Sources/CoreLibrary/Couleur.cs @@ -0,0 +1,12 @@ +namespace CoreLibrary +{ + public enum Couleur + { + ROUGE, + VERT, + BLEU, + JAUNE, + BLANC, + NOIR + } +} diff --git a/Sources/CoreLibrary/Couleurs.cs b/Sources/CoreLibrary/Couleurs.cs deleted file mode 100644 index 1963ba9..0000000 --- a/Sources/CoreLibrary/Couleurs.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace CoreLibrary -{ - /// - /// Énumération représentant les 6 couleurs que peuvent prendre les jetons. - /// - public enum Couleur - { - Rouge, - Bleu, - Vert, - Jaune, - Noir = 100, - Blanc, - } -} diff --git a/Sources/CoreLibrary/IRegles.cs b/Sources/CoreLibrary/IRegles.cs index 9b040d2..dfe558b 100644 --- a/Sources/CoreLibrary/IRegles.cs +++ b/Sources/CoreLibrary/IRegles.cs @@ -2,17 +2,24 @@ { public interface IRegles { - void AjouterJoueur(Joueur joueur); - void CommencerPartie(); - bool EstTermine(); - Joueur[] Gagnant(); - Joueur[] Perdant(); - int Tour(); - void JouerCombinaison(CombinaisonJoueur combinaison); - bool EstCombinaisonValide(CombinaisonJoueur combinaison); + string Nom { get; } + + int TourMaximum { get; } + int TailleCodeMaximum { get; } + + int NbJoueurs { get; } + int NbJoueursMaximum { get; } + + void AjouterJoueur(string nom); Joueur JoueurCourant(); - Joueur[] Joueurs(); - CombinaisonJoueur Plateau(); void PasserLaMain(); + + Code GenererCode(); + + void CommencerLaPartie(); + bool EstTerminee(); + + IEnumerable Gagnants(); + IEnumerable Perdants(); } } diff --git a/Sources/CoreLibrary/Indicateur.cs b/Sources/CoreLibrary/Indicateur.cs new file mode 100644 index 0000000..05e3689 --- /dev/null +++ b/Sources/CoreLibrary/Indicateur.cs @@ -0,0 +1,8 @@ +namespace CoreLibrary +{ + public enum Indicateur + { + BONNEPLACE, + BONNECOULEUR + } +} diff --git a/Sources/CoreLibrary/Jeton.cs b/Sources/CoreLibrary/Jeton.cs index deaaf95..b115aa2 100644 --- a/Sources/CoreLibrary/Jeton.cs +++ b/Sources/CoreLibrary/Jeton.cs @@ -1,26 +1,12 @@ -namespace CoreLibrary -{ - /// - /// Représente un jeton de jeu qui permet de récupérer et de modifier sa couleur. - /// - public abstract class Jeton - { - /// - /// Initialise une nouvelle instance de la classe Jeton avec la couleur spécifiée. - /// - /// La couleur du jeton. - protected Jeton(Couleur couleur) - { - this.Couleur = couleur; - } - - /// - /// Obtient la couleur du jeton. - /// - public Couleur Couleur - { - get; - private set; - } - } -} +namespace CoreLibrary +{ + public struct Jeton + { + public readonly Couleur Couleur { get; private init; } + + public Jeton(Couleur couleur) + { + Couleur = couleur; + } + } +} diff --git a/Sources/CoreLibrary/JetonIndicateur.cs b/Sources/CoreLibrary/JetonIndicateur.cs deleted file mode 100644 index 846fb07..0000000 --- a/Sources/CoreLibrary/JetonIndicateur.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace CoreLibrary -{ - /// - /// Représente un jeton indicateur, une classe dérivée de la classe Jeton. - /// - public class JetonIndicateur : Jeton - { - /// - /// Initialise une nouvelle instance de la classe JetonIndicateur avec la couleur spécifiée. - /// - /// La couleur du jeton. - /// Levée si la couleur spécifiée n'est pas Noir ou Blanc. - public JetonIndicateur(Couleur couleur) - : base(couleur) - { - if (couleur < Couleur.Noir) - { - throw new ArgumentException("La couleur doit être Noir ou Blanc"); - } - - - } - } -} - diff --git a/Sources/CoreLibrary/JetonJoueur.cs b/Sources/CoreLibrary/JetonJoueur.cs deleted file mode 100644 index e862382..0000000 --- a/Sources/CoreLibrary/JetonJoueur.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace CoreLibrary -{ - /// - /// Représente un jeton joueur, une classe dérivée de la classe Jeton. - /// - public class JetonJoueur : Jeton - { - /// - /// Initialise une nouvelle instance de la classe JetonJoueur avec la couleur spécifiée. - /// - /// La couleur du jeton. - public JetonJoueur(Couleur couleur) - : base(couleur) - { - - } - } -} - diff --git a/Sources/CoreLibrary/Joueur.cs b/Sources/CoreLibrary/Joueur.cs index f997809..48a1bbb 100644 --- a/Sources/CoreLibrary/Joueur.cs +++ b/Sources/CoreLibrary/Joueur.cs @@ -1,48 +1,14 @@ -namespace CoreLibrary -{ - /// - /// Représente un joueur dans une partie. - /// - public class Joueur - { - private Plateau plateau; - private bool gagne; - - /// - /// Initialise une nouvelle instance de la classe avec le nom spécifié. - /// - /// Le nom du joueur. - public Joueur(string nom) - { - this.Nom = nom; - this.plateau = new Plateau(); - } - - /// - /// Obtient ou définit le nom du joueur. - /// - public string Nom - { - get; - set; - } - - /// - /// Joue une combinaison pour ce joueur sur le plateau. - /// - /// La combinaison à jouer. - public void JouerCombinaison(CombinaisonJoueur combinaisonJoueur) - { - gagne = plateau.AjouterCombinaison(combinaisonJoueur); - } - - /// - /// Détermine si le joueur a gagné la partie. - /// - /// True si le joueur a gagné, sinon False. - public bool AGagne() - { - return gagne; - } - } -} +namespace CoreLibrary +{ + public class Joueur + { + public string Nom { get; private init; } + public Plateau Plateau { get; private init; } + + public Joueur(string nom, Plateau plateau) + { + Nom = nom; + Plateau = plateau; + } + } +} diff --git a/Sources/CoreLibrary/Partie.cs b/Sources/CoreLibrary/Partie.cs deleted file mode 100644 index ef6c3a0..0000000 --- a/Sources/CoreLibrary/Partie.cs +++ /dev/null @@ -1,135 +0,0 @@ -namespace CoreLibrary -{ - internal class Partie : IRegles - { - private static readonly int maximumJoueur = 2; - private static readonly int maximumTour = 12; - - private int? indice; - private readonly Joueur[] joueurs = new Joueur[maximumJoueur]; - - private int tour = 1; - - public void AjouterJoueur(Joueur joueur) - { - if(joueurs.Length >= maximumJoueur) - { - throw new Exception("Nombre de joueurs maximum atteint"); - } - - joueurs.Append(joueur); - } - - public void CommencerPartie() - { - indice = 0; - } - - public bool EstCombinaisonValide(CombinaisonJoueur combinaison) - { - return combinaison.EstComplete(); - } - - public bool EstTermine() - { - if (!indice.HasValue || indice != 0) - return false; - - if (tour > maximumTour) - return true; - - for (int i = 0; i < joueurs.Length; ++i) - { - if (joueurs[i].AGagne()) - return true; - } - - return false; - } - - public Joueur[] Gagnant() - { - Joueur[] gagnants = new Joueur[joueurs.Length]; - - if (!indice.HasValue) - throw new Exception("Partie non démarrée"); - - if (!EstTermine()) - throw new Exception("Partie non terminée"); - - for (int i = 0; i < joueurs.Length; ++i) - { - if (joueurs[i].AGagne()) - gagnants.Append(joueurs[i]); - } - - return gagnants; - } - - public void JouerCombinaison(CombinaisonJoueur combinaison) - { - if (!indice.HasValue) - throw new Exception("Partie non démarrée"); - - if (!EstCombinaisonValide(combinaison)) - throw new Exception("Combinaison n'est pas valide"); - - joueurs[indice.Value].JouerCombinaison(combinaison); - } - - public Joueur JoueurCourant() - { - if (!indice.HasValue) - throw new Exception("Partie non démarrée"); - - return joueurs[indice.Value]; - } - - public Joueur[] Joueurs() - { - return joueurs; - } - - public void PasserLaMain() - { - if (!indice.HasValue) - throw new Exception("Partie non démarrée"); - - ++indice; - if (indice >= joueurs.Length) - { - ++tour; - indice = 0; - } - } - - public Joueur[] Perdant() - { - if (!indice.HasValue) - throw new Exception("Partie non démarrée"); - - if (!EstTermine()) - throw new Exception("Partie non terminée"); - - Joueur[] perdants = new Joueur[joueurs.Length]; - - for (int i = 0; i < joueurs.Length; ++i) - { - if (!joueurs[i].AGagne()) - perdants.Append(joueurs[i]); - } - - return perdants; - } - - public CombinaisonJoueur Plateau() - { - throw new NotImplementedException(); - } - - public int Tour() - { - return tour; - } - } -} diff --git a/Sources/CoreLibrary/Plateau.cs b/Sources/CoreLibrary/Plateau.cs index e762345..2017fa7 100644 --- a/Sources/CoreLibrary/Plateau.cs +++ b/Sources/CoreLibrary/Plateau.cs @@ -1,43 +1,96 @@ -namespace CoreLibrary -{ - - /// - /// Représente le plateau de jeu qui initialise deux tableaux de taille 12. - /// Il possède deux méthodes : une pour ajouter une combinaison dans le tableau et une autre pour vérifier si le tableau est plein. - /// - public class Plateau - { - private static readonly int tailleMax = 12; - private CombinaisonSecrete combinaisonSecrete = new CombinaisonSecrete(); - private CombinaisonJoueur[] lesCombinaisonsJoueur = new CombinaisonJoueur[tailleMax]; - private Combinaison[] lesCombinaisonsIndicateur = new CombinaisonIndicateur[tailleMax]; - private int index = 0; - - /// - /// Ajoute une combinaison de joueur au plateau. - /// - /// La combinaison du joueur à ajouter. - /// True si la combinaison correspond à la combinaison secrète, sinon False. - public bool AjouterCombinaison(CombinaisonJoueur combinaisonJoueur) - { - if (EstComplet()) - { - throw new Exception("Le plateau est plein, impossible d'ajouter une combinaison supplémentaire."); - } - - lesCombinaisonsJoueur[index] = combinaisonJoueur; - index++; - return combinaisonSecrete.EstEgale(combinaisonJoueur); - } - - /// - /// Vérifie si le plateau est complet. - /// - /// True si le plateau est plein, sinon False. - public bool EstComplet() - { - return index >= tailleMax; - } - } -} - +namespace CoreLibrary +{ + public class Plateau + { + private static Random random = new Random(); + + private readonly Code codeSecret; + private readonly Code?[] grille; + private readonly IEnumerable[] indicateurs; + + private readonly int tailleCode; + + public int Tour { get; private set; } = 1; + public bool Victoire { get; private set; } = false; + + public Plateau(int tailleCode, int tailleGrille) + { + codeSecret = new Code(tailleCode); + grille = new Code?[tailleGrille]; + indicateurs = new IEnumerable[tailleGrille]; + + this.tailleCode = tailleCode; + + GenererCodeAleatoire(); + + foreach (Jeton? jeton in codeSecret.Jetons()) + { + Console.WriteLine(jeton.Value.Couleur); + } + } + + private void GenererCodeAleatoire() + { + Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); + + for (int i = 0; i < tailleCode; ++i) + { + codeSecret.AjouterJeton(new Jeton(couleurs[random.Next(couleurs.Length)])); + } + } + + public bool EstComplet() + { + return Tour - 1 == grille.Length; + } + + public void AjouterCode(Code code) + { + indicateurs[Tour - 1] = codeSecret.Comparer(code); + grille[Tour - 1] = code; + ++Tour; + + if (EstBonCode(code)) + { + Victoire = true; + } + } + + public bool EstBonCode(Code code) + { + IEnumerable indicateurs = codeSecret.Comparer(code); + + if (indicateurs.Count() != tailleCode) + { + return false; + } + + foreach (Indicateur indicateur in indicateurs) + { + if (indicateur != Indicateur.BONNEPLACE) + { + return false; + } + } + + return true; + } + + public IEnumerable> Grille() + { + IEnumerable[] grilleJetons = new IEnumerable[grille.Length]; + + for (int i = 0; i < grille.Length; ++i) + { + grilleJetons[i] = grille[i]?.Jetons() ?? new Code(tailleCode).Jetons(); + } + + return grilleJetons; + } + + public IEnumerable> Indicateurs() + { + return indicateurs; + } + } +} diff --git a/Sources/CoreLibrary/ReglesClassiques.cs b/Sources/CoreLibrary/ReglesClassiques.cs new file mode 100644 index 0000000..632c259 --- /dev/null +++ b/Sources/CoreLibrary/ReglesClassiques.cs @@ -0,0 +1,109 @@ +namespace CoreLibrary +{ + public class ReglesClassiques : IRegles + { + private int nbJoueurs = 0; + private int? joueurCourant; + private readonly Joueur[] joueurs; + + public string Nom { get => "Règles classiques"; } + + public int TourMaximum { get => 12; } + public int TailleCodeMaximum { get => 4; } + + public int NbJoueurs { get => nbJoueurs; } + public int NbJoueursMaximum { get => 2; } + + + public ReglesClassiques() + { + joueurs = new Joueur[NbJoueursMaximum]; + } + + + public void AjouterJoueur(string nom) + { + joueurs[nbJoueurs++] = new Joueur(nom, new Plateau(TailleCodeMaximum, TourMaximum)); + } + + public Joueur JoueurCourant() + { + if (!joueurCourant.HasValue) + throw new Exception(); + + return joueurs[joueurCourant.Value]; + } + + public void PasserLaMain() + { + if (!joueurCourant.HasValue) + throw new Exception(); + + joueurCourant++; + if (joueurCourant >= joueurs.Length) + { + joueurCourant = 0; + } + + } + + + public Code GenererCode() + { + return new Code(TailleCodeMaximum); + } + + + public void CommencerLaPartie() + { + joueurCourant = 0; + } + + + public bool EstTerminee() + { + if (!joueurCourant.HasValue || joueurCourant != 0) + return false; + + if (JoueurCourant().Plateau.Tour > TourMaximum) + return true; + + for (int i = 0; i < joueurs.Length; ++i) + { + if (joueurs[i].Plateau.Victoire) + return true; + } + + return false; + } + + + public IEnumerable Gagnants() + { + Joueur[] gagnants = []; + for (int i = 0; i < joueurs.Length; ++i) + { + if (joueurs[i].Plateau.Victoire) + { + gagnants = gagnants.Append(joueurs[i]).ToArray(); + } + } + + return gagnants; + } + + public IEnumerable Perdants() + { + Joueur[] perdants = []; + for (int i = 0; i < joueurs.Length; ++i) + { + if (!joueurs[i].Plateau.Victoire) + { + perdants = perdants.Append(joueurs[i]).ToArray(); + } + } + + return perdants; + } + } +}