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

Loading…
Cancel
Save