Nouvelle version du core
continuous-integration/drone/push Build is passing Details

master
Céleste BARBOSA 1 year ago
parent 070c71bb1e
commit e624363abf

@ -0,0 +1,102 @@
namespace CoreLibrary
{
public class Code
{
private readonly Jeton?[] lesJetons;
public int NbJetons { get; private set; } = 0;
public Code(int tailleCode)
{
lesJetons = new Jeton?[tailleCode];
}
public Code(IEnumerable<Jeton?> jetons)
{
lesJetons = jetons.ToArray();
}
public void AjouterJeton(Jeton jeton)
{
lesJetons[NbJetons++] = jeton;
}
public void SupprimerDernierJeton()
{
lesJetons[NbJetons--] = null;
}
public Jeton RecupererJeton(int indice)
{
Jeton? jeton = lesJetons[indice];
if (!jeton.HasValue)
throw new Exception();
return jeton.Value;
}
public IEnumerable<Jeton?> Jetons()
{
return lesJetons;
}
public bool EstComplet()
{
return NbJetons == lesJetons.Length;
}
public int TailleMaximale()
{
return lesJetons.Length;
}
public IEnumerable<Indicateur> Comparer(Code autreCode)
{
// Mon code est le code correct, l'autre code est celui qui teste
Indicateur[] indicateurs = [];
Jeton?[] mesJetons = Jetons().ToArray();
Jeton?[] sesJetons = autreCode.Jetons().ToArray();
for (int i = 0; i < mesJetons.Length; ++i)
{
Jeton? monJeton = mesJetons[i];
Jeton? sonJeton = sesJetons[i];
if (monJeton.HasValue && sonJeton.HasValue && monJeton.Value.Couleur.Equals(sonJeton.Value.Couleur))
{
indicateurs = indicateurs.Append(Indicateur.BONNEPLACE).ToArray();
mesJetons[i] = null;
sesJetons[i] = null;
}
}
for (int i = 0; i < sesJetons.Length; ++i)
{
Jeton? sonJeton = sesJetons[i];
if (sonJeton.HasValue)
{
for (int j = 0; j < mesJetons.Length; ++j)
{
Jeton? monJeton = mesJetons[j];
if (monJeton.HasValue && sonJeton.Value.Couleur.Equals(monJeton.Value.Couleur))
{
indicateurs = indicateurs.Append(Indicateur.BONNECOULEUR).ToArray();
mesJetons[j] = null;
sesJetons[i] = null;
break;
}
}
}
}
return indicateurs;
}
}
}

@ -1,16 +0,0 @@
namespace CoreLibrary
{
public abstract class Combinaison
{
protected static readonly int taillePhysique = 4;
public int TailleLogique { get; private set; } = 0;
private readonly Jeton[] lesJetons = new Jeton[taillePhysique];
protected void AjouterJeton(Jeton jeton) => lesJetons[TailleLogique++] = jeton;
public Jeton GetJeton(int indice) => lesJetons[indice];
protected void SupprimerDernierJeton() => --TailleLogique;
}
}

