From 6cf14178bc153d90ff6ffef1c3b09edb3127dbfe Mon Sep 17 00:00:00 2001 From: victor perez ngounou Date: Sat, 22 Oct 2022 12:57:34 +0200 Subject: [PATCH] . --- Sources/BowlingLib/Model/Equipe.cs | 57 +++++--- Sources/BowlingLib/Model/Frame.cs | 82 ++++++------ Sources/BowlingLib/Model/Joueur.cs | 3 + Sources/BowlingLib/Model/Partie.cs | 3 + Sources/BowlingMaping/PartieDbDataManager.cs | 18 +-- Sources/Tests/BowlingAppUnitTest/UTPartie.cs | 122 ++++++++++++++++++ .../BowlingAppUnitTest/UnitTestJoueur.cs | 31 +++-- 7 files changed, 238 insertions(+), 78 deletions(-) diff --git a/Sources/BowlingLib/Model/Equipe.cs b/Sources/BowlingLib/Model/Equipe.cs index e22a31a..0723871 100644 --- a/Sources/BowlingLib/Model/Equipe.cs +++ b/Sources/BowlingLib/Model/Equipe.cs @@ -4,36 +4,39 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace BowlingLib.Model { + /// + /// Classe Model Equipe + /// public class Equipe { private string nom; private readonly long id; - + public List Joueurs = new List(); - + public string Nom { get { return nom; } set { nom = value; } } - + public Equipe(string nom, params Joueur[] joueurs) { this.nom = nom; AjouterJoueurs(joueurs); - // foreach (Joueur nouv in joueurs) AjouterJoueur(nouv); + // foreach (Joueur nouv in joueurs) AjouterJoueur(nouv); } - public long Id + public long Id { get { return id; } } @@ -44,13 +47,18 @@ namespace BowlingLib.Model this.nom = nom; } - public Equipe(long id, string nom, List joueurs) + public Equipe(long id, string nom, List joueurs) { this.id = id; Joueurs = joueurs; Nom = nom; } + /// + /// Ajoute une liste de joueur à l'équipe + /// + /// + /// public List AjouterJoueurs(params Joueur[] joueurs) { List result = new(); @@ -63,40 +71,57 @@ namespace BowlingLib.Model } return result; - + } + /// + /// Ajouter un joueur s'il n'exciste pas dans la bd + /// + /// + /// + public bool AjouterJoueur(Joueur joueur) { - if(!isExist(joueur)) + if (!isExist(joueur)) { Joueurs.Add(joueur); return true; - }else + } + else { return false; } } + /// + /// Supprimer un joueur + /// + /// + public void SupprimerJoueur(Joueur joueur) { Joueurs.Remove(joueur); } - //retourner la liste non modifiable des joueurs de l'équipe + /// + /// retourner la liste non modifiable des joueurs de l'équipe + /// + /// public List GetJoueurs() { return Joueurs; } - // Fonction permettant de vérifier si un joueur existe déjà dans la liste (l'équipe) + /// + /// Fonction permettant de vérifier si un joueur existe déjà dans la liste (l'équipe) + /// + /// + /// public bool isExist(Joueur nouvJoueur) { - - if (Joueurs.Contains(nouvJoueur)) return true; - - + if (Joueurs.Contains(nouvJoueur)) + return true; return false; } diff --git a/Sources/BowlingLib/Model/Frame.cs b/Sources/BowlingLib/Model/Frame.cs index ff00887..233a4c4 100644 --- a/Sources/BowlingLib/Model/Frame.cs +++ b/Sources/BowlingLib/Model/Frame.cs @@ -8,10 +8,14 @@ using System.Threading.Tasks; namespace BowlingLib.Model { + /// + /// Classe Model Frame + /// public class Frame { const int MAX_QUILLE = 10; - public int Numero { + public int Numero + { get { return numero; } set @@ -29,8 +33,9 @@ namespace BowlingLib.Model } - public int QuillesRestantes - { get + public int QuillesRestantes + { + get { return quillesRestantes; } @@ -41,12 +46,12 @@ namespace BowlingLib.Model } private int quillesRestantes; - public int QuillesTombees + public int QuillesTombees { get { return quillesTombees; - } + } set { this.quillesTombees = value; @@ -54,36 +59,39 @@ namespace BowlingLib.Model } private int quillesTombees; - public bool IsStrike { - get { + public bool IsStrike + { + get + { return isStrike; - } - set{ + } + set + { this.isStrike = value; - } + } } private bool isStrike; - public bool IsSpare - { - get + public bool IsSpare + { + get { return isPark; - } + } set { this.isPark = value; - } + } } private bool isPark; public bool IsFinished { get - { - return isFinished; - } - set + { + return isFinished; + } + set { this.isFinished = value; } @@ -91,14 +99,14 @@ namespace BowlingLib.Model } private bool isFinished; - public Lancer Lancer1 + public Lancer Lancer1 { get - { - return lancer1; + { + return lancer1; } - set - { + set + { this.lancer1 = value; } } @@ -136,18 +144,12 @@ namespace BowlingLib.Model /// /// /// - /// - /// - /// - /// /// /// /// - public Frame(long id, int numero, bool isStrike, bool isSpare, int lancer1, int lancer2, [AllowNull] int lancer3) : this(numero) + public Frame(long id, int numero, int lancer1, int lancer2, [AllowNull] int lancer3) : this(numero) { this.id = id; - IsStrike = isStrike; - IsSpare = isSpare; Lancer1 = new Lancer(lancer1); Lancer2 = new Lancer(lancer2); Lancer3 = new Lancer(lancer3); @@ -169,6 +171,7 @@ namespace BowlingLib.Model throw new ArgumentException("Le nombre de quilles tombees doit et etre positif"); } + //Situation lor du dernier frame if (this.Numero == 10) { if (this.Lancer1 == null) @@ -179,6 +182,7 @@ namespace BowlingLib.Model if (quillesTombees == MAX_QUILLE) { this.IsStrike = true; + QuillesRestantes = MAX_QUILLE; } } else if (this.Lancer2 == null) @@ -186,11 +190,14 @@ namespace BowlingLib.Model this.Lancer2 = new Lancer(quillesTombees); this.QuillesRestantes -= quillesTombees; this.QuillesTombees += quillesTombees; + //lorsque le premier lancer est un strike if (this.IsStrike) { if (quillesTombees == MAX_QUILLE) { + //lorsque le lancer actuel est un strike this.IsStrike = true; + QuillesRestantes = MAX_QUILLE;//Remetre le nombre de quilles restantes à 10 pour le lancer 2 } else { @@ -201,8 +208,9 @@ namespace BowlingLib.Model { if (quillesTombees + this.Lancer1.QuillesTombees == MAX_QUILLE) { + //lorsque le lancer actuel est un spare this.IsSpare = true; - QuillesRestantes = 10; + QuillesRestantes = MAX_QUILLE;//Remetre le nombre de quilles restantes à 10 pour le lancer 3 } } } @@ -211,22 +219,22 @@ namespace BowlingLib.Model this.Lancer3 = new Lancer(quillesTombees); this.QuillesRestantes -= quillesTombees; this.QuillesTombees += quillesTombees; - if (this.IsStrike) + if (this.IsStrike)//si le deuxième lancer etait un strike { if (quillesTombees == MAX_QUILLE) { - this.IsStrike = true; + this.IsStrike = true;//cas ou il effectue un 3eme strike } else { this.IsStrike = false; } } - else if (this.IsSpare) + else if (this.IsSpare)//si le deuxième lancer etait un spare { if (quillesTombees + this.Lancer2.QuillesTombees == MAX_QUILLE) { - this.IsSpare = true; + this.IsSpare = true;//cas ou il effectue un 3eme spare } else { @@ -271,7 +279,7 @@ namespace BowlingLib.Model this.IsSpare = true; } } - if (this.QuillesRestantes == 0 || (this.Lancer2 != null && this.Numero != 10)) + if (this.QuillesRestantes == 0 || (this.Lancer2 != null && this.Numero != 10) || (!IsStrike && !IsSpare && Numero == 10)) { this.IsFinished = true; } diff --git a/Sources/BowlingLib/Model/Joueur.cs b/Sources/BowlingLib/Model/Joueur.cs index 11fef50..4413a6e 100644 --- a/Sources/BowlingLib/Model/Joueur.cs +++ b/Sources/BowlingLib/Model/Joueur.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace BowlingLib.Model { + /// + /// Classe Model Joueur + /// public class Joueur { private string pseudo; diff --git a/Sources/BowlingLib/Model/Partie.cs b/Sources/BowlingLib/Model/Partie.cs index d602ed3..d5bf968 100644 --- a/Sources/BowlingLib/Model/Partie.cs +++ b/Sources/BowlingLib/Model/Partie.cs @@ -7,6 +7,9 @@ using System.Threading.Tasks; namespace BowlingLib.Model { + /// + /// Classe Model Partie + /// public class Partie { public ReadOnlyCollection Frames { get; } diff --git a/Sources/BowlingMaping/PartieDbDataManager.cs b/Sources/BowlingMaping/PartieDbDataManager.cs index b9e0d11..99773fd 100644 --- a/Sources/BowlingMaping/PartieDbDataManager.cs +++ b/Sources/BowlingMaping/PartieDbDataManager.cs @@ -83,18 +83,18 @@ namespace BowlingMaping using (var context = new BowlingContext()) { PartieEntity entity = context.Parties.Find(name); - Joueur joueur = new Joueur(entity.Joueur.Id,entity.Joueur.Pseudo); + Joueur joueur = new Joueur(entity.Joueur.Id, entity.Joueur.Pseudo); List frames = new List(); foreach (FrameEntity frameEntity in entity.Frames) { - Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.IsStrike, frameEntity.IsSpare,frameEntity.Lancer1,frameEntity.Lancer2,frameEntity.Lancer3); + Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.Lancer1, frameEntity.Lancer2, frameEntity.Lancer3); frames.Add(frame); } - result = new Partie(entity.Id, joueur,frames, entity.Date, entity.Score); + result = new Partie(entity.Id, joueur, frames, entity.Date, entity.Score); } return result; } - + /// /// Retourne Toutes les parties en base de donné /// @@ -104,13 +104,13 @@ namespace BowlingMaping List result = new List(); using (var context = new BowlingContext()) { - foreach (PartieEntity entity in context.Parties.OrderBy(item=>item.Date)) + foreach (PartieEntity entity in context.Parties.OrderBy(item => item.Date)) { Joueur joueur = new Joueur(entity.Joueur.Id, entity.Joueur.Pseudo); List frames = new List(); foreach (FrameEntity frameEntity in entity.Frames) { - Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.IsStrike, frameEntity.IsSpare, frameEntity.Lancer1, frameEntity.Lancer2, frameEntity.Lancer3); + Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.Lancer1, frameEntity.Lancer2, frameEntity.Lancer3); frames.Add(frame); } result.Add(new Partie(entity.Id, joueur, frames, entity.Date, entity.Score)); @@ -118,14 +118,14 @@ namespace BowlingMaping } return result; } - + public IEnumerable GetAllWithDate(DateTime date) { List result = new List(); using (var context = new BowlingContext()) { - foreach (PartieEntity entity in context.Parties.OrderBy(item=>item.Date)) + foreach (PartieEntity entity in context.Parties.OrderBy(item => item.Date)) { if (entity.Date == date) { @@ -133,7 +133,7 @@ namespace BowlingMaping List frames = new List(); foreach (FrameEntity frameEntity in entity.Frames) { - Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.IsStrike, frameEntity.IsSpare, frameEntity.Lancer1, frameEntity.Lancer2, frameEntity.Lancer3); + Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.Lancer1, frameEntity.Lancer2, frameEntity.Lancer3); frames.Add(frame); } result.Add(new Partie(entity.Id, joueur, frames, entity.Date, entity.Score)); diff --git a/Sources/Tests/BowlingAppUnitTest/UTPartie.cs b/Sources/Tests/BowlingAppUnitTest/UTPartie.cs index af3ed50..5c27a41 100644 --- a/Sources/Tests/BowlingAppUnitTest/UTPartie.cs +++ b/Sources/Tests/BowlingAppUnitTest/UTPartie.cs @@ -160,5 +160,127 @@ namespace BowlingAppUnitTest //Assert Assert.Equal(90, score); } + + //le cas ou le joueur fait un strike au dernier lancer + + [Fact] + + public void TestGetScore5() + { + //Arrange + StubPartie stubPartie = new StubPartie(); + IEnumerable listParties = stubPartie.GetAll(1); + Partie partie = listParties.ElementAt(0); + partie.AddFrame(new Frame(1)); + partie.AddFrame(new Frame(2)); + partie.AddFrame(new Frame(3)); + partie.AddFrame(new Frame(4)); + partie.AddFrame(new Frame(5)); + partie.AddFrame(new Frame(6)); + partie.AddFrame(new Frame(7)); + partie.AddFrame(new Frame(8)); + partie.AddFrame(new Frame(9)); + partie.AddFrame(new Frame(10)); + + for (int i = 0; i < partie.Frames.Count; i++) + { + if (i < 9) + { + partie.Frames[i].Lancer(5); + partie.Frames[i].Lancer(4); + continue; + } + partie.Frames[i].Lancer(10); + if (partie.Frames[i].IsStrike) + { + partie.Frames[i].Lancer(5); + partie.Frames[i].Lancer(4); + } + } + + //Act + int? score = partie.GetScore(); + + //Assert + Assert.Equal(100, score); + } + + //le cas ou le joueur fait un spare au deuxieme lancer du dernier frame + + [Fact] + + public void TestGetScore6() + { + //Arrange + StubPartie stubPartie = new StubPartie(); + IEnumerable listParties = stubPartie.GetAll(1); + Partie partie = listParties.ElementAt(0); + partie.AddFrame(new Frame(1)); + partie.AddFrame(new Frame(2)); + partie.AddFrame(new Frame(3)); + partie.AddFrame(new Frame(4)); + partie.AddFrame(new Frame(5)); + partie.AddFrame(new Frame(6)); + partie.AddFrame(new Frame(7)); + partie.AddFrame(new Frame(8)); + partie.AddFrame(new Frame(9)); + partie.AddFrame(new Frame(10)); + + for (int i = 0; i < partie.Frames.Count; i++) + { + if (i < 9) + { + partie.Frames[i].Lancer(5); + partie.Frames[i].Lancer(4); + continue; + } + partie.Frames[i].Lancer(5); + partie.Frames[i].Lancer(5); + if (partie.Frames[i].IsSpare) + { + partie.Frames[i].Lancer(5); + } + } + + //Act + int? score = partie.GetScore(); + + //Assert + Assert.Equal(96, score); + } + + //le cas ou le nombre de lancer est atteind 2 eme frames + + [Fact] + + public void TestGetScore7() + { + //Arrange + StubPartie stubPartie = new StubPartie(); + IEnumerable listParties = stubPartie.GetAll(1); + Partie partie = listParties.ElementAt(0); + partie.AddFrame(new Frame(1)); + + partie.Frames[0].Lancer(5); + partie.Frames[0].Lancer(5); + Assert.Throws + (() => partie.Frames[0].Lancer(5)); + } + + //le cas ou les lancer sont finis + + [Fact] + + public void TestGetScore8() + { + //Arrange + StubPartie stubPartie = new StubPartie(); + IEnumerable listParties = stubPartie.GetAll(1); + Partie partie = listParties.ElementAt(0); + partie.AddFrame(new Frame(1)); + partie.Frames[0].Lancer(5); + partie.Frames[0].Lancer(5); + Assert.True(partie.Frames[0].IsFinished); + } } } \ No newline at end of file diff --git a/Sources/Tests/BowlingAppUnitTest/UnitTestJoueur.cs b/Sources/Tests/BowlingAppUnitTest/UnitTestJoueur.cs index b0cd10c..5171dba 100644 --- a/Sources/Tests/BowlingAppUnitTest/UnitTestJoueur.cs +++ b/Sources/Tests/BowlingAppUnitTest/UnitTestJoueur.cs @@ -54,24 +54,22 @@ namespace Test.BowlingAppUnitTest Assert.NotEqual(expectedPseudo, j.Pseudo); } - // else - // { + // else + // { - // if (!isEqual) - // { - // Assert.NotEqual(expectedPseudo, j.Pseudo); - - // } - if(isEqual) - { - Assert.Equal(expectedPseudo, j.Pseudo); - - } + // if (!isEqual) + // { + // Assert.NotEqual(expectedPseudo, j.Pseudo); + // } + if (isEqual) + { + Assert.Equal(expectedPseudo, j.Pseudo); } + } //Test joueur avec stub @@ -93,8 +91,8 @@ namespace Test.BowlingAppUnitTest //Compter le nombre de joueur dans un objet IEnumerable Assert.Equal(0, stub.GetAll().Count()); } - - + + [Fact] public void TestUpdate() { @@ -106,8 +104,9 @@ namespace Test.BowlingAppUnitTest Assert.Equal("Augustin", stub.GetAll().First().Pseudo); } - - + + } } +