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.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();

@ -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<Couleur, ConsoleColor> convertirCouleurs = new Dictionary<Couleur, ConsoleColor>()
private readonly static Dictionary<Couleur, ConsoleColor> couleursTerminal = new Dictionary<Couleur, ConsoleColor>()
{
{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<Indicateur, ConsoleColor> convertirIndicateurs = new Dictionary<Indicateur, ConsoleColor>()
private readonly static Dictionary<Indicateur, ConsoleColor> indicateursTerminal = new Dictionary<Indicateur, ConsoleColor>()
{
{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<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.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<Jeton?> 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<Indicateur> 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<IEnumerable<Jeton?>> grille, IEnumerable<IEnumerable<Indicateur>> indicateurs)
public static void CommencerLaPartie(Object? sender, DebutPartieEventArgs e)
{
IEnumerable<Jeton?>[] grilleT = grille.ToArray();
IEnumerable<Indicateur>[] 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...");
}
}
}

@ -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 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 Joueur Joueur { 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;
Tour = tour;
Grille = grille;
Indicateurs = indicateurs;
}
}
}

@ -1,13 +1,6 @@

using System.Security.Cryptography.X509Certificates;
namespace CoreLibrary.Events
namespace CoreLibrary.Events
{
public class PasserMainEventArgs : EventArgs
{
public PasserMainEventArgs()
{
}
}
}

@ -4,23 +4,33 @@ namespace CoreLibrary
{
public class Partie
{
private IRegles regles;
private readonly IRegles regles;
public event EventHandler<AjouterJoueursEventArgs> ajouterJoueur;
public event EventHandler<DebutPartieEventArgs> debutPartie;
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;
public delegate string? StringEventHandler<TEventArgs>(Object? sender, TEventArgs e);
public delegate Jeton JetonEventHandler<TEventArgs>(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<Joueur> gagnants, IEnumerable<Joueur> perdants) => partieTerminee?.Invoke(this, new PartieTermineeEventArgs(gagnants, perdants));
public event StringEventHandler<DemanderJoueurEventArgs>? DemanderJoueur;
public event JetonEventHandler<DemanderJetonEventArgs>? DemanderJeton;
public event EventHandler<AjouterJoueursEventArgs>? AjouterJoueur;
public event EventHandler<DebutPartieEventArgs>? DebutPartie;
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 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)
@ -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());
}
}
}

Loading…
Cancel
Save