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

Loading…
Cancel
Save