You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.1 KiB
119 lines
4.1 KiB
using CoreLibrary.Persistance;
|
|
using CoreLibrary.Core;
|
|
using CoreLibrary.Evenements;
|
|
using CoreLibrary.Joueurs;
|
|
using CoreLibrary.Regles;
|
|
using System.Runtime.Serialization;
|
|
|
|
namespace CoreLibrary
|
|
{
|
|
[DataContract]
|
|
[KnownType(typeof(ReglesClassiques))]
|
|
public class Partie : IEstPersistant
|
|
{
|
|
public event EventHandler<PartieDemanderJoueurEventArgs>? PartieDemanderJoueur;
|
|
public event EventHandler<PartieDebutPartieEventArgs>? PartieDebutPartie;
|
|
public event EventHandler<PartieNouveauTourEventArgs>? PartieNouveauTour;
|
|
public event EventHandler<PartiePasserLaMainEventArgs>? PartiePasserLaMain;
|
|
public event EventHandler<PartiePartieTermineeEventArgs>? PartiePartieTerminee;
|
|
|
|
private void QuandPartieDemanderJoueur(Joueur joueurDemande) => PartieDemanderJoueur?.Invoke(this, new PartieDemanderJoueurEventArgs(joueurs.Count + 1, joueurDemande));
|
|
private void QuandPartieDebutPartie() => PartieDebutPartie?.Invoke(this, new PartieDebutPartieEventArgs());
|
|
private void QuandPartieNouveauTour() => PartieNouveauTour?.Invoke(this, new PartieNouveauTourEventArgs(plateaux.ElementAt(courant).Taille + 1, joueurs.ElementAt(courant), plateaux.ElementAt(courant), new Code(Regles.TailleCode)));
|
|
private void QuandPartiePasserLaMain() => PartiePasserLaMain?.Invoke(this, new PartiePasserLaMainEventArgs(joueurs.ElementAt(courant)));
|
|
private void QuandPartiePartieTerminee(IReadOnlyList<Joueur> gagnants, IReadOnlyList<Joueur> perdants) => PartiePartieTerminee?.Invoke(this, new PartiePartieTermineeEventArgs(gagnants, perdants));
|
|
|
|
[DataMember]
|
|
private readonly List<Joueur> joueurs = new List<Joueur>();
|
|
[DataMember]
|
|
private readonly List<Plateau> plateaux = new List<Plateau>();
|
|
[DataMember]
|
|
private int courant = 0;
|
|
|
|
[DataMember]
|
|
public IRegles Regles { get; private init; }
|
|
|
|
public Partie(IRegles regles)
|
|
{
|
|
Regles = regles;
|
|
}
|
|
|
|
public void Jouer()
|
|
{
|
|
DemanderJoueur();
|
|
}
|
|
|
|
private void DemanderJoueur()
|
|
{
|
|
Joueur joueurDemande = new Joueur();
|
|
joueurDemande.JoueurSeConnecter += JoueurConnecte;
|
|
|
|
QuandPartieDemanderJoueur(joueurDemande);
|
|
}
|
|
|
|
private void JoueurConnecte(object? sender, JoueurSeConnecterEventArgs e)
|
|
{
|
|
Plateau plateau = new Plateau(Regles.TailleCode, Regles.NbTour);
|
|
plateau.PlateauAjouterCode += PlateauAjouterCode;
|
|
|
|
joueurs.Add(e.JoueurConnecte);
|
|
plateaux.Add(plateau);
|
|
|
|
if (joueurs.Count < Regles.NbJoueurs)
|
|
{
|
|
DemanderJoueur();
|
|
}
|
|
else
|
|
{
|
|
DebutPartie();
|
|
}
|
|
}
|
|
|
|
private void DebutPartie()
|
|
{
|
|
QuandPartieDebutPartie();
|
|
NouveauTour();
|
|
}
|
|
|
|
private void NouveauTour()
|
|
{
|
|
QuandPartieNouveauTour();
|
|
}
|
|
|
|
private void PlateauAjouterCode(object? sender, PlateauAjouterCodeEventArgs e)
|
|
{
|
|
QuandPartiePasserLaMain();
|
|
|
|
if (courant + 1 == joueurs.Count && (e.Plateau.Complet || plateaux.Any(plateau => plateau.Victoire)))
|
|
{
|
|
PartieTerminee();
|
|
}
|
|
else
|
|
{
|
|
if (courant + 1 == joueurs.Count)
|
|
courant = 0;
|
|
else
|
|
++courant;
|
|
|
|
NouveauTour();
|
|
}
|
|
}
|
|
|
|
private void PartieTerminee()
|
|
{
|
|
List<Joueur> gagnants = new List<Joueur>();
|
|
List<Joueur> perdants = new List<Joueur>();
|
|
|
|
for (int i = 0; i < joueurs.Count; ++i)
|
|
{
|
|
if (plateaux.ElementAt(i).Victoire)
|
|
gagnants.Add(joueurs[i]);
|
|
else
|
|
perdants.Add(joueurs[i]);
|
|
}
|
|
|
|
QuandPartiePartieTerminee(gagnants, perdants);
|
|
}
|
|
}
|
|
}
|