Interface Règles et implémentation Partie

master
Céleste BARBOSA 1 year ago
parent 9218a53fcd
commit f4b986c5f2

@ -0,0 +1,18 @@
namespace CoreLibrary
{
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);
Joueur JoueurCourant();
Joueur[] Joueurs();
CombinaisonJoueur Plateau();
void PasserLaMain();
}
}

@ -1,72 +1,135 @@
namespace CoreLibrary namespace CoreLibrary
{ {
/// <summary> internal class Partie : IRegles
/// Représente une partie du jeu.
/// </summary>
public class Partie
{ {
private int tour; private static readonly int maximumJoueur = 2;
private Joueur joueur1; private static readonly int maximumTour = 12;
private Joueur joueur2;
private Joueur joueurCourant;
/// <summary> private int? indice;
/// Initialise une nouvelle instance de la classe Partie avec les noms des joueurs. private readonly Joueur[] joueurs = new Joueur[maximumJoueur];
/// </summary>
/// <param name="nomJoueur1">Le nom du premier joueur.</param> private int tour = 1;
/// <param name="nomJoueur2">Le nom du deuxième joueur.</param>
public Partie(string nomJoueur1, string nomJoueur2) public void AjouterJoueur(Joueur joueur)
{
if(joueurs.Length >= maximumJoueur)
{
throw new Exception("Nombre de joueurs maximum atteint");
}
joueurs.Append(joueur);
}
public void CommencerPartie()
{ {
this.tour = 1; indice = 0;
this.joueur1 = new Joueur(nomJoueur1);
this.joueur2 = new Joueur(nomJoueur2);
this.joueurCourant = this.joueur1;
} }
/// <summary> public bool EstCombinaisonValide(CombinaisonJoueur combinaison)
/// Récupère le joueur qui joue actuellement.
/// </summary>
/// <returns>Le joueur actuel.</returns>
public Joueur GetJoueur()
{ {
return this.joueurCourant; return combinaison.EstComplete();
} }
/// <summary> public bool EstTermine()
/// Passe la main au prochain joueur.
/// </summary>
public void PasserlaMain()
{ {
if (this.joueurCourant == this.joueur1) if (!indice.HasValue || indice != 0)
return false;
if (tour > maximumTour)
return true;
for (int i = 0; i < joueurs.Length; ++i)
{ {
this.joueurCourant = this.joueur2; if (joueurs[i].AGagne())
return true;
}
return false;
} }
else
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)
{ {
this.joueurCourant = this.joueur1; if (joueurs[i].AGagne())
gagnants.Append(joueurs[i]);
} }
tour++;
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);
} }
/// <summary> public Joueur JoueurCourant()
/// Détermine si la partie est terminée.
/// </summary>
/// <returns>True si la partie est terminée, sinon False.</returns>
public bool EstTerminer()
{ {
const int nbMaxTour = 24; if (!indice.HasValue)
if (joueurCourant.AGagne()) throw new Exception("Partie non démarrée");
return joueurs[indice.Value];
}
public Joueur[] Joueurs()
{ {
return true; return joueurs;
} }
if (tour == nbMaxTour) public void PasserLaMain()
{ {
return true; if (!indice.HasValue)
throw new Exception("Partie non démarrée");
++indice;
if (indice >= joueurs.Length)
{
++tour;
indice = 0;
}
} }
return false; 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;
}
}
}

Loading…
Cancel
Save