From b0a2d916d43fde7e783d5c07b1d02b67f60d7efc Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Mon, 6 May 2024 17:44:27 +0200 Subject: [PATCH 1/6] =?UTF-8?q?D=C3=A9but=20de=20la=20classe=20Partie=20et?= =?UTF-8?q?=20des=20=C3=A9venments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/ConsoleApp/Program.cs | 88 ------------------- Sources/CoreLibrary/CoreLibrary.csproj | 4 + .../CodeIndiceHorsDePorteeException.cs | 2 +- .../CodeJetonNullException.cs | 0 .../CodeTableauLesJetonsCompletException.cs | 0 .../CodeTableauLesJetonsIncompletException.cs | 0 .../CodeTableauLesJetonsVideException.cs | 0 .../PlateauCodeIncompletException.cs | 0 .../PlateauTailleCodeException.cs | 0 .../PlateauTailleCodeIncompleteException.cs | 0 .../PlateauTailleGrilleException.cs | 0 .../ReglesClassiquesJoueurCourantNull.cs | 0 Sources/CoreLibrary/Partie.cs | 42 +++++++++ 13 files changed, 47 insertions(+), 89 deletions(-) rename Sources/CoreLibrary/{ => Exceptions}/CodeIndiceHorsDePorteeException.cs (90%) rename Sources/CoreLibrary/{ => Exceptions}/CodeJetonNullException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/CodeTableauLesJetonsCompletException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/CodeTableauLesJetonsIncompletException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/CodeTableauLesJetonsVideException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/PlateauCodeIncompletException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/PlateauTailleCodeException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/PlateauTailleCodeIncompleteException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/PlateauTailleGrilleException.cs (100%) rename Sources/CoreLibrary/{ => Exceptions}/ReglesClassiquesJoueurCourantNull.cs (100%) create mode 100644 Sources/CoreLibrary/Partie.cs diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index 475a617..e69de29 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -1,88 +0,0 @@ -using CoreLibrary; -using ConsoleApp; - - - -Console.OutputEncoding = System.Text.Encoding.UTF8; - -ReglesClassiques partie = new ReglesClassiques(); - -Utils.AfficherTitre(); - -Utils.AfficherTitre("Joueurs"); - -string joueur1; -string joueur2; - -while (true) -{ - try - { - joueur1 = Utils.SaisirNom(); - break; - } - catch(UtilsNomJoueurNullException) - { - Console.WriteLine("Nom invalide pour le joueur ! Ressaisir le nom"); - } -} - -while (true) -{ - try - { - joueur2 = Utils.SaisirNom(); - break; - } - catch (UtilsNomJoueurNullException) - { - Console.WriteLine("Nom invalide pour le joueur 2 ! Ressaisir le nom"); - } -} - -Utils.AfficherSeparateur(); - -partie.AjouterJoueur(joueur1); -partie.AjouterJoueur(joueur2); - -partie.CommencerLaPartie(); - -while (!partie.EstTerminee()) -{ - Console.WriteLine(partie.JoueurCourant().Nom); - Console.WriteLine(); - - Utils.DessinerPlateau(partie.JoueurCourant().Plateau.Grille(), partie.JoueurCourant().Plateau.Indicateurs()); - Console.WriteLine(); - - Code code = partie.GenererCode(); - - Utils.ChoixCode(ref code); - - partie.JoueurCourant().Plateau.AjouterCode(code); - - partie.PasserLaMain(); - - Utils.AfficherSeparateur(); -} - -Console.WriteLine("La partie est maintenant terminιe !"); - -Joueur[] gagnants = partie.Gagnants().ToArray(); -Joueur[] perdants = partie.Perdants().ToArray(); - - - -if (gagnants.Length > 1) -{ - Console.WriteLine("C'est une ιgalitι !"); -} -else if (gagnants.Length == 1) -{ - Console.WriteLine($"C'est une victoire de {gagnants[0].Nom}." + - $""); -} -else -{ - Console.WriteLine("C'est une dιfaite..."); -} diff --git a/Sources/CoreLibrary/CoreLibrary.csproj b/Sources/CoreLibrary/CoreLibrary.csproj index fa71b7a..9ff0d50 100644 --- a/Sources/CoreLibrary/CoreLibrary.csproj +++ b/Sources/CoreLibrary/CoreLibrary.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs b/Sources/CoreLibrary/Exceptions/CodeIndiceHorsDePorteeException.cs similarity index 90% rename from Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs rename to Sources/CoreLibrary/Exceptions/CodeIndiceHorsDePorteeException.cs index 36c0929..6615035 100644 --- a/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs +++ b/Sources/CoreLibrary/Exceptions/CodeIndiceHorsDePorteeException.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CoreLibrary +namespace CoreLibrary.Excpetion { public class CodeIndiceHorsDePorteeException : Exception { diff --git a/Sources/CoreLibrary/CodeJetonNullException.cs b/Sources/CoreLibrary/Exceptions/CodeJetonNullException.cs similarity index 100% rename from Sources/CoreLibrary/CodeJetonNullException.cs rename to Sources/CoreLibrary/Exceptions/CodeJetonNullException.cs diff --git a/Sources/CoreLibrary/CodeTableauLesJetonsCompletException.cs b/Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsCompletException.cs similarity index 100% rename from Sources/CoreLibrary/CodeTableauLesJetonsCompletException.cs rename to Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsCompletException.cs diff --git a/Sources/CoreLibrary/CodeTableauLesJetonsIncompletException.cs b/Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsIncompletException.cs similarity index 100% rename from Sources/CoreLibrary/CodeTableauLesJetonsIncompletException.cs rename to Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsIncompletException.cs diff --git a/Sources/CoreLibrary/CodeTableauLesJetonsVideException.cs b/Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsVideException.cs similarity index 100% rename from Sources/CoreLibrary/CodeTableauLesJetonsVideException.cs rename to Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsVideException.cs diff --git a/Sources/CoreLibrary/PlateauCodeIncompletException.cs b/Sources/CoreLibrary/Exceptions/PlateauCodeIncompletException.cs similarity index 100% rename from Sources/CoreLibrary/PlateauCodeIncompletException.cs rename to Sources/CoreLibrary/Exceptions/PlateauCodeIncompletException.cs diff --git a/Sources/CoreLibrary/PlateauTailleCodeException.cs b/Sources/CoreLibrary/Exceptions/PlateauTailleCodeException.cs similarity index 100% rename from Sources/CoreLibrary/PlateauTailleCodeException.cs rename to Sources/CoreLibrary/Exceptions/PlateauTailleCodeException.cs diff --git a/Sources/CoreLibrary/PlateauTailleCodeIncompleteException.cs b/Sources/CoreLibrary/Exceptions/PlateauTailleCodeIncompleteException.cs similarity index 100% rename from Sources/CoreLibrary/PlateauTailleCodeIncompleteException.cs rename to Sources/CoreLibrary/Exceptions/PlateauTailleCodeIncompleteException.cs diff --git a/Sources/CoreLibrary/PlateauTailleGrilleException.cs b/Sources/CoreLibrary/Exceptions/PlateauTailleGrilleException.cs similarity index 100% rename from Sources/CoreLibrary/PlateauTailleGrilleException.cs rename to Sources/CoreLibrary/Exceptions/PlateauTailleGrilleException.cs diff --git a/Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs b/Sources/CoreLibrary/Exceptions/ReglesClassiquesJoueurCourantNull.cs similarity index 100% rename from Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs rename to Sources/CoreLibrary/Exceptions/ReglesClassiquesJoueurCourantNull.cs diff --git a/Sources/CoreLibrary/Partie.cs b/Sources/CoreLibrary/Partie.cs new file mode 100644 index 0000000..ecb4b1c --- /dev/null +++ b/Sources/CoreLibrary/Partie.cs @@ -0,0 +1,42 @@ +ο»Ώnamespace CoreLibrary +{ + public class Partie + { + private IRegles regles; + + public Partie(IRegles regles) + { + this.regles = regles; + } + + public void Jouer() + { + DefinirJoueurs(); + + regles.CommencerLaPartie(); + + while (!regles.EstTerminee()) + { + // ?? + } + + PartieTerminee(); + + } + + private void DefinirJoueurs() + { + while(regles.NbJoueurs != regles.NbJoueursMaximum) + { + // + } + } + + private void PartieTerminee() + { + + } + + + } +} From 33c2b4ff03bba073989fb096035c00fe91407ba5 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Mon, 6 May 2024 18:31:45 +0200 Subject: [PATCH 2/6] Classe Partie --- Sources/ConsoleApp/Program.cs | 4 ++++ Sources/CoreLibrary/Code.cs | 2 -- Sources/CoreLibrary/Partie.cs | 25 ++++++++++++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index e69de29..3c85a5e 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -0,0 +1,4 @@ +using CoreLibrary; + +Partie maPartie = new Partie(new ReglesClassiques()); +maPartie.Jouer(); diff --git a/Sources/CoreLibrary/Code.cs b/Sources/CoreLibrary/Code.cs index c23f278..27bf7d4 100644 --- a/Sources/CoreLibrary/Code.cs +++ b/Sources/CoreLibrary/Code.cs @@ -37,8 +37,6 @@ public Jeton RecupererJeton(int indice) { - if(indice < 0 || indice > TailleMaximale()) - throw new CodeIndiceHorsDePorteeException(); Jeton? jeton = lesJetons[indice]; diff --git a/Sources/CoreLibrary/Partie.cs b/Sources/CoreLibrary/Partie.cs index ecb4b1c..fb61c2c 100644 --- a/Sources/CoreLibrary/Partie.cs +++ b/Sources/CoreLibrary/Partie.cs @@ -17,24 +17,43 @@ while (!regles.EstTerminee()) { - // ?? + Joueur joueurCourant = regles.JoueurCourant(); + Plateau plateauCourant = joueurCourant.Plateau; + + Code code = regles.GenererCode(); + + while (!code.EstComplet()) + { + AjouterJeton(code); + } + plateauCourant.AjouterCode(code); + + regles.PasserLaMain(); } PartieTerminee(); } + private void AjouterJeton(Code code) + { + Jeton jeton = new Jeton(Couleur.ROUGE); + code.AjouterJeton(jeton); + } + private void DefinirJoueurs() { while(regles.NbJoueurs != regles.NbJoueursMaximum) { - // + string nom = "pauline"; + regles.AjouterJoueur(nom); } } private void PartieTerminee() { - + regles.Gagnants(); + regles.Perdants(); } From 6645c50a8eeed222038ceb854a3f58e395b9f7b9 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Mon, 6 May 2024 19:31:03 +0200 Subject: [PATCH 3/6] =?UTF-8?q?ajout=20des=20=C3=A9v=C3=A9nements=20dans?= =?UTF-8?q?=20partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/ConsoleApp/Program.cs | 9 +++++ Sources/CoreLibrary/CoreLibrary.csproj | 4 -- .../Events/AjouterCodeEventArgs.cs | 12 ++++++ .../Events/AjouterJetonEventArgs.cs | 13 +++++++ .../Events/AjouterJoueursEventArgs.cs | 12 ++++++ .../Events/DebutPartieEventArgs.cs | 10 +++++ .../Events/NouveauTourEventArgs.cs | 14 +++++++ .../Events/PartieTermineeEventArgs.cs | 20 ++++++++++ .../CoreLibrary/Events/PasserMainEventArgs.cs | 13 +++++++ Sources/CoreLibrary/Partie.cs | 37 ++++++++++++++++--- 10 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 Sources/CoreLibrary/Events/AjouterCodeEventArgs.cs create mode 100644 Sources/CoreLibrary/Events/AjouterJetonEventArgs.cs create mode 100644 Sources/CoreLibrary/Events/AjouterJoueursEventArgs.cs create mode 100644 Sources/CoreLibrary/Events/DebutPartieEventArgs.cs create mode 100644 Sources/CoreLibrary/Events/NouveauTourEventArgs.cs create mode 100644 Sources/CoreLibrary/Events/PartieTermineeEventArgs.cs create mode 100644 Sources/CoreLibrary/Events/PasserMainEventArgs.cs diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index 3c85a5e..a43a2ae 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -1,4 +1,13 @@ using CoreLibrary; +using CoreLibrary.Events; + +void LaPartieDemarre(object sender, DebutPartieEventArgs evenement) +{ + Console.WriteLine("La partie commence"); +} + Partie maPartie = new Partie(new ReglesClassiques()); +maPartie.debutPartie += LaPartieDemarre; + maPartie.Jouer(); diff --git a/Sources/CoreLibrary/CoreLibrary.csproj b/Sources/CoreLibrary/CoreLibrary.csproj index 9ff0d50..fa71b7a 100644 --- a/Sources/CoreLibrary/CoreLibrary.csproj +++ b/Sources/CoreLibrary/CoreLibrary.csproj @@ -6,8 +6,4 @@ enable - - - - diff --git a/Sources/CoreLibrary/Events/AjouterCodeEventArgs.cs b/Sources/CoreLibrary/Events/AjouterCodeEventArgs.cs new file mode 100644 index 0000000..3cd1c56 --- /dev/null +++ b/Sources/CoreLibrary/Events/AjouterCodeEventArgs.cs @@ -0,0 +1,12 @@ +ο»Ώ +namespace CoreLibrary.Events +{ + public class AjouterCodeEventArgs : EventArgs + { + public Code Code { get; private set; } + public AjouterCodeEventArgs(Code code) + { + Code = code; + } + } +} diff --git a/Sources/CoreLibrary/Events/AjouterJetonEventArgs.cs b/Sources/CoreLibrary/Events/AjouterJetonEventArgs.cs new file mode 100644 index 0000000..50b35ab --- /dev/null +++ b/Sources/CoreLibrary/Events/AjouterJetonEventArgs.cs @@ -0,0 +1,13 @@ +ο»Ώ + +namespace CoreLibrary.Events +{ + public class AjouterJetonEventArgs : EventArgs + { + public Jeton Jeton { get; private set; } + public AjouterJetonEventArgs(Jeton jeton) + { + Jeton = jeton; + } + } +} diff --git a/Sources/CoreLibrary/Events/AjouterJoueursEventArgs.cs b/Sources/CoreLibrary/Events/AjouterJoueursEventArgs.cs new file mode 100644 index 0000000..4fb2ddb --- /dev/null +++ b/Sources/CoreLibrary/Events/AjouterJoueursEventArgs.cs @@ -0,0 +1,12 @@ +ο»Ώnamespace CoreLibrary.Events +{ + public class AjouterJoueursEventArgs : EventArgs + { + public Joueur Joueur { get; private set; } + + public AjouterJoueursEventArgs(Joueur joueur) + { + Joueur = joueur; + } + } +} diff --git a/Sources/CoreLibrary/Events/DebutPartieEventArgs.cs b/Sources/CoreLibrary/Events/DebutPartieEventArgs.cs new file mode 100644 index 0000000..93745af --- /dev/null +++ b/Sources/CoreLibrary/Events/DebutPartieEventArgs.cs @@ -0,0 +1,10 @@ +ο»Ώnamespace CoreLibrary.Events +{ + public class DebutPartieEventArgs : EventArgs + { + + public DebutPartieEventArgs() + { + } + } +} diff --git a/Sources/CoreLibrary/Events/NouveauTourEventArgs.cs b/Sources/CoreLibrary/Events/NouveauTourEventArgs.cs new file mode 100644 index 0000000..a6e297f --- /dev/null +++ b/Sources/CoreLibrary/Events/NouveauTourEventArgs.cs @@ -0,0 +1,14 @@ +ο»Ώnamespace CoreLibrary.Events +{ + public class NouveauTourEventArgs : EventArgs + { + public Joueur Joueur { get; private set; } + public int Tour { get; private set; } + + public NouveauTourEventArgs(Joueur joueur, int tour) + { + Joueur = joueur; + Tour = tour; + } + } +} \ No newline at end of file diff --git a/Sources/CoreLibrary/Events/PartieTermineeEventArgs.cs b/Sources/CoreLibrary/Events/PartieTermineeEventArgs.cs new file mode 100644 index 0000000..094073c --- /dev/null +++ b/Sources/CoreLibrary/Events/PartieTermineeEventArgs.cs @@ -0,0 +1,20 @@ +ο»Ώusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CoreLibrary.Events +{ + public class PartieTermineeEventArgs : EventArgs + { + public IEnumerable Gagnants { get; private set; } + public IEnumerable Perdants { get; private set; } + + public PartieTermineeEventArgs(IEnumerable gagnants, IEnumerable perdants) + { + Gagnants = gagnants; + Perdants = perdants; + } + } +} diff --git a/Sources/CoreLibrary/Events/PasserMainEventArgs.cs b/Sources/CoreLibrary/Events/PasserMainEventArgs.cs new file mode 100644 index 0000000..30999ad --- /dev/null +++ b/Sources/CoreLibrary/Events/PasserMainEventArgs.cs @@ -0,0 +1,13 @@ +ο»Ώ +using System.Security.Cryptography.X509Certificates; + +namespace CoreLibrary.Events +{ + public class PasserMainEventArgs : EventArgs + { + public PasserMainEventArgs() + { + + } + } +} diff --git a/Sources/CoreLibrary/Partie.cs b/Sources/CoreLibrary/Partie.cs index fb61c2c..41a2bd9 100644 --- a/Sources/CoreLibrary/Partie.cs +++ b/Sources/CoreLibrary/Partie.cs @@ -1,9 +1,28 @@ -ο»Ώnamespace CoreLibrary +ο»Ώusing CoreLibrary.Events; + +namespace CoreLibrary { public class Partie { private IRegles regles; + public event EventHandler ajouterJoueur; + public event EventHandler debutPartie; + public event EventHandler nouveauTour; + public event EventHandler ajouterJeton; + public event EventHandler ajouterCode; + public event EventHandler passerMain; + public event EventHandler partieTerminee; + + private void QuandAjouterJoueur(Joueur joueur) => ajouterJoueur?.Invoke(this, new AjouterJoueursEventArgs(joueur)); + private void QuandDebutPartie() => debutPartie?.Invoke(this, new DebutPartieEventArgs()); + private void QuandNouveauTour(Joueur joueur, int tour) => nouveauTour?.Invoke(this, new NouveauTourEventArgs(joueur, tour)); + private void QuandNouveauJeton(Jeton jeton) => ajouterJeton?.Invoke(this, new AjouterJetonEventArgs(jeton)); + private void QuandNouveauCode(Code code) => ajouterCode?.Invoke(this, new AjouterCodeEventArgs(code)); + private void QuandPasserMain() => passerMain?.Invoke(this, new PasserMainEventArgs()); + private void QuandPartieTerminee(IEnumerable gagnants, IEnumerable perdants) => partieTerminee?.Invoke(this, new PartieTermineeEventArgs(gagnants, perdants)); + + public Partie(IRegles regles) { this.regles = regles; @@ -11,34 +30,41 @@ public void Jouer() { - DefinirJoueurs(); + DefinirJoueurs(); regles.CommencerLaPartie(); + QuandDebutPartie(); while (!regles.EstTerminee()) { Joueur joueurCourant = regles.JoueurCourant(); - Plateau plateauCourant = joueurCourant.Plateau; + Plateau plateauCourant = joueurCourant.Plateau; + + QuandNouveauTour(joueurCourant, plateauCourant.Tour); Code code = regles.GenererCode(); while (!code.EstComplet()) { - AjouterJeton(code); + AjouterJeton(code); + } plateauCourant.AjouterCode(code); + QuandNouveauCode(code); regles.PasserLaMain(); + QuandPasserMain(); } PartieTerminee(); - + QuandPartieTerminee(regles.Gagnants(), regles.Perdants()); } private void AjouterJeton(Code code) { Jeton jeton = new Jeton(Couleur.ROUGE); code.AjouterJeton(jeton); + QuandNouveauJeton(jeton); } private void DefinirJoueurs() @@ -47,6 +73,7 @@ { string nom = "pauline"; regles.AjouterJoueur(nom); + //QuandAjouterJoueur() } } From 78c938384e5b40a5c19c631ffdc3753a32f3b3d1 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Fri, 10 May 2024 14:00:43 +0200 Subject: [PATCH 4/6] Partie consoleapp --- Sources/ConsoleApp/Program.cs | 22 +- Sources/ConsoleApp/Utils.cs | 233 +++++++++--------- .../ConsoleApp/UtilsNomJoueurNullException.cs | 14 -- .../Events/DebutPartieEventArgs.cs | 4 - .../Events/DemanderJetonEventArgs.cs | 7 + .../Events/DemanderJoueurEventArgs.cs | 12 + .../Events/NouveauTourEventArgs.cs | 10 +- .../CoreLibrary/Events/PasserMainEventArgs.cs | 9 +- Sources/CoreLibrary/Partie.cs | 80 +++--- 9 files changed, 194 insertions(+), 197 deletions(-) delete mode 100644 Sources/ConsoleApp/UtilsNomJoueurNullException.cs create mode 100644 Sources/CoreLibrary/Events/DemanderJetonEventArgs.cs create mode 100644 Sources/CoreLibrary/Events/DemanderJoueurEventArgs.cs diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index a43a2ae..c7dc2de 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -1,13 +1,21 @@ +using ConsoleApp; using CoreLibrary; -using CoreLibrary.Events; -void LaPartieDemarre(object sender, DebutPartieEventArgs evenement) -{ - Console.WriteLine("La partie commence"); -} +Console.OutputEncoding = System.Text.Encoding.UTF8; + +Utils.DessinerTitre(); Partie maPartie = new Partie(new ReglesClassiques()); -maPartie.debutPartie += LaPartieDemarre; -maPartie.Jouer(); +maPartie.DemanderJoueur += Utils.DemanderJoueur; + +maPartie.DebutPartie += Utils.CommencerLaPartie; +maPartie.NouveauTour += Utils.NouveauTour; +maPartie.DemanderJeton += Utils.DemanderJeton; +maPartie.AjouterJeton += Utils.AjouterJeton; +maPartie.AjouterCode += Utils.AjouterCode; +maPartie.PartieTerminee += Utils.PartieTerminee; + + +maPartie.Jouer(); \ No newline at end of file diff --git a/Sources/ConsoleApp/Utils.cs b/Sources/ConsoleApp/Utils.cs index 76b1885..d6157ee 100644 --- a/Sources/ConsoleApp/Utils.cs +++ b/Sources/ConsoleApp/Utils.cs @@ -1,31 +1,30 @@ ο»Ώusing CoreLibrary; +using CoreLibrary.Events; +using System.Collections.Generic; +using System.Reflection; namespace ConsoleApp { - internal class Utils + public class Utils { - private static int nombreJoueurs = 0; - - private readonly static int longueurTitre = 55; - - private readonly static Dictionary convertirCouleurs = new Dictionary() + private readonly static Dictionary couleursTerminal = new Dictionary() { {Couleur.NOIR, ConsoleColor.Black }, {Couleur.BLANC, ConsoleColor.White }, {Couleur.ROUGE, ConsoleColor.Red }, {Couleur.VERT, ConsoleColor.Green }, {Couleur.BLEU, ConsoleColor.Blue }, - {Couleur.JAUNE, ConsoleColor.Yellow } + {Couleur.JAUNE, ConsoleColor.DarkYellow } }; - private readonly static Dictionary convertirIndicateurs = new Dictionary() + + private readonly static Dictionary indicateursTerminal = new Dictionary() { {Indicateur.BONNEPLACE, ConsoleColor.Black }, {Indicateur.BONNECOULEUR, ConsoleColor.White } }; - public static void AfficherTitre() + public static void DessinerTitre() { - Console.WriteLine(""" __ __ _ _ _ | \/ | __ _ ___| |_ ___ _ _ _ __ (_) _ _ __| | @@ -33,50 +32,28 @@ namespace ConsoleApp |_| |_|\__,_|/__/ \__|\___||_| |_|_|_||_||_||_|\__,_| """); - AfficherSeparateur(); + DessinerSeparateur(); } + - public static void AfficherTitre(string titre) - { - int taille = longueurTitre - titre.Length; - int gauche = taille > 0 ? taille / 2 : 0; - - Console.Write(titre.PadLeft(titre.Length + gauche)); - AfficherSeparateur(); - } - - public static void AfficherSeparateur() + public static void DessinerSeparateur() { Console.WriteLine(""" - _______________________________________________________ + ─────────────────────────────────────────────────────── """); } - public static string SaisirNom() - { - string nom = $"Joueur {++nombreJoueurs}"; - - Console.WriteLine(nom); - Console.Write(">>> "); - nom = Console.ReadLine() ?? nom; - if (nom == "") - { - --nombreJoueurs; - throw new UtilsNomJoueurNullException(); - } - Console.WriteLine(); - - return nom; - } - - public static void DessinerJeton(Jeton jeton) + public static void DessinerPion(Enum pion) { Console.Write(" "); - Console.ForegroundColor = convertirCouleurs.GetValueOrDefault(jeton.Couleur); + Console.ForegroundColor = pion.GetType().Equals(typeof(Couleur)) ? + couleursTerminal.GetValueOrDefault((Couleur) pion) : + indicateursTerminal.GetValueOrDefault((Indicateur) pion); + Console.BackgroundColor = Console.ForegroundColor.Equals(ConsoleColor.Black) ? ConsoleColor.White : ConsoleColor.Black; Console.Write("⬀"); @@ -86,97 +63,113 @@ namespace ConsoleApp Console.Write(" "); } - public static void DessinerIndicateur(Indicateur indicateur) + public static void DessinerPlateau(IEnumerable> grille, IEnumerable> indicateurs) { - Console.Write(" "); + IEnumerable[] grilleTableau = (IEnumerable[]) grille.ToArray(); + IEnumerable[] indicateursTableau = (IEnumerable[]) indicateurs.ToArray(); - Console.ForegroundColor = convertirIndicateurs.GetValueOrDefault(indicateur); - Console.BackgroundColor = Console.ForegroundColor.Equals(ConsoleColor.Black) ? ConsoleColor.White : ConsoleColor.Black; + Console.WriteLine(" Codes Indicateurs "); + Console.WriteLine("──────────────── ────────────────"); + Console.WriteLine("β”‚ β”‚ β”‚ β”‚"); - Console.Write("⬀"); + for (int i = 0; i < grille.Count(); ++i) + { + Console.Write("β”‚ "); - Console.ResetColor(); + Jeton?[] ligneGrille = grilleTableau[i].ToArray(); - Console.Write(" "); - } + for (int j = 0; j < ligneGrille.Length; ++j) + { + Jeton? jeton = ligneGrille[j]; - public static void DessinerCode(IEnumerable jetons) - { - foreach (Jeton? jeton in jetons) - { - if (jeton.HasValue) + if(jeton.HasValue) + { + DessinerPion(jeton.Value.Couleur); + } + else + { + Console.Write(" "); + } + + } + + Console.Write(" β”‚ β”‚ "); + + if(indicateursTableau[i] != null) { - DessinerJeton(jeton.Value); + Indicateur[] ligneIndicateurs = indicateursTableau[i].ToArray(); + + for (int j = 0; j < ligneIndicateurs.Length; ++j) + { + DessinerPion(ligneIndicateurs[j]); + } + + if (ligneIndicateurs.Length < 4) + { + Console.Write("".PadLeft((4 - ligneIndicateurs.Length) * 3)); + } } else { - Console.Write(" "); + Console.Write(" "); } + + Console.WriteLine(" β”‚"); } + + + Console.WriteLine("β”‚ β”‚ β”‚ β”‚"); + Console.WriteLine("──────────────── ────────────────"); } - public static void DessinerIndicateurs(IEnumerable indicateurs) + public static string? DemanderJoueur(Object? sender, DemanderJoueurEventArgs e) { - if (indicateurs == null) - { - indicateurs = []; - } + Console.WriteLine($"Joueur {e.Numero}"); + Console.Write(">>> "); - foreach (Indicateur indicateur in indicateurs) - { - DessinerIndicateur(indicateur); - } + string? nom = Console.ReadLine(); - if (indicateurs.Count() < 4) - Console.Write("".PadLeft((4 - indicateurs.Count()) * 3)); + Console.WriteLine(); + + return nom; } - public static void DessinerPlateau(IEnumerable> grille, IEnumerable> indicateurs) + + public static void CommencerLaPartie(Object? sender, DebutPartieEventArgs e) { - IEnumerable[] grilleT = grille.ToArray(); - IEnumerable[] indicateursT = indicateurs.ToArray(); + DessinerSeparateur(); + Console.WriteLine("La partie commence, bonne chance Γ  tous !\n"); + } - Console.WriteLine(" Codes Indicateurs "); - Console.WriteLine("──────────────── ────────────────"); - Console.WriteLine("β”‚ β”‚ β”‚ β”‚"); + + public static void NouveauTour(Object? sender, NouveauTourEventArgs e) + { + DessinerSeparateur(); - for (int i = 0; i < grilleT.Length; ++i) - { - Console.Write("β”‚ "); - DessinerCode(grilleT[i]); - Console.Write(" β”‚"); + Console.WriteLine($"Tour {e.Tour} - {e.Joueur.Nom}\n"); - Console.Write(" "); + DessinerPlateau(e.Grille, e.Indicateurs); - Console.Write("β”‚ "); - DessinerIndicateurs(indicateursT[i]); - Console.WriteLine(" β”‚"); - } + Console.WriteLine(); - Console.WriteLine("β”‚ β”‚ β”‚ β”‚"); - Console.WriteLine("──────────────── ────────────────"); } - - public static Jeton? ChoixJeton() + public static Jeton DemanderJeton(Object? sender, DemanderJetonEventArgs e) { Console.TreatControlCAsInput = true; bool aChoisi = false; int indice = 0; Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); - Couleur couleur = couleurs[indice]; while (!aChoisi) { - DessinerJeton(new Jeton(couleur)); + DessinerPion(couleurs[indice]); Console.Write("\b\b\b"); - ConsoleKey touche = Console.ReadKey(true).Key; - - switch (touche) + switch (Console.ReadKey(true).Key) { case ConsoleKey.Enter: aChoisi = true; @@ -190,8 +183,8 @@ namespace ConsoleApp ++indice; break; - case ConsoleKey.Escape: - return null; + /*case ConsoleKey.Escape: + return null;*/ default: break; @@ -201,38 +194,40 @@ namespace ConsoleApp indice = couleurs.Length - 1; else if (indice >= couleurs.Length) indice = 0; - - couleur = couleurs[indice]; } Console.TreatControlCAsInput = false; - return new Jeton(couleur); + + return new Jeton(couleurs[indice]); } - public static void ChoixCode(ref Code code) + public static void AjouterJeton(Object? sender, AjouterJetonEventArgs e) { - while (!code.EstComplet()) - { - Jeton? jeton = ChoixJeton(); + DessinerPion(e.Jeton.Couleur); + } - if (jeton.HasValue) - { - DessinerJeton(jeton.Value); - code.AjouterJeton(jeton.Value); - } - else - { - Console.Write("\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"); - try - { - code.SupprimerDernierJeton(); - } - catch(CodeTableauLesJetonsVideException) - { - Console.WriteLine("Il n'y a pas de jetons! Impossible de supprimer"); - } - - } + public static void AjouterCode(Object? sender, AjouterCodeEventArgs e) + { + Console.WriteLine(); + + DessinerSeparateur(); + } + + public static void PartieTerminee(Object? sender, PartieTermineeEventArgs e) + { + Joueur[] gagnants = e.Gagnants.ToArray(); + + if (gagnants.Length > 1) + { + Console.WriteLine("C'est une Γ©galitΓ© !"); + } + else if (gagnants.Length == 1) + { + Console.WriteLine($"C'est une victoire de {gagnants[0].Nom}."); + } + else + { + Console.WriteLine("C'est une dΓ©faite des deux joueurs..."); } } } diff --git a/Sources/ConsoleApp/UtilsNomJoueurNullException.cs b/Sources/ConsoleApp/UtilsNomJoueurNullException.cs deleted file mode 100644 index 20187e7..0000000 --- a/Sources/ConsoleApp/UtilsNomJoueurNullException.cs +++ /dev/null @@ -1,14 +0,0 @@ -ο»Ώusing System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ConsoleApp -{ - public class UtilsNomJoueurNullException : Exception - { - public UtilsNomJoueurNullException() : base("Le nom du joueur est null") - { } - } -} diff --git a/Sources/CoreLibrary/Events/DebutPartieEventArgs.cs b/Sources/CoreLibrary/Events/DebutPartieEventArgs.cs index 93745af..5b830a7 100644 --- a/Sources/CoreLibrary/Events/DebutPartieEventArgs.cs +++ b/Sources/CoreLibrary/Events/DebutPartieEventArgs.cs @@ -2,9 +2,5 @@ { public class DebutPartieEventArgs : EventArgs { - - public DebutPartieEventArgs() - { - } } } diff --git a/Sources/CoreLibrary/Events/DemanderJetonEventArgs.cs b/Sources/CoreLibrary/Events/DemanderJetonEventArgs.cs new file mode 100644 index 0000000..e2a982b --- /dev/null +++ b/Sources/CoreLibrary/Events/DemanderJetonEventArgs.cs @@ -0,0 +1,7 @@ +ο»Ώ +namespace CoreLibrary.Events +{ + public class DemanderJetonEventArgs : EventArgs + { + } +} diff --git a/Sources/CoreLibrary/Events/DemanderJoueurEventArgs.cs b/Sources/CoreLibrary/Events/DemanderJoueurEventArgs.cs new file mode 100644 index 0000000..9b38733 --- /dev/null +++ b/Sources/CoreLibrary/Events/DemanderJoueurEventArgs.cs @@ -0,0 +1,12 @@ +ο»Ώnamespace CoreLibrary.Events +{ + public class DemanderJoueurEventArgs : EventArgs + { + public int Numero { get; private set; } + + public DemanderJoueurEventArgs(int numero) + { + Numero = numero; + } + } +} diff --git a/Sources/CoreLibrary/Events/NouveauTourEventArgs.cs b/Sources/CoreLibrary/Events/NouveauTourEventArgs.cs index a6e297f..1a15771 100644 --- a/Sources/CoreLibrary/Events/NouveauTourEventArgs.cs +++ b/Sources/CoreLibrary/Events/NouveauTourEventArgs.cs @@ -1,14 +1,20 @@ -ο»Ώnamespace CoreLibrary.Events +ο»Ώusing CoreLibrary.Exceptions; + +namespace CoreLibrary.Events { public class NouveauTourEventArgs : EventArgs { public Joueur Joueur { get; private set; } public int Tour { get; private set; } + public IEnumerable> Grille { get; private set; } + public IEnumerable> Indicateurs { get; private set; } - public NouveauTourEventArgs(Joueur joueur, int tour) + public NouveauTourEventArgs(Joueur joueur, int tour, IEnumerable> grille, IEnumerable> indicateurs) { Joueur = joueur; Tour = tour; + Grille = grille; + Indicateurs = indicateurs; } } } \ No newline at end of file diff --git a/Sources/CoreLibrary/Events/PasserMainEventArgs.cs b/Sources/CoreLibrary/Events/PasserMainEventArgs.cs index 30999ad..fcff1f4 100644 --- a/Sources/CoreLibrary/Events/PasserMainEventArgs.cs +++ b/Sources/CoreLibrary/Events/PasserMainEventArgs.cs @@ -1,13 +1,6 @@ -ο»Ώ -using System.Security.Cryptography.X509Certificates; - -namespace CoreLibrary.Events +ο»Ώnamespace CoreLibrary.Events { public class PasserMainEventArgs : EventArgs { - public PasserMainEventArgs() - { - - } } } diff --git a/Sources/CoreLibrary/Partie.cs b/Sources/CoreLibrary/Partie.cs index 41a2bd9..c41b587 100644 --- a/Sources/CoreLibrary/Partie.cs +++ b/Sources/CoreLibrary/Partie.cs @@ -4,23 +4,33 @@ namespace CoreLibrary { public class Partie { - private IRegles regles; + private readonly IRegles regles; - public event EventHandler ajouterJoueur; - public event EventHandler debutPartie; - public event EventHandler nouveauTour; - public event EventHandler ajouterJeton; - public event EventHandler ajouterCode; - public event EventHandler passerMain; - public event EventHandler partieTerminee; + public delegate string? StringEventHandler(Object? sender, TEventArgs e); + public delegate Jeton JetonEventHandler(Object? sender, TEventArgs e); - private void QuandAjouterJoueur(Joueur joueur) => ajouterJoueur?.Invoke(this, new AjouterJoueursEventArgs(joueur)); - private void QuandDebutPartie() => debutPartie?.Invoke(this, new DebutPartieEventArgs()); - private void QuandNouveauTour(Joueur joueur, int tour) => nouveauTour?.Invoke(this, new NouveauTourEventArgs(joueur, tour)); - private void QuandNouveauJeton(Jeton jeton) => ajouterJeton?.Invoke(this, new AjouterJetonEventArgs(jeton)); - private void QuandNouveauCode(Code code) => ajouterCode?.Invoke(this, new AjouterCodeEventArgs(code)); - private void QuandPasserMain() => passerMain?.Invoke(this, new PasserMainEventArgs()); - private void QuandPartieTerminee(IEnumerable gagnants, IEnumerable perdants) => partieTerminee?.Invoke(this, new PartieTermineeEventArgs(gagnants, perdants)); + public event StringEventHandler? DemanderJoueur; + public event JetonEventHandler? DemanderJeton; + + public event EventHandler? AjouterJoueur; + public event EventHandler? DebutPartie; + public event EventHandler? NouveauTour; + public event EventHandler? AjouterJeton; + public event EventHandler? AjouterCode; + public event EventHandler? PasserMain; + public event EventHandler? PartieTerminee; + + private string? QuandDemanderJoueur(int numero) => DemanderJoueur?.Invoke(this, new DemanderJoueurEventArgs(numero)); + private Jeton? QuandDemanderJeton() => DemanderJeton?.Invoke(this, new DemanderJetonEventArgs()); + + + private void QuandAjouterJoueur(Joueur joueur) => AjouterJoueur?.Invoke(this, new AjouterJoueursEventArgs(joueur)); + private void QuandDebutPartie() => DebutPartie?.Invoke(this, new DebutPartieEventArgs()); + private void QuandNouveauTour(Joueur joueur, int tour, IEnumerable> grille, IEnumerable> indicateurs) => NouveauTour?.Invoke(this, new NouveauTourEventArgs(joueur, tour, grille, indicateurs)); + private void QuandNouveauJeton(Jeton jeton) => AjouterJeton?.Invoke(this, new AjouterJetonEventArgs(jeton)); + private void QuandNouveauCode(Code code) => AjouterCode?.Invoke(this, new AjouterCodeEventArgs(code)); + private void QuandPasserMain() => PasserMain?.Invoke(this, new PasserMainEventArgs()); + private void QuandPartieTerminee(IEnumerable gagnants, IEnumerable perdants) => PartieTerminee?.Invoke(this, new PartieTermineeEventArgs(gagnants, perdants)); public Partie(IRegles regles) @@ -30,7 +40,12 @@ namespace CoreLibrary public void Jouer() { - DefinirJoueurs(); + while (regles.NbJoueurs != regles.NbJoueursMaximum) + { + string nom = QuandDemanderJoueur(regles.NbJoueurs + 1) ?? $"Joueur {regles.NbJoueurs+1}"; + Joueur joueur = regles.AjouterJoueur(nom); + QuandAjouterJoueur(joueur); + } regles.CommencerLaPartie(); QuandDebutPartie(); @@ -40,14 +55,15 @@ namespace CoreLibrary Joueur joueurCourant = regles.JoueurCourant(); Plateau plateauCourant = joueurCourant.Plateau; - QuandNouveauTour(joueurCourant, plateauCourant.Tour); + QuandNouveauTour(joueurCourant, plateauCourant.Tour, plateauCourant.Grille(), plateauCourant.Indicateurs()); Code code = regles.GenererCode(); while (!code.EstComplet()) { - AjouterJeton(code); - + Jeton jeton = QuandDemanderJeton() ?? new Jeton(); + code.AjouterJeton(jeton); + QuandNouveauJeton(jeton); } plateauCourant.AjouterCode(code); QuandNouveauCode(code); @@ -56,33 +72,11 @@ namespace CoreLibrary QuandPasserMain(); } - PartieTerminee(); - QuandPartieTerminee(regles.Gagnants(), regles.Perdants()); - } - - private void AjouterJeton(Code code) - { - Jeton jeton = new Jeton(Couleur.ROUGE); - code.AjouterJeton(jeton); - QuandNouveauJeton(jeton); - } - - private void DefinirJoueurs() - { - while(regles.NbJoueurs != regles.NbJoueursMaximum) - { - string nom = "pauline"; - regles.AjouterJoueur(nom); - //QuandAjouterJoueur() - } - } - private void PartieTerminee() - { regles.Gagnants(); regles.Perdants(); - } - + QuandPartieTerminee(regles.Gagnants(), regles.Perdants()); + } } } From 43a0c68f723983174800c2c26864e5e9fd112690 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Fri, 10 May 2024 14:01:41 +0200 Subject: [PATCH 5/6] oublie indexation --- Sources/CoreLibrary/Code.cs | 3 ++- Sources/CoreLibrary/ReglesClassiques.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/CoreLibrary/Code.cs b/Sources/CoreLibrary/Code.cs index 4651bcb..0e1756e 100644 --- a/Sources/CoreLibrary/Code.cs +++ b/Sources/CoreLibrary/Code.cs @@ -84,9 +84,10 @@ namespace CoreLibrary { // Mon code est le code correct, l'autre code est celui qui teste + Indicateur[] indicateurs = []; - if (EstComplet() || !autreCode.EstComplet()) + if (!EstComplet() || !autreCode.EstComplet()) return indicateurs; Jeton?[] mesJetons = Jetons().ToArray(); diff --git a/Sources/CoreLibrary/ReglesClassiques.cs b/Sources/CoreLibrary/ReglesClassiques.cs index 53c3ef8..ef16576 100644 --- a/Sources/CoreLibrary/ReglesClassiques.cs +++ b/Sources/CoreLibrary/ReglesClassiques.cs @@ -24,7 +24,7 @@ namespace CoreLibrary public Joueur AjouterJoueur(string nom) { - Joueur joueur = new Joueur(nom, new Plateau(TailleCodeMaximum, TourMaximum)) + Joueur joueur = new Joueur(nom, new Plateau(TailleCodeMaximum, TourMaximum)); joueurs[nbJoueurs++] = joueur; return joueur; } From 84d05806d36e91f60b62cfbe5f2147aa448de1e8 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Sun, 12 May 2024 17:20:55 +0200 Subject: [PATCH 6/6] =?UTF-8?q?Ajout=20de=20la=20possibilit=C3=A9=20de=20s?= =?UTF-8?q?upprimer=20le=20dernier=20jeton=20en=20faisant=20echap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/ConsoleApp/Evenements.cs | 121 ++++++++++++++++ Sources/ConsoleApp/Program.cs | 21 ++- Sources/ConsoleApp/Utils.cs | 132 ++---------------- .../Events/SupprimerDernierJetonEventArgs.cs | 6 + Sources/CoreLibrary/Partie.cs | 34 ++++- 5 files changed, 177 insertions(+), 137 deletions(-) create mode 100644 Sources/ConsoleApp/Evenements.cs create mode 100644 Sources/CoreLibrary/Events/SupprimerDernierJetonEventArgs.cs diff --git a/Sources/ConsoleApp/Evenements.cs b/Sources/ConsoleApp/Evenements.cs new file mode 100644 index 0000000..662f9ce --- /dev/null +++ b/Sources/ConsoleApp/Evenements.cs @@ -0,0 +1,121 @@ +ο»Ώusing CoreLibrary.Events; +using CoreLibrary; + +namespace ConsoleApp +{ + public class Evenements + { + public static string? DemanderJoueur(Object? sender, DemanderJoueurEventArgs e) + { + Console.WriteLine($"Joueur {e.Numero}"); + Console.Write(">>> "); + + string? nom = Console.ReadLine(); + + Console.WriteLine(); + + return nom; + } + + + public static void CommencerLaPartie(Object? sender, DebutPartieEventArgs e) + { + Utils.DessinerSeparateur(); + + Console.WriteLine("La partie commence, bonne chance Γ  tous !\n"); + } + + + public static void NouveauTour(Object? sender, NouveauTourEventArgs e) + { + Utils.DessinerSeparateur(); + + Console.WriteLine($"Tour {e.Tour} - {e.Joueur.Nom}\n"); + + Utils.DessinerPlateau(e.Grille, e.Indicateurs); + + Console.WriteLine(); + + } + + public static Jeton? DemanderJeton(Object? sender, DemanderJetonEventArgs e) + { + Console.TreatControlCAsInput = true; + + bool aChoisi = false; + int indice = 0; + Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); + + while (!aChoisi) + { + Utils.DessinerPion(couleurs[indice]); + Console.Write("\b\b\b"); + + switch (Console.ReadKey(true).Key) + { + case ConsoleKey.Enter: + aChoisi = true; + break; + + case ConsoleKey.LeftArrow: + --indice; + break; + + case ConsoleKey.RightArrow: + ++indice; + break; + + case ConsoleKey.Escape: + return null; + + default: + break; + } + + if (indice < 0) + indice = couleurs.Length - 1; + else if (indice >= couleurs.Length) + indice = 0; + } + + Console.TreatControlCAsInput = false; + + return new Jeton(couleurs[indice]); + } + + public static void AjouterJeton(Object? sender, AjouterJetonEventArgs e) + { + Utils.DessinerPion(e.Jeton.Couleur); + } + + public static void SupprimerDernierJeton(Object? sender, SupprimerDernierJetonEventArgs e) + { + Console.Write("\b\b\b \b\b\b\b\b\b"); + } + + public static void AjouterCode(Object? sender, AjouterCodeEventArgs e) + { + Console.WriteLine(); + + Utils.DessinerSeparateur(); + } + + public static void PartieTerminee(Object? sender, PartieTermineeEventArgs e) + { + Joueur[] gagnants = e.Gagnants.ToArray(); + + if (gagnants.Length > 1) + { + Console.WriteLine("C'est une Γ©galitΓ© !"); + } + else if (gagnants.Length == 1) + { + Console.WriteLine($"C'est une victoire de {gagnants[0].Nom}."); + } + else + { + Console.WriteLine("C'est une dΓ©faite des deux joueurs..."); + } + } + } +} diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index c7dc2de..cb384c3 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -8,14 +8,13 @@ Utils.DessinerTitre(); Partie maPartie = new Partie(new ReglesClassiques()); -maPartie.DemanderJoueur += Utils.DemanderJoueur; - -maPartie.DebutPartie += Utils.CommencerLaPartie; -maPartie.NouveauTour += Utils.NouveauTour; -maPartie.DemanderJeton += Utils.DemanderJeton; -maPartie.AjouterJeton += Utils.AjouterJeton; -maPartie.AjouterCode += Utils.AjouterCode; -maPartie.PartieTerminee += Utils.PartieTerminee; - - -maPartie.Jouer(); \ No newline at end of file +maPartie.DemanderJoueur += Evenements.DemanderJoueur; +maPartie.DebutPartie += Evenements.CommencerLaPartie; +maPartie.NouveauTour += Evenements.NouveauTour; +maPartie.DemanderJeton += Evenements.DemanderJeton; +maPartie.AjouterJeton += Evenements.AjouterJeton; +maPartie.SupprimerDernierJeton += Evenements.SupprimerDernierJeton; +maPartie.AjouterCode += Evenements.AjouterCode; +maPartie.PartieTerminee += Evenements.PartieTerminee; + +maPartie.Jouer(); diff --git a/Sources/ConsoleApp/Utils.cs b/Sources/ConsoleApp/Utils.cs index d6157ee..442fac5 100644 --- a/Sources/ConsoleApp/Utils.cs +++ b/Sources/ConsoleApp/Utils.cs @@ -1,7 +1,4 @@ ο»Ώusing CoreLibrary; -using CoreLibrary.Events; -using System.Collections.Generic; -using System.Reflection; namespace ConsoleApp { @@ -34,7 +31,7 @@ namespace ConsoleApp DessinerSeparateur(); } - + public static void DessinerSeparateur() { @@ -50,9 +47,9 @@ namespace ConsoleApp { Console.Write(" "); - Console.ForegroundColor = pion.GetType().Equals(typeof(Couleur)) ? - couleursTerminal.GetValueOrDefault((Couleur) pion) : - indicateursTerminal.GetValueOrDefault((Indicateur) pion); + Console.ForegroundColor = pion.GetType().Equals(typeof(Couleur)) ? + couleursTerminal.GetValueOrDefault((Couleur)pion) : + indicateursTerminal.GetValueOrDefault((Indicateur)pion); Console.BackgroundColor = Console.ForegroundColor.Equals(ConsoleColor.Black) ? ConsoleColor.White : ConsoleColor.Black; @@ -65,8 +62,8 @@ namespace ConsoleApp public static void DessinerPlateau(IEnumerable> grille, IEnumerable> indicateurs) { - IEnumerable[] grilleTableau = (IEnumerable[]) grille.ToArray(); - IEnumerable[] indicateursTableau = (IEnumerable[]) indicateurs.ToArray(); + IEnumerable[] grilleTableau = (IEnumerable[])grille.ToArray(); + IEnumerable[] indicateursTableau = (IEnumerable[])indicateurs.ToArray(); Console.WriteLine(" Codes Indicateurs "); Console.WriteLine("──────────────── ────────────────"); @@ -82,7 +79,7 @@ namespace ConsoleApp { Jeton? jeton = ligneGrille[j]; - if(jeton.HasValue) + if (jeton.HasValue) { DessinerPion(jeton.Value.Couleur); } @@ -90,12 +87,12 @@ namespace ConsoleApp { Console.Write(" "); } - + } Console.Write(" β”‚ β”‚ "); - - if(indicateursTableau[i] != null) + + if (indicateursTableau[i] != null) { Indicateur[] ligneIndicateurs = indicateursTableau[i].ToArray(); @@ -121,114 +118,5 @@ namespace ConsoleApp Console.WriteLine("β”‚ β”‚ β”‚ β”‚"); Console.WriteLine("──────────────── ────────────────"); } - - - public static string? DemanderJoueur(Object? sender, DemanderJoueurEventArgs e) - { - Console.WriteLine($"Joueur {e.Numero}"); - Console.Write(">>> "); - - string? nom = Console.ReadLine(); - - Console.WriteLine(); - - return nom; - } - - - public static void CommencerLaPartie(Object? sender, DebutPartieEventArgs e) - { - DessinerSeparateur(); - - Console.WriteLine("La partie commence, bonne chance Γ  tous !\n"); - } - - - public static void NouveauTour(Object? sender, NouveauTourEventArgs e) - { - DessinerSeparateur(); - - Console.WriteLine($"Tour {e.Tour} - {e.Joueur.Nom}\n"); - - DessinerPlateau(e.Grille, e.Indicateurs); - - Console.WriteLine(); - - } - - public static Jeton DemanderJeton(Object? sender, DemanderJetonEventArgs e) - { - Console.TreatControlCAsInput = true; - - bool aChoisi = false; - int indice = 0; - Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); - - while (!aChoisi) - { - DessinerPion(couleurs[indice]); - Console.Write("\b\b\b"); - - switch (Console.ReadKey(true).Key) - { - case ConsoleKey.Enter: - aChoisi = true; - break; - - case ConsoleKey.LeftArrow: - --indice; - break; - - case ConsoleKey.RightArrow: - ++indice; - break; - - /*case ConsoleKey.Escape: - return null;*/ - - default: - break; - } - - if (indice < 0) - indice = couleurs.Length - 1; - else if (indice >= couleurs.Length) - indice = 0; - } - - Console.TreatControlCAsInput = false; - - return new Jeton(couleurs[indice]); - } - - public static void AjouterJeton(Object? sender, AjouterJetonEventArgs e) - { - DessinerPion(e.Jeton.Couleur); - } - - public static void AjouterCode(Object? sender, AjouterCodeEventArgs e) - { - Console.WriteLine(); - - DessinerSeparateur(); - } - - public static void PartieTerminee(Object? sender, PartieTermineeEventArgs e) - { - Joueur[] gagnants = e.Gagnants.ToArray(); - - if (gagnants.Length > 1) - { - Console.WriteLine("C'est une Γ©galitΓ© !"); - } - else if (gagnants.Length == 1) - { - Console.WriteLine($"C'est une victoire de {gagnants[0].Nom}."); - } - else - { - Console.WriteLine("C'est une dΓ©faite des deux joueurs..."); - } - } } } diff --git a/Sources/CoreLibrary/Events/SupprimerDernierJetonEventArgs.cs b/Sources/CoreLibrary/Events/SupprimerDernierJetonEventArgs.cs new file mode 100644 index 0000000..4796efd --- /dev/null +++ b/Sources/CoreLibrary/Events/SupprimerDernierJetonEventArgs.cs @@ -0,0 +1,6 @@ +ο»Ώnamespace CoreLibrary.Events +{ + public class SupprimerDernierJetonEventArgs : EventArgs + { + } +} diff --git a/Sources/CoreLibrary/Partie.cs b/Sources/CoreLibrary/Partie.cs index c41b587..d59ba5e 100644 --- a/Sources/CoreLibrary/Partie.cs +++ b/Sources/CoreLibrary/Partie.cs @@ -1,4 +1,5 @@ ο»Ώusing CoreLibrary.Events; +using CoreLibrary.Exceptions; namespace CoreLibrary { @@ -7,7 +8,7 @@ namespace CoreLibrary private readonly IRegles regles; public delegate string? StringEventHandler(Object? sender, TEventArgs e); - public delegate Jeton JetonEventHandler(Object? sender, TEventArgs e); + public delegate Jeton? JetonEventHandler(Object? sender, TEventArgs e); public event StringEventHandler? DemanderJoueur; public event JetonEventHandler? DemanderJeton; @@ -16,6 +17,7 @@ namespace CoreLibrary public event EventHandler? DebutPartie; public event EventHandler? NouveauTour; public event EventHandler? AjouterJeton; + public event EventHandler? SupprimerDernierJeton; public event EventHandler? AjouterCode; public event EventHandler? PasserMain; public event EventHandler? PartieTerminee; @@ -28,6 +30,7 @@ namespace CoreLibrary private void QuandDebutPartie() => DebutPartie?.Invoke(this, new DebutPartieEventArgs()); private void QuandNouveauTour(Joueur joueur, int tour, IEnumerable> grille, IEnumerable> indicateurs) => NouveauTour?.Invoke(this, new NouveauTourEventArgs(joueur, tour, grille, indicateurs)); private void QuandNouveauJeton(Jeton jeton) => AjouterJeton?.Invoke(this, new AjouterJetonEventArgs(jeton)); + private void QuandSupprimerDernierJeton() => SupprimerDernierJeton?.Invoke(this, new SupprimerDernierJetonEventArgs()); private void QuandNouveauCode(Code code) => AjouterCode?.Invoke(this, new AjouterCodeEventArgs(code)); private void QuandPasserMain() => PasserMain?.Invoke(this, new PasserMainEventArgs()); private void QuandPartieTerminee(IEnumerable gagnants, IEnumerable perdants) => PartieTerminee?.Invoke(this, new PartieTermineeEventArgs(gagnants, perdants)); @@ -61,9 +64,32 @@ namespace CoreLibrary while (!code.EstComplet()) { - Jeton jeton = QuandDemanderJeton() ?? new Jeton(); - code.AjouterJeton(jeton); - QuandNouveauJeton(jeton); + Jeton? jeton = QuandDemanderJeton(); + + if(jeton == null) + { + if(DemanderJeton != null) + { + // Si quelqu'un Γ©coute l'Γ©vΓ©nement mais ne renvoie rien, alors on supprime le dernier jeton + try + { + code.SupprimerDernierJeton(); + QuandSupprimerDernierJeton(); + } + catch(CodeVideException){} + } + else + { + // Si personne n'Γ©coute l'Γ©venment, on met un jeton alΓ©atoire + jeton = new Jeton(); + } + } + + if(jeton != null) + { + code.AjouterJeton(jeton.Value); + QuandNouveauJeton(jeton.Value); + } } plateauCourant.AjouterCode(code); QuandNouveauCode(code);