Partie consoleapp
continuous-integration/drone/push Build is failing Details

master
Céleste BARBOSA 12 months ago
parent 46120b4340
commit 78c938384e

@ -1,13 +1,21 @@
using ConsoleApp;
using CoreLibrary; 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()); Partie maPartie = new Partie(new ReglesClassiques());
maPartie.debutPartie += LaPartieDemarre;
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(); maPartie.Jouer();

@ -1,31 +1,30 @@
using CoreLibrary; using CoreLibrary;
using CoreLibrary.Events;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApp namespace ConsoleApp
{ {
internal class Utils public class Utils
{ {
private static int nombreJoueurs = 0; private readonly static Dictionary<Couleur, ConsoleColor> couleursTerminal = new Dictionary<Couleur, ConsoleColor>()
private readonly static int longueurTitre = 55;
private readonly static Dictionary<Couleur, ConsoleColor> convertirCouleurs = new Dictionary<Couleur, ConsoleColor>()
{ {
{Couleur.NOIR, ConsoleColor.Black }, {Couleur.NOIR, ConsoleColor.Black },
{Couleur.BLANC, ConsoleColor.White }, {Couleur.BLANC, ConsoleColor.White },
{Couleur.ROUGE, ConsoleColor.Red }, {Couleur.ROUGE, ConsoleColor.Red },
{Couleur.VERT, ConsoleColor.Green }, {Couleur.VERT, ConsoleColor.Green },
{Couleur.BLEU, ConsoleColor.Blue }, {Couleur.BLEU, ConsoleColor.Blue },
{Couleur.JAUNE, ConsoleColor.Yellow } {Couleur.JAUNE, ConsoleColor.DarkYellow }
}; };
private readonly static Dictionary<Indicateur, ConsoleColor> convertirIndicateurs = new Dictionary<Indicateur, ConsoleColor>()
private readonly static Dictionary<Indicateur, ConsoleColor> indicateursTerminal = new Dictionary<Indicateur, ConsoleColor>()
{ {
{Indicateur.BONNEPLACE, ConsoleColor.Black }, {Indicateur.BONNEPLACE, ConsoleColor.Black },
{Indicateur.BONNECOULEUR, ConsoleColor.White } {Indicateur.BONNECOULEUR, ConsoleColor.White }
}; };
public static void AfficherTitre() public static void DessinerTitre()
{ {
Console.WriteLine(""" 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(""" Console.WriteLine("""
_______________________________________________________
"""); """);
} }
public static string SaisirNom() public static void DessinerPion(Enum pion)
{
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)
{ {
Console.Write(" "); 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.BackgroundColor = Console.ForegroundColor.Equals(ConsoleColor.Black) ? ConsoleColor.White : ConsoleColor.Black;
Console.Write("⬤"); Console.Write("⬤");
@ -86,97 +63,113 @@ namespace ConsoleApp
Console.Write(" "); Console.Write(" ");
} }
public static void DessinerIndicateur(Indicateur indicateur) public static void DessinerPlateau(IEnumerable<IEnumerable<Jeton?>> grille, IEnumerable<IEnumerable<Indicateur>> indicateurs)
{ {
Console.Write(" "); IEnumerable<Jeton?>[] grilleTableau = (IEnumerable<Jeton?>[]) grille.ToArray();
IEnumerable<Indicateur>[] indicateursTableau = (IEnumerable<Indicateur>[]) indicateurs.ToArray();
Console.ForegroundColor = convertirIndicateurs.GetValueOrDefault(indicateur); Console.WriteLine(" Codes Indicateurs ");
Console.BackgroundColor = Console.ForegroundColor.Equals(ConsoleColor.Black) ? ConsoleColor.White : ConsoleColor.Black; 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<Jeton?> jetons) if(jeton.HasValue)
{ {
foreach (Jeton? jeton in jetons) DessinerPion(jeton.Value.Couleur);
{ }
if (jeton.HasValue) 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 else
{ {
Console.Write(" "); Console.Write(" ");
} }
Console.WriteLine(" │");
} }
Console.WriteLine("│ │ │ │");
Console.WriteLine("──────────────── ────────────────");
} }
public static void DessinerIndicateurs(IEnumerable<Indicateur> indicateurs) public static string? DemanderJoueur(Object? sender, DemanderJoueurEventArgs e)
{ {
if (indicateurs == null) Console.WriteLine($"Joueur {e.Numero}");
{ Console.Write(">>> ");
indicateurs = [];
}
foreach (Indicateur indicateur in indicateurs) string? nom = Console.ReadLine();
{
DessinerIndicateur(indicateur);
}
if (indicateurs.Count() < 4) Console.WriteLine();
Console.Write("".PadLeft((4 - indicateurs.Count()) * 3));
return nom;
} }
public static void DessinerPlateau(IEnumerable<IEnumerable<Jeton?>> grille, IEnumerable<IEnumerable<Indicateur>> indicateurs)
public static void CommencerLaPartie(Object? sender, DebutPartieEventArgs e)
{ {
IEnumerable<Jeton?>[] grilleT = grille.ToArray(); DessinerSeparateur();
IEnumerable<Indicateur>[] indicateursT = indicateurs.ToArray();
Console.WriteLine("La partie commence, bonne chance à tous !\n");
}
Console.WriteLine(" Codes Indicateurs ");
Console.WriteLine("──────────────── ────────────────");
Console.WriteLine("│ │ │ │");
for (int i = 0; i < grilleT.Length; ++i) public static void NouveauTour(Object? sender, NouveauTourEventArgs e)
{ {
Console.Write("│ "); DessinerSeparateur();
DessinerCode(grilleT[i]);
Console.Write(" │");
Console.Write(" "); Console.WriteLine($"Tour {e.Tour} - {e.Joueur.Nom}\n");
Console.Write("│ "); DessinerPlateau(e.Grille, e.Indicateurs);
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; Console.TreatControlCAsInput = true;
bool aChoisi = false; bool aChoisi = false;
int indice = 0; int indice = 0;
Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur));
Couleur couleur = couleurs[indice];
while (!aChoisi) while (!aChoisi)
{ {
DessinerJeton(new Jeton(couleur)); DessinerPion(couleurs[indice]);
Console.Write("\b\b\b"); Console.Write("\b\b\b");
ConsoleKey touche = Console.ReadKey(true).Key; switch (Console.ReadKey(true).Key)
switch (touche)
{ {
case ConsoleKey.Enter: case ConsoleKey.Enter:
aChoisi = true; aChoisi = true;
@ -190,8 +183,8 @@ namespace ConsoleApp
++indice; ++indice;
break; break;
case ConsoleKey.Escape: /*case ConsoleKey.Escape:
return null; return null;*/
default: default:
break; break;
@ -201,38 +194,40 @@ namespace ConsoleApp
indice = couleurs.Length - 1; indice = couleurs.Length - 1;
else if (indice >= couleurs.Length) else if (indice >= couleurs.Length)
indice = 0; indice = 0;
couleur = couleurs[indice];
} }
Console.TreatControlCAsInput = false; 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()) DessinerPion(e.Jeton.Couleur);
{ }
Jeton? jeton = ChoixJeton();
if (jeton.HasValue) public static void AjouterCode(Object? sender, AjouterCodeEventArgs e)
{ {
DessinerJeton(jeton.Value); Console.WriteLine();
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");
}
} 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...");
} }
} }
} }

@ -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")
{ }
}
}

@ -2,9 +2,5 @@
{ {
public class DebutPartieEventArgs : EventArgs public class DebutPartieEventArgs : EventArgs
{ {
public DebutPartieEventArgs()
{
}
} }
} }

@ -0,0 +1,7 @@

namespace CoreLibrary.Events
{
public class DemanderJetonEventArgs : EventArgs
{
}
}

@ -0,0 +1,12 @@
namespace CoreLibrary.Events
{
public class DemanderJoueurEventArgs : EventArgs
{
public int Numero { get; private set; }
public DemanderJoueurEventArgs(int numero)
{
Numero = numero;
}
}
}

@ -1,14 +1,20 @@
namespace CoreLibrary.Events using CoreLibrary.Exceptions;
namespace CoreLibrary.Events
{ {
public class NouveauTourEventArgs : EventArgs public class NouveauTourEventArgs : EventArgs
{ {
public Joueur Joueur { get; private set; } public Joueur Joueur { get; private set; }
public int Tour { get; private set; } public int Tour { get; private set; }
public IEnumerable<IEnumerable<Jeton?>> Grille { get; private set; }
public IEnumerable<IEnumerable<Indicateur>> Indicateurs { get; private set; }
public NouveauTourEventArgs(Joueur joueur, int tour) public NouveauTourEventArgs(Joueur joueur, int tour, IEnumerable<IEnumerable<Jeton?>> grille, IEnumerable<IEnumerable<Indicateur>> indicateurs)
{ {
Joueur = joueur; Joueur = joueur;
Tour = tour; Tour = tour;
Grille = grille;
Indicateurs = indicateurs;
} }
} }
} }

@ -1,13 +1,6 @@
 namespace CoreLibrary.Events
using System.Security.Cryptography.X509Certificates;
namespace CoreLibrary.Events
{ {
public class PasserMainEventArgs : EventArgs public class PasserMainEventArgs : EventArgs
{ {
public PasserMainEventArgs()
{
}
} }
} }

@ -4,23 +4,33 @@ namespace CoreLibrary
{ {
public class Partie public class Partie
{ {
private IRegles regles; private readonly IRegles regles;
public event EventHandler<AjouterJoueursEventArgs> ajouterJoueur; public delegate string? StringEventHandler<TEventArgs>(Object? sender, TEventArgs e);
public event EventHandler<DebutPartieEventArgs> debutPartie; public delegate Jeton JetonEventHandler<TEventArgs>(Object? sender, TEventArgs e);
public event EventHandler<NouveauTourEventArgs> nouveauTour;
public event EventHandler<AjouterJetonEventArgs> ajouterJeton;
public event EventHandler<AjouterCodeEventArgs> ajouterCode;
public event EventHandler<PasserMainEventArgs> passerMain;
public event EventHandler<PartieTermineeEventArgs> partieTerminee;
private void QuandAjouterJoueur(Joueur joueur) => ajouterJoueur?.Invoke(this, new AjouterJoueursEventArgs(joueur)); public event StringEventHandler<DemanderJoueurEventArgs>? DemanderJoueur;
private void QuandDebutPartie() => debutPartie?.Invoke(this, new DebutPartieEventArgs()); public event JetonEventHandler<DemanderJetonEventArgs>? DemanderJeton;
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)); public event EventHandler<AjouterJoueursEventArgs>? AjouterJoueur;
private void QuandNouveauCode(Code code) => ajouterCode?.Invoke(this, new AjouterCodeEventArgs(code)); public event EventHandler<DebutPartieEventArgs>? DebutPartie;
private void QuandPasserMain() => passerMain?.Invoke(this, new PasserMainEventArgs()); public event EventHandler<NouveauTourEventArgs>? NouveauTour;
private void QuandPartieTerminee(IEnumerable<Joueur> gagnants, IEnumerable<Joueur> perdants) => partieTerminee?.Invoke(this, new PartieTermineeEventArgs(gagnants, perdants)); public event EventHandler<AjouterJetonEventArgs>? AjouterJeton;
public event EventHandler<AjouterCodeEventArgs>? AjouterCode;
public event EventHandler<PasserMainEventArgs>? PasserMain;
public event EventHandler<PartieTermineeEventArgs>? 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<IEnumerable<Jeton?>> grille, IEnumerable<IEnumerable<Indicateur>> 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<Joueur> gagnants, IEnumerable<Joueur> perdants) => PartieTerminee?.Invoke(this, new PartieTermineeEventArgs(gagnants, perdants));
public Partie(IRegles regles) public Partie(IRegles regles)
@ -30,7 +40,12 @@ namespace CoreLibrary
public void Jouer() 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(); regles.CommencerLaPartie();
QuandDebutPartie(); QuandDebutPartie();
@ -40,14 +55,15 @@ namespace CoreLibrary
Joueur joueurCourant = regles.JoueurCourant(); Joueur joueurCourant = regles.JoueurCourant();
Plateau plateauCourant = joueurCourant.Plateau; Plateau plateauCourant = joueurCourant.Plateau;
QuandNouveauTour(joueurCourant, plateauCourant.Tour); QuandNouveauTour(joueurCourant, plateauCourant.Tour, plateauCourant.Grille(), plateauCourant.Indicateurs());
Code code = regles.GenererCode(); Code code = regles.GenererCode();
while (!code.EstComplet()) while (!code.EstComplet())
{ {
AjouterJeton(code); Jeton jeton = QuandDemanderJeton() ?? new Jeton();
code.AjouterJeton(jeton);
QuandNouveauJeton(jeton);
} }
plateauCourant.AjouterCode(code); plateauCourant.AjouterCode(code);
QuandNouveauCode(code); QuandNouveauCode(code);
@ -56,33 +72,11 @@ namespace CoreLibrary
QuandPasserMain(); 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.Gagnants();
regles.Perdants(); regles.Perdants();
}
QuandPartieTerminee(regles.Gagnants(), regles.Perdants());
}
} }
} }

Loading…
Cancel
Save