From f4b986c5f28abe4cff5e2d59374f49b9b079c258 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Sat, 13 Apr 2024 11:19:22 +0200 Subject: [PATCH] =?UTF-8?q?Interface=20R=C3=A8gles=20et=20impl=C3=A9mentat?= =?UTF-8?q?ion=20Partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreLibrary/IRegles.cs | 18 ++++ CoreLibrary/Partie.cs | 207 +++++++++++++++++++++++++++-------------- 2 files changed, 153 insertions(+), 72 deletions(-) create mode 100644 CoreLibrary/IRegles.cs diff --git a/CoreLibrary/IRegles.cs b/CoreLibrary/IRegles.cs new file mode 100644 index 0000000..9b040d2 --- /dev/null +++ b/CoreLibrary/IRegles.cs @@ -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(); + } +} diff --git a/CoreLibrary/Partie.cs b/CoreLibrary/Partie.cs index 1c16855..ef6c3a0 100644 --- a/CoreLibrary/Partie.cs +++ b/CoreLibrary/Partie.cs @@ -1,72 +1,135 @@ -namespace CoreLibrary -{ - /// - /// Représente une partie du jeu. - /// - public class Partie - { - private int tour; - private Joueur joueur1; - private Joueur joueur2; - private Joueur joueurCourant; - - /// - /// Initialise une nouvelle instance de la classe Partie avec les noms des joueurs. - /// - /// Le nom du premier joueur. - /// Le nom du deuxième joueur. - public Partie(string nomJoueur1, string nomJoueur2) - { - this.tour = 1; - this.joueur1 = new Joueur(nomJoueur1); - this.joueur2 = new Joueur(nomJoueur2); - this.joueurCourant = this.joueur1; - } - - /// - /// Récupère le joueur qui joue actuellement. - /// - /// Le joueur actuel. - public Joueur GetJoueur() - { - return this.joueurCourant; - } - - /// - /// Passe la main au prochain joueur. - /// - public void PasserlaMain() - { - if (this.joueurCourant == this.joueur1) - { - this.joueurCourant = this.joueur2; - } - else - { - this.joueurCourant = this.joueur1; - } - tour++; - } - - /// - /// Détermine si la partie est terminée. - /// - /// True si la partie est terminée, sinon False. - public bool EstTerminer() - { - const int nbMaxTour = 24; - if (joueurCourant.AGagne()) - { - return true; - } - - if (tour == nbMaxTour) - { - return true; - } - - return false; - } - } -} - +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; + } + } +}