@ -1,72 +0,0 @@
namespace CoreLibrary
{
public class CombinaisonIndicateur : Combinaison
{
public CombinaisonIndicateur(CombinaisonJoueur combinaisonJoueur, CombinaisonSecrete combinaisonSecrete)
{
if(!combinaisonJoueur.EstComplete())
{
throw new ArgumentException("Combinaison non complète");
}
JetonJoueur?[] lesJetonsJoueur = new JetonJoueur?[combinaisonJoueur.TailleLogique];
for(int i = 0; i < combinaisonJoueur.TailleLogique; ++i)
{
lesJetonsJoueur[i] = (JetonJoueur) combinaisonJoueur.GetJeton(i);
}
JetonJoueur?[] lesJetonsSecret = new JetonJoueur?[combinaisonSecrete.TailleLogique];
for (int i = 0; i < combinaisonSecrete.TailleLogique; ++i)
{
lesJetonsSecret[i] = (JetonJoueur)combinaisonSecrete.GetJeton(i);
}
for (int i = 0; i < Combinaison.taillePhysique; ++i)
{
JetonJoueur? jetonJoueur = lesJetonsJoueur[i];
JetonJoueur? jetonSecret = lesJetonsSecret[i];
if (jetonJoueur is not null && jetonSecret is not null && ComparerJeton(jetonJoueur, jetonSecret))
{
AjouterJetonBienPlace();
lesJetonsJoueur[i] = null;
lesJetonsSecret[i] = null;
}
}
for (int i = 0; i < Combinaison.taillePhysique; ++i)
{
JetonJoueur? jetonJoueur = lesJetonsJoueur[i];
if (jetonJoueur is not null && ContientJeton(jetonJoueur, lesJetonsSecret))
{
AjouterJetonBonneCouleur();
lesJetonsJoueur[i] = null;
lesJetonsSecret[i] = null;
}
}
}
private void AjouterJetonBienPlace()
{
JetonIndicateur jeton = new JetonIndicateur(Couleur.Noir);
base.AjouterJeton(jeton);
}
private void AjouterJetonBonneCouleur()
{
JetonIndicateur jeton = new JetonIndicateur(Couleur.Blanc);
base.AjouterJeton(jeton);
}
private bool ComparerJeton(JetonJoueur jetonJoueur, JetonJoueur jetonSecret)
{
return jetonJoueur.Couleur == jetonSecret.Couleur;
}
private bool ContientJeton(JetonJoueur jetonJoueur, JetonJoueur?[] jetonsSecret)
{
return Array.IndexOf(jetonsSecret, jetonJoueur) != -1;
}
}
}

@ -1,12 +0,0 @@
namespace CoreLibrary
{
public class CombinaisonJoueur : Combinaison
{
public void AjouterJeton(JetonJoueur jetonJoueur) => base.AjouterJeton(jetonJoueur);
public new void SupprimerDernierJeton() => base.SupprimerDernierJeton();
public bool EstComplete() => base.TailleLogique == Combinaison.taillePhysique;
}
}

@ -1,47 +0,0 @@
namespace CoreLibrary
{
public class CombinaisonSecrete : Combinaison
{
private static readonly Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur));
private static readonly Random rand = new Random();
public CombinaisonSecrete()
{
for(int i = 0; i < Combinaison.taillePhysique; i++) {
this.AjouterJetonAleatoire();
}
}
private void AjouterJetonAleatoire()
{
JetonJoueur jeton = CombinaisonSecrete.GenererJetonAleatoire();
base.AjouterJeton(jeton);
}
private static JetonJoueur GenererJetonAleatoire()
{
int indice = rand.Next(couleurs.Length);
Couleur couleur = couleurs[indice];
return new JetonJoueur(couleur);
}
public bool EstEgale(CombinaisonJoueur combinaisonJoueur)
{
if(!combinaisonJoueur.EstComplete())
{
throw new Exception("Combinaison imcomplete");
}
for(int indice = 0; indice < CombinaisonSecrete.taillePhysique; ++indice)
{
if(this.GetJeton(indice).Couleur != combinaisonJoueur.GetJeton(indice).Couleur)
{
return false;
}
}
return true;
}
}
}

@ -0,0 +1,12 @@
namespace CoreLibrary
{
public enum Couleur
{
ROUGE,
VERT,
BLEU,
JAUNE,
BLANC,
NOIR
}
}

@ -1,15 +0,0 @@
namespace CoreLibrary
{
/// <summary>
/// Énumération représentant les 6 couleurs que peuvent prendre les jetons.
/// </summary>
public enum Couleur
{
Rouge,
Bleu,
Vert,
Jaune,
Noir = 100,
Blanc,
}
}

@ -2,17 +2,24 @@
{
public interface IRegles
{
void AjouterJoueur(Joueur joueur);
void CommencerPartie();
bool EstTermine();
Joueur[] Gagnant();
Joueur[] Perdant();
int Tour();
void JouerCombinaison(CombinaisonJoueur combinaison);
bool EstCombinaisonValide(CombinaisonJoueur combinaison);
string Nom { get; }
int TourMaximum { get; }
int TailleCodeMaximum { get; }
int NbJoueurs { get; }
int NbJoueursMaximum { get; }
void AjouterJoueur(string nom);
Joueur JoueurCourant();
Joueur[] Joueurs();
CombinaisonJoueur Plateau();
void PasserLaMain();
Code GenererCode();
void CommencerLaPartie();
bool EstTerminee();
IEnumerable<Joueur> Gagnants();
IEnumerable<Joueur> Perdants();
}
}

@ -0,0 +1,8 @@
namespace CoreLibrary
{
public enum Indicateur
{
BONNEPLACE,
BONNECOULEUR
}
}

@ -1,26 +1,12 @@
namespace CoreLibrary
{
/// <summary>
/// Représente un jeton de jeu qui permet de récupérer et de modifier sa couleur.
/// </summary>
public abstract class Jeton
{
/// <summary>
/// Initialise une nouvelle instance de la classe Jeton avec la couleur spécifiée.
/// </summary>
/// <param name="couleur">La couleur du jeton.</param>
protected Jeton(Couleur couleur)
{
this.Couleur = couleur;
}
/// <summary>
/// Obtient la couleur du jeton.
/// </summary>
public Couleur Couleur
{
get;
private set;
}
}
}
namespace CoreLibrary
{
public struct Jeton
{
public readonly Couleur Couleur { get; private init; }
public Jeton(Couleur couleur)
{
Couleur = couleur;
}
}
}

@ -1,25 +0,0 @@
namespace CoreLibrary
{
/// <summary>
/// Représente un jeton indicateur, une classe dérivée de la classe Jeton.
/// </summary>
public class JetonIndicateur : Jeton
{
/// <summary>
/// Initialise une nouvelle instance de la classe JetonIndicateur avec la couleur spécifiée.
/// </summary>
/// <param name="couleur">La couleur du jeton.</param>
/// <exception cref="ArgumentException">Levée si la couleur spécifiée n'est pas Noir ou Blanc.</exception>
public JetonIndicateur(Couleur couleur)
: base(couleur)
{
if (couleur < Couleur.Noir)
{
throw new ArgumentException("La couleur doit être Noir ou Blanc");
}
}
}
}

@ -1,19 +0,0 @@
namespace CoreLibrary
{
/// <summary>
/// Représente un jeton joueur, une classe dérivée de la classe Jeton.
/// </summary>
public class JetonJoueur : Jeton
{
/// <summary>
/// Initialise une nouvelle instance de la classe JetonJoueur avec la couleur spécifiée.
/// </summary>
/// <param name="couleur">La couleur du jeton.</param>
public JetonJoueur(Couleur couleur)
: base(couleur)
{
}
}
}

@ -1,48 +1,14 @@
namespace CoreLibrary
{
/// <summary>
/// Représente un joueur dans une partie.
/// </summary>
public class Joueur
{
private Plateau plateau;
private bool gagne;
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="Joueur"/> avec le nom spécifié.
/// </summary>
/// <param name="nom">Le nom du joueur.</param>
public Joueur(string nom)
{
this.Nom = nom;
this.plateau = new Plateau();
}
/// <summary>
/// Obtient ou définit le nom du joueur.
/// </summary>
public string Nom
{
get;
set;
}
/// <summary>
/// Joue une combinaison pour ce joueur sur le plateau.
/// </summary>
/// <param name="combinaisonJoueur">La combinaison à jouer.</param>
public void JouerCombinaison(CombinaisonJoueur combinaisonJoueur)
{
gagne = plateau.AjouterCombinaison(combinaisonJoueur);
}
/// <summary>
/// Détermine si le joueur a gagné la partie.
/// </summary>
/// <returns>True si le joueur a gagné, sinon False.</returns>
public bool AGagne()
{
return gagne;
}
}
}
namespace CoreLibrary
{
public class Joueur
{
public string Nom { get; private init; }
public Plateau Plateau { get; private init; }
public Joueur(string nom, Plateau plateau)
{
Nom = nom;
Plateau = plateau;
}
}
}

@ -1,135 +0,0 @@
namespace CoreLibrary
{
internal class Partie : IRegles
{
private static readonly int maximumJoueur = 2;
private static readonly int maximumTour = 12;
private int? indice;
private readonly Joueur[] joueurs = new Joueur[maximumJoueur];
private int tour = 1;
public void AjouterJoueur(Joueur joueur)
{
if(joueurs.Length >= maximumJoueur)
{
throw new Exception("Nombre de joueurs maximum atteint");
}
joueurs.Append(joueur);
}
public void CommencerPartie()
{
indice = 0;
}
public bool EstCombinaisonValide(CombinaisonJoueur combinaison)
{
return combinaison.EstComplete();
}
public bool EstTermine()
{
if (!indice.HasValue || indice != 0)
return false;
if (tour > maximumTour)
return true;
for (int i = 0; i < joueurs.Length; ++i)
{
if (joueurs[i].AGagne())
return true;
}
return false;
}
public Joueur[] Gagnant()
{
Joueur[] gagnants = new Joueur[joueurs.Length];
if (!indice.HasValue)
throw new Exception("Partie non démarrée");
if (!EstTermine())
throw new Exception("Partie non terminée");
for (int i = 0; i < joueurs.Length; ++i)
{
if (joueurs[i].AGagne())
gagnants.Append(joueurs[i]);
}
return gagnants;
}
public void JouerCombinaison(CombinaisonJoueur combinaison)
{
if (!indice.HasValue)
throw new Exception("Partie non démarrée");
if (!EstCombinaisonValide(combinaison))
throw new Exception("Combinaison n'est pas valide");
joueurs[indice.Value].JouerCombinaison(combinaison);
}
public Joueur JoueurCourant()
{
if (!indice.HasValue)
throw new Exception("Partie non démarrée");
return joueurs[indice.Value];
}
public Joueur[] Joueurs()
{
return joueurs;
}
public void PasserLaMain()
{
if (!indice.HasValue)
throw new Exception("Partie non démarrée");
++indice;
if (indice >= joueurs.Length)
{
++tour;
indice = 0;
}
}
public Joueur[] Perdant()
{
if (!indice.HasValue)
throw new Exception("Partie non démarrée");
if (!EstTermine())
throw new Exception("Partie non terminée");
Joueur[] perdants = new Joueur[joueurs.Length];
for (int i = 0; i < joueurs.Length; ++i)
{
if (!joueurs[i].AGagne())
perdants.Append(joueurs[i]);
}
return perdants;
}
public CombinaisonJoueur Plateau()
{
throw new NotImplementedException();
}
public int Tour()
{
return tour;
}
}
}

@ -1,43 +1,96 @@
namespace CoreLibrary
{
/// <summary>
/// Représente le plateau de jeu qui initialise deux tableaux de taille 12.
/// Il possède deux méthodes : une pour ajouter une combinaison dans le tableau et une autre pour vérifier si le tableau est plein.
/// </summary>
public class Plateau
{
private static readonly int tailleMax = 12;
private CombinaisonSecrete combinaisonSecrete = new CombinaisonSecrete();
private CombinaisonJoueur[] lesCombinaisonsJoueur = new CombinaisonJoueur[tailleMax];
private Combinaison[] lesCombinaisonsIndicateur = new CombinaisonIndicateur[tailleMax];
private int index = 0;
/// <summary>
/// Ajoute une combinaison de joueur au plateau.
/// </summary>
/// <param name="combinaisonJoueur">La combinaison du joueur à ajouter.</param>
/// <returns>True si la combinaison correspond à la combinaison secrète, sinon False.</returns>
public bool AjouterCombinaison(CombinaisonJoueur combinaisonJoueur)
{
if (EstComplet())
{
throw new Exception("Le plateau est plein, impossible d'ajouter une combinaison supplémentaire.");
}
lesCombinaisonsJoueur[index] = combinaisonJoueur;
index++;
return combinaisonSecrete.EstEgale(combinaisonJoueur);
}
/// <summary>
/// Vérifie si le plateau est complet.
/// </summary>
/// <returns>True si le plateau est plein, sinon False.</returns>
public bool EstComplet()
{
return index >= tailleMax;
}
}
}
namespace CoreLibrary
{
public class Plateau
{
private static Random random = new Random();
private readonly Code codeSecret;
private readonly Code?[] grille;
private readonly IEnumerable<Indicateur>[] indicateurs;
private readonly int tailleCode;
public int Tour { get; private set; } = 1;
public bool Victoire { get; private set; } = false;
public Plateau(int tailleCode, int tailleGrille)
{
codeSecret = new Code(tailleCode);
grille = new Code?[tailleGrille];
indicateurs = new IEnumerable<Indicateur>[tailleGrille];
this.tailleCode = tailleCode;
GenererCodeAleatoire();
foreach (Jeton? jeton in codeSecret.Jetons())
{
Console.WriteLine(jeton.Value.Couleur);
}
}
private void GenererCodeAleatoire()
{
Couleur[] couleurs = (Couleur[])Enum.GetValues(typeof(Couleur));
for (int i = 0; i < tailleCode; ++i)
{
codeSecret.AjouterJeton(new Jeton(couleurs[random.Next(couleurs.Length)]));
}
}
public bool EstComplet()
{
return Tour - 1 == grille.Length;
}
public void AjouterCode(Code code)
{
indicateurs[Tour - 1] = codeSecret.Comparer(code);
grille[Tour - 1] = code;
++Tour;
if (EstBonCode(code))
{
Victoire = true;
}
}
public bool EstBonCode(Code code)
{
IEnumerable<Indicateur> indicateurs = codeSecret.Comparer(code);
if (indicateurs.Count() != tailleCode)
{
return false;
}
foreach (Indicateur indicateur in indicateurs)
{
if (indicateur != Indicateur.BONNEPLACE)
{
return false;
}
}
return true;
}
public IEnumerable<IEnumerable<Jeton?>> Grille()
{
IEnumerable<Jeton?>[] grilleJetons = new IEnumerable<Jeton?>[grille.Length];
for (int i = 0; i < grille.Length; ++i)
{
grilleJetons[i] = grille[i]?.Jetons() ?? new Code(tailleCode).Jetons();
}
return grilleJetons;
}
public IEnumerable<IEnumerable<Indicateur>> Indicateurs()
{
return indicateurs;
}
}
}

@ -0,0 +1,109 @@
namespace CoreLibrary
{
public class ReglesClassiques : IRegles
{
private int nbJoueurs = 0;
private int? joueurCourant;
private readonly Joueur[] joueurs;
public string Nom { get => "Règles classiques"; }
public int TourMaximum { get => 12; }
public int TailleCodeMaximum { get => 4; }
public int NbJoueurs { get => nbJoueurs; }
public int NbJoueursMaximum { get => 2; }
public ReglesClassiques()
{
joueurs = new Joueur[NbJoueursMaximum];
}
public void AjouterJoueur(string nom)
{
joueurs[nbJoueurs++] = new Joueur(nom, new Plateau(TailleCodeMaximum, TourMaximum));
}
public Joueur JoueurCourant()
{
if (!joueurCourant.HasValue)
throw new Exception();
return joueurs[joueurCourant.Value];
}
public void PasserLaMain()
{
if (!joueurCourant.HasValue)
throw new Exception();
joueurCourant++;
if (joueurCourant >= joueurs.Length)
{
joueurCourant = 0;
}
}
public Code GenererCode()
{
return new Code(TailleCodeMaximum);
}
public void CommencerLaPartie()
{
joueurCourant = 0;
}
public bool EstTerminee()
{
if (!joueurCourant.HasValue || joueurCourant != 0)
return false;
if (JoueurCourant().Plateau.Tour > TourMaximum)
return true;
for (int i = 0; i < joueurs.Length; ++i)
{
if (joueurs[i].Plateau.Victoire)
return true;
}
return false;
}
public IEnumerable<Joueur> Gagnants()
{
Joueur[] gagnants = [];
for (int i = 0; i < joueurs.Length; ++i)
{
if (joueurs[i].Plateau.Victoire)
{
gagnants = gagnants.Append(joueurs[i]).ToArray();
}
}
return gagnants;
}
public IEnumerable<Joueur> Perdants()
{
Joueur[] perdants = [];
for (int i = 0; i < joueurs.Length; ++i)
{
if (!joueurs[i].Plateau.Victoire)
{
perdants = perdants.Append(joueurs[i]).ToArray();
}
}
return perdants;
}
}
}
Loading…
Cancel
Save