You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mastermind/Sources/ConsoleApp/Utils.cs

235 lines
7.4 KiB

using CoreLibrary;
using CoreLibrary.Events;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApp
{
public class Utils
{
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.DarkYellow }
};
private readonly static Dictionary<Indicateur, ConsoleColor> indicateursTerminal = new Dictionary<Indicateur, ConsoleColor>()
{
{Indicateur.BONNEPLACE, ConsoleColor.Black },
{Indicateur.BONNECOULEUR, ConsoleColor.White }
};
public static void DessinerTitre()
{
Console.WriteLine("""
__ __ _ _ _
| \/ | __ _ ___| |_ ___ _ _ _ __ (_) _ _ __| |
| |\/| |/ _` |(_-<| _|/ -_)| '_|| ' \ | || ' \ / _` |
|_| |_|\__,_|/__/ \__|\___||_| |_|_|_||_||_||_|\__,_|
""");
DessinerSeparateur();
}
public static void DessinerSeparateur()
{
Console.WriteLine("""
""");
}
public static void DessinerPion(Enum pion)
{
Console.Write(" ");
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("⬤");
Console.ResetColor();
Console.Write(" ");
}
public static void DessinerPlateau(IEnumerable<IEnumerable<Jeton?>> grille, IEnumerable<IEnumerable<Indicateur>> indicateurs)
{
IEnumerable<Jeton?>[] grilleTableau = (IEnumerable<Jeton?>[]) grille.ToArray();
IEnumerable<Indicateur>[] indicateursTableau = (IEnumerable<Indicateur>[]) indicateurs.ToArray();
Console.WriteLine(" Codes Indicateurs ");
Console.WriteLine("──────────────── ────────────────");
Console.WriteLine("│ │ │ │");
for (int i = 0; i < grille.Count(); ++i)
{
Console.Write("│ ");
Jeton?[] ligneGrille = grilleTableau[i].ToArray();
for (int j = 0; j < ligneGrille.Length; ++j)
{
Jeton? jeton = ligneGrille[j];
if(jeton.HasValue)
{
DessinerPion(jeton.Value.Couleur);
}
else
{
Console.Write(" ");
}
}
Console.Write(" │ │ ");
if(indicateursTableau[i] != null)
{
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.WriteLine(" │");
}
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...");
}
}
}
}