From 2e1dcef32bfc78b417a6fa7aedbdd36c82e159c1 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Fri, 3 May 2024 15:46:26 +0200 Subject: [PATCH] Application console --- Sources/ConsoleApp/Program.cs | 61 ++++++++- Sources/ConsoleApp/Utils.cs | 226 ++++++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+), 4 deletions(-) create mode 100644 Sources/ConsoleApp/Utils.cs diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index b436bdb..fc9e32a 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -1,7 +1,60 @@ using CoreLibrary; +using ConsoleApp; -JetonIndicateur ji1 = new JetonIndicateur(Couleur.Noir); -Console.WriteLine(ji1.Couleur); -Jeton j1 = new JetonIndicateur(Couleur.Blanc); -Console.WriteLine(j1.Couleur); \ No newline at end of file + +Console.OutputEncoding = System.Text.Encoding.UTF8; + +ReglesClassiques partie = new ReglesClassiques(); + +Utils.AfficherTitre(); + +Utils.AfficherTitre("Joueurs"); +string joueur1 = Utils.SaisirNom(); +string joueur2 = Utils.SaisirNom(); +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/ConsoleApp/Utils.cs b/Sources/ConsoleApp/Utils.cs new file mode 100644 index 0000000..b19d651 --- /dev/null +++ b/Sources/ConsoleApp/Utils.cs @@ -0,0 +1,226 @@ +ο»Ώusing CoreLibrary; + +namespace ConsoleApp +{ + internal class Utils + { + private static int nombreJoueurs = 0; + + private readonly static int longueurTitre = 55; + + private readonly static Dictionary convertirCouleurs = 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 } + }; + private readonly static Dictionary convertirIndicateurs = new Dictionary() + { + {Indicateur.BONNEPLACE, ConsoleColor.Black }, + {Indicateur.BONNECOULEUR, ConsoleColor.White } + }; + + public static void AfficherTitre() + { + + Console.WriteLine(""" + __ __ _ _ _ + | \/ | __ _ ___| |_ ___ _ _ _ __ (_) _ _ __| | + | |\/| |/ _` |(_-<| _|/ -_)| '_|| ' \ | || ' \ / _` | + |_| |_|\__,_|/__/ \__|\___||_| |_|_|_||_||_||_|\__,_| + """); + + AfficherSeparateur(); + } + + 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() + { + Console.WriteLine(""" + + _______________________________________________________ + + + """); + } + + public static string SaisirNom() + { + string nom = $"Joueur {++nombreJoueurs}"; + + Console.WriteLine(nom); + Console.Write(">>> "); + nom = Console.ReadLine() ?? nom; + Console.WriteLine(); + + return nom; + } + + public static void DessinerJeton(Jeton jeton) + { + Console.Write(" "); + + Console.ForegroundColor = convertirCouleurs.GetValueOrDefault(jeton.Couleur); + Console.BackgroundColor = Console.ForegroundColor.Equals(ConsoleColor.Black) ? ConsoleColor.White : ConsoleColor.Black; + + Console.Write("⬀"); + + Console.ResetColor(); + + Console.Write(" "); + } + + public static void DessinerIndicateur(Indicateur indicateur) + { + Console.Write(" "); + + Console.ForegroundColor = convertirIndicateurs.GetValueOrDefault(indicateur); + Console.BackgroundColor = Console.ForegroundColor.Equals(ConsoleColor.Black) ? ConsoleColor.White : ConsoleColor.Black; + + Console.Write("⬀"); + + Console.ResetColor(); + + Console.Write(" "); + } + + public static void DessinerCode(IEnumerable jetons) + { + foreach (Jeton? jeton in jetons) + { + if (jeton.HasValue) + { + DessinerJeton(jeton.Value); + } + else + { + Console.Write(" "); + } + } + } + + + public static void DessinerIndicateurs(IEnumerable indicateurs) + { + if (indicateurs == null) + { + indicateurs = []; + } + + foreach (Indicateur indicateur in indicateurs) + { + DessinerIndicateur(indicateur); + } + + if (indicateurs.Count() < 4) + Console.Write("".PadLeft((4 - indicateurs.Count()) * 3)); + } + + public static void DessinerPlateau(IEnumerable> grille, IEnumerable> indicateurs) + { + IEnumerable[] grilleT = grille.ToArray(); + IEnumerable[] indicateursT = indicateurs.ToArray(); + + + Console.WriteLine(" Codes Indicateurs "); + Console.WriteLine("──────────────── ────────────────"); + Console.WriteLine("β”‚ β”‚ β”‚ β”‚"); + + for (int i = 0; i < grilleT.Length; ++i) + { + Console.Write("β”‚ "); + DessinerCode(grilleT[i]); + Console.Write(" β”‚"); + + Console.Write(" "); + + Console.Write("β”‚ "); + DessinerIndicateurs(indicateursT[i]); + Console.WriteLine(" β”‚"); + } + + Console.WriteLine("β”‚ β”‚ β”‚ β”‚"); + Console.WriteLine("──────────────── ────────────────"); + } + + + public static Jeton? ChoixJeton() + { + 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)); + Console.Write("\b\b\b"); + + ConsoleKey touche = Console.ReadKey(true).Key; + + switch (touche) + { + 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; + + couleur = couleurs[indice]; + } + + Console.TreatControlCAsInput = false; + return new Jeton(couleur); + } + + public static void ChoixCode(ref Code code) + { + while (!code.EstComplet()) + { + Jeton? jeton = ChoixJeton(); + + 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"); + code.SupprimerDernierJeton(); + } + } + } + } +}