From 7a430538b9706d0d4495de66524b5a3f5a049eb7 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Mon, 6 May 2024 18:49:42 +0200 Subject: [PATCH 1/2] nouvelles exceptions --- Sources/CoreLibrary/Code.cs | 55 +++++++++++++------ .../CodeIndiceHorsDePorteeException.cs | 14 ----- Sources/CoreLibrary/CodeJetonNullException.cs | 14 ----- .../CodeTableauLesJetonsCompletException.cs | 15 ----- .../CodeTableauLesJetonsIncompletException.cs | 14 ----- .../CodeTableauLesJetonsVideException.cs | 14 ----- .../Exceptions/CodeCompletException.cs | 9 +++ .../Exceptions/CodeIncompletException.cs | 9 +++ .../Exceptions/CodeInvalideException.cs | 9 +++ .../Exceptions/CodeVideException.cs | 9 +++ .../Exceptions/GrilleCompleteException.cs | 9 +++ .../Exceptions/IndiceCodeException.cs | 9 +++ .../Exceptions/PartieNonCommenceeException.cs | 9 +++ .../Exceptions/TailleCodeException.cs | 9 +++ .../Exceptions/TailleGrilleException.cs | 9 +++ Sources/CoreLibrary/Plateau.cs | 22 ++++++-- .../PlateauCodeIncompletException.cs | 8 --- .../CoreLibrary/PlateauTailleCodeException.cs | 7 --- .../PlateauTailleCodeIncompleteException.cs | 7 --- .../PlateauTailleGrilleException.cs | 8 --- Sources/CoreLibrary/ReglesClassiques.cs | 8 ++- .../ReglesClassiquesJoueurCourantNull.cs | 8 --- 22 files changed, 141 insertions(+), 134 deletions(-) delete mode 100644 Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs delete mode 100644 Sources/CoreLibrary/CodeJetonNullException.cs delete mode 100644 Sources/CoreLibrary/CodeTableauLesJetonsCompletException.cs delete mode 100644 Sources/CoreLibrary/CodeTableauLesJetonsIncompletException.cs delete mode 100644 Sources/CoreLibrary/CodeTableauLesJetonsVideException.cs create mode 100644 Sources/CoreLibrary/Exceptions/CodeCompletException.cs create mode 100644 Sources/CoreLibrary/Exceptions/CodeIncompletException.cs create mode 100644 Sources/CoreLibrary/Exceptions/CodeInvalideException.cs create mode 100644 Sources/CoreLibrary/Exceptions/CodeVideException.cs create mode 100644 Sources/CoreLibrary/Exceptions/GrilleCompleteException.cs create mode 100644 Sources/CoreLibrary/Exceptions/IndiceCodeException.cs create mode 100644 Sources/CoreLibrary/Exceptions/PartieNonCommenceeException.cs create mode 100644 Sources/CoreLibrary/Exceptions/TailleCodeException.cs create mode 100644 Sources/CoreLibrary/Exceptions/TailleGrilleException.cs delete mode 100644 Sources/CoreLibrary/PlateauCodeIncompletException.cs delete mode 100644 Sources/CoreLibrary/PlateauTailleCodeException.cs delete mode 100644 Sources/CoreLibrary/PlateauTailleCodeIncompleteException.cs delete mode 100644 Sources/CoreLibrary/PlateauTailleGrilleException.cs delete mode 100644 Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs diff --git a/Sources/CoreLibrary/Code.cs b/Sources/CoreLibrary/Code.cs index 79e10b3..6081402 100644 --- a/Sources/CoreLibrary/Code.cs +++ b/Sources/CoreLibrary/Code.cs @@ -1,4 +1,6 @@ -namespace CoreLibrary +using CoreLibrary.Exceptions; + +namespace CoreLibrary { public class Code { @@ -7,41 +9,59 @@ public int NbJetons { get; private set; } = 0; public Code(int tailleCode) - { + { + if(tailleCode <= 0) + { + throw new TailleCodeException(tailleCode); + } + lesJetons = new Jeton?[tailleCode]; } - public Code(IEnumerable jetons) + public Code(IEnumerable jetons) { - lesJetons = jetons.ToArray(); + if (jetons.Count() == 0) + { + throw new TailleCodeException(jetons.Count()); + } + + lesJetons = new Jeton?[jetons.Count()]; + foreach(Jeton jeton in jetons) + { + AjouterJeton(jeton); + } } public void AjouterJeton(Jeton jeton) - { - if (EstComplet()) - throw new CodeTableauLesJetonsCompletException(); - + { + if (NbJetons == TailleMaximale()) + { + throw new CodeCompletException(); + } + lesJetons[NbJetons++] = jeton; } public void SupprimerDernierJeton() { - if(NbJetons <= 0) - throw new CodeTableauLesJetonsVideException(); - - lesJetons[NbJetons--] = null; + if(NbJetons == 0) + { + throw new CodeVideException(); + } + + lesJetons[--NbJetons] = null; } public Jeton RecupererJeton(int indice) { if(indice < 0 || indice > TailleMaximale()) - throw new CodeIndiceHorsDePorteeException(); + throw new IndiceCodeException(indice, NbJetons-1); Jeton? jeton = lesJetons[indice]; if (!jeton.HasValue) - throw new CodeJetonNullException(); - + throw new IndiceCodeException(indice, NbJetons-1); + return jeton.Value; } @@ -63,11 +83,12 @@ public IEnumerable Comparer(Code autreCode) { // Mon code est le code correct, l'autre code est celui qui teste - if (!autreCode.EstComplet()) - throw new CodeTableauLesJetonsIncompletException(); Indicateur[] indicateurs = []; + if (EstComplet() || !autreCode.EstComplet()) + return indicateurs; + Jeton?[] mesJetons = Jetons().ToArray(); Jeton?[] sesJetons = autreCode.Jetons().ToArray(); diff --git a/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs b/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs deleted file mode 100644 index 36c0929..0000000 --- a/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CoreLibrary -{ - public class CodeIndiceHorsDePorteeException : Exception - { - public CodeIndiceHorsDePorteeException() : base("L'indice pointe en dehors du tableau") - { } - } -} diff --git a/Sources/CoreLibrary/CodeJetonNullException.cs b/Sources/CoreLibrary/CodeJetonNullException.cs deleted file mode 100644 index 9fcc9f3..0000000 --- a/Sources/CoreLibrary/CodeJetonNullException.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CoreLibrary -{ - internal class CodeJetonNullException : Exception - { - public CodeJetonNullException() : base("le jeton est null") - { } - } -} diff --git a/Sources/CoreLibrary/CodeTableauLesJetonsCompletException.cs b/Sources/CoreLibrary/CodeTableauLesJetonsCompletException.cs deleted file mode 100644 index dfc3d44..0000000 --- a/Sources/CoreLibrary/CodeTableauLesJetonsCompletException.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CoreLibrary -{ - public class CodeTableauLesJetonsCompletException : Exception - { - public CodeTableauLesJetonsCompletException() : base("Le tableau des jetons est plein") - { } - - } -} diff --git a/Sources/CoreLibrary/CodeTableauLesJetonsIncompletException.cs b/Sources/CoreLibrary/CodeTableauLesJetonsIncompletException.cs deleted file mode 100644 index 2205f9c..0000000 --- a/Sources/CoreLibrary/CodeTableauLesJetonsIncompletException.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CoreLibrary -{ - public class CodeTableauLesJetonsIncompletException : Exception - { - public CodeTableauLesJetonsIncompletException() : base("Le tableau des jetons est incomplet") - { } - } -} diff --git a/Sources/CoreLibrary/CodeTableauLesJetonsVideException.cs b/Sources/CoreLibrary/CodeTableauLesJetonsVideException.cs deleted file mode 100644 index d9300f8..0000000 --- a/Sources/CoreLibrary/CodeTableauLesJetonsVideException.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CoreLibrary -{ - public class CodeTableauLesJetonsVideException : Exception - { - public CodeTableauLesJetonsVideException() : base("Le tableau des jetons est vide") - { } - } -} diff --git a/Sources/CoreLibrary/Exceptions/CodeCompletException.cs b/Sources/CoreLibrary/Exceptions/CodeCompletException.cs new file mode 100644 index 0000000..3a560c0 --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/CodeCompletException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class CodeCompletException : Exception + { + public CodeCompletException() : + base("Le code dans lequel vous essayez d'ajouter un jeton est déjà complet.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/CodeIncompletException.cs b/Sources/CoreLibrary/Exceptions/CodeIncompletException.cs new file mode 100644 index 0000000..5e29af8 --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/CodeIncompletException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class CodeIncompletException : Exception + { + public CodeIncompletException() : + base("Le code que vous essayez d'ajouter dans la grille n'est pas complet.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/CodeInvalideException.cs b/Sources/CoreLibrary/Exceptions/CodeInvalideException.cs new file mode 100644 index 0000000..fe616e9 --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/CodeInvalideException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class CodeInvalideException : Exception + { + public CodeInvalideException(int tailleCodeAjoute, int tailleCodePlateau) : + base($"Le code que vous essayez d'ajouter est un code de taille {tailleCodeAjoute}, or le plateau attend un code de {tailleCodePlateau}.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/CodeVideException.cs b/Sources/CoreLibrary/Exceptions/CodeVideException.cs new file mode 100644 index 0000000..bc6c9d4 --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/CodeVideException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class CodeVideException : Exception + { + public CodeVideException() : + base("Le code dans lequel vous essayez de supprimer un jeton est déjà vide.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/GrilleCompleteException.cs b/Sources/CoreLibrary/Exceptions/GrilleCompleteException.cs new file mode 100644 index 0000000..b7f8434 --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/GrilleCompleteException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class GrilleCompleteException : Exception + { + public GrilleCompleteException() : + base("La grille dans laquelle vous essayez d'ajouter un code est déjà complète.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/IndiceCodeException.cs b/Sources/CoreLibrary/Exceptions/IndiceCodeException.cs new file mode 100644 index 0000000..8b3c0e5 --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/IndiceCodeException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class IndiceCodeException : Exception + { + public IndiceCodeException(int indice, int indiceMax) : + base($"Vous avez essayé de récupérer le jeton à la place {indice}, mais son indice doit être compris entre 0 et {indiceMax}.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/PartieNonCommenceeException.cs b/Sources/CoreLibrary/Exceptions/PartieNonCommenceeException.cs new file mode 100644 index 0000000..052bacc --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/PartieNonCommenceeException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class PartieNonCommenceeException : Exception + { + public PartieNonCommenceeException() : + base("La partie n'a pas encore commencée.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/TailleCodeException.cs b/Sources/CoreLibrary/Exceptions/TailleCodeException.cs new file mode 100644 index 0000000..ebf67c9 --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/TailleCodeException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class TailleCodeException : Exception + { + public TailleCodeException(int taille) : + base($"Un code doit avoir une taille positive non nulle, or il a reçu {taille}.") + { } + } +} diff --git a/Sources/CoreLibrary/Exceptions/TailleGrilleException.cs b/Sources/CoreLibrary/Exceptions/TailleGrilleException.cs new file mode 100644 index 0000000..494e1ee --- /dev/null +++ b/Sources/CoreLibrary/Exceptions/TailleGrilleException.cs @@ -0,0 +1,9 @@ +namespace CoreLibrary.Exceptions +{ + public class TailleGrilleException : Exception + { + public TailleGrilleException(int taille) : + base($"Une grille doit avoir une taille positive non nulle, or elle a reçu {taille}.") + { } + } +} diff --git a/Sources/CoreLibrary/Plateau.cs b/Sources/CoreLibrary/Plateau.cs index d8b0c16..dd3bbff 100644 --- a/Sources/CoreLibrary/Plateau.cs +++ b/Sources/CoreLibrary/Plateau.cs @@ -1,4 +1,6 @@ -namespace CoreLibrary +using CoreLibrary.Exceptions; + +namespace CoreLibrary { public class Plateau { @@ -17,12 +19,12 @@ { if(tailleCode <= 0) { - throw new PlateauTailleCodeException(); + throw new TailleCodeException(tailleCode); } if (tailleGrille <= 0) { - throw new PlateauTailleGrilleException(); + throw new TailleGrilleException(tailleGrille); } codeSecret = new Code(tailleCode); @@ -53,12 +55,12 @@ { if (code.TailleMaximale() != tailleCode) { - throw new PlateauTailleCodeException(); + throw new CodeInvalideException(code.TailleMaximale(), tailleCode); } if (!code.EstComplet()) { - throw new PlateauCodeIncompletException(); + throw new CodeIncompletException(); } indicateurs[Tour - 1] = codeSecret.Comparer(code); @@ -73,6 +75,16 @@ public bool EstBonCode(Code code) { + if (code.TailleMaximale() != tailleCode) + { + throw new CodeInvalideException(code.TailleMaximale(), tailleCode); + } + + if (!code.EstComplet()) + { + throw new CodeIncompletException(); + } + IEnumerable indicateurs = codeSecret.Comparer(code); if (indicateurs.Count() != tailleCode) diff --git a/Sources/CoreLibrary/PlateauCodeIncompletException.cs b/Sources/CoreLibrary/PlateauCodeIncompletException.cs deleted file mode 100644 index d522cb4..0000000 --- a/Sources/CoreLibrary/PlateauCodeIncompletException.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace CoreLibrary -{ - public class PlateauCodeIncompletException : Exception - { - public PlateauCodeIncompletException() : base("Le code est incomplet") - { } - } -} diff --git a/Sources/CoreLibrary/PlateauTailleCodeException.cs b/Sources/CoreLibrary/PlateauTailleCodeException.cs deleted file mode 100644 index a73ed73..0000000 --- a/Sources/CoreLibrary/PlateauTailleCodeException.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace CoreLibrary -{ - public class PlateauTailleCodeException : Exception - { - public PlateauTailleCodeException() : base("La taille du code doit être positive non nulle.") { } - } -} diff --git a/Sources/CoreLibrary/PlateauTailleCodeIncompleteException.cs b/Sources/CoreLibrary/PlateauTailleCodeIncompleteException.cs deleted file mode 100644 index 38133c7..0000000 --- a/Sources/CoreLibrary/PlateauTailleCodeIncompleteException.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace CoreLibrary -{ - public class PlateauTailleCodeIncompleteException : Exception - { - public PlateauTailleCodeIncompleteException() : base("Le code n'est pas remplit au maximum") { } - } -} diff --git a/Sources/CoreLibrary/PlateauTailleGrilleException.cs b/Sources/CoreLibrary/PlateauTailleGrilleException.cs deleted file mode 100644 index 1305e65..0000000 --- a/Sources/CoreLibrary/PlateauTailleGrilleException.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace CoreLibrary -{ - public class PlateauTailleGrilleException : Exception - { - public PlateauTailleGrilleException() : base("La taille de la grille doit être égale positive non nulle.") - { } - } -} diff --git a/Sources/CoreLibrary/ReglesClassiques.cs b/Sources/CoreLibrary/ReglesClassiques.cs index a7ac01e..3776b16 100644 --- a/Sources/CoreLibrary/ReglesClassiques.cs +++ b/Sources/CoreLibrary/ReglesClassiques.cs @@ -1,4 +1,6 @@ -namespace CoreLibrary +using CoreLibrary.Exceptions; + +namespace CoreLibrary { public class ReglesClassiques : IRegles { @@ -28,7 +30,7 @@ public Joueur JoueurCourant() { if (!joueurCourant.HasValue) - throw new ReglesClassiquesJoueurCourantNull(); + throw new PartieNonCommenceeException(); return joueurs[joueurCourant.Value]; } @@ -37,7 +39,7 @@ { if (!joueurCourant.HasValue) { - throw new ReglesClassiquesJoueurCourantNull(); + throw new PartieNonCommenceeException(); } joueurCourant++; diff --git a/Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs b/Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs deleted file mode 100644 index 89e6df5..0000000 --- a/Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace CoreLibrary -{ - public class ReglesClassiquesJoueurCourantNull : Exception - { - public ReglesClassiquesJoueurCourantNull() : base("Le joueur courant est null") - { } - } -} From 568da5718ba3cc50a72275538dd8daade29081e6 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Mon, 6 May 2024 18:50:30 +0200 Subject: [PATCH 2/2] Correction de AJouterJoueur interface --- Sources/CoreLibrary/IRegles.cs | 2 +- Sources/CoreLibrary/ReglesClassiques.cs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/CoreLibrary/IRegles.cs b/Sources/CoreLibrary/IRegles.cs index dfe558b..7c5c389 100644 --- a/Sources/CoreLibrary/IRegles.cs +++ b/Sources/CoreLibrary/IRegles.cs @@ -10,7 +10,7 @@ int NbJoueurs { get; } int NbJoueursMaximum { get; } - void AjouterJoueur(string nom); + Joueur AjouterJoueur(string nom); Joueur JoueurCourant(); void PasserLaMain(); diff --git a/Sources/CoreLibrary/ReglesClassiques.cs b/Sources/CoreLibrary/ReglesClassiques.cs index 3776b16..53c3ef8 100644 --- a/Sources/CoreLibrary/ReglesClassiques.cs +++ b/Sources/CoreLibrary/ReglesClassiques.cs @@ -22,9 +22,11 @@ namespace CoreLibrary } - public void AjouterJoueur(string nom) + public Joueur AjouterJoueur(string nom) { - joueurs[nbJoueurs++] = new Joueur(nom, new Plateau(TailleCodeMaximum, TourMaximum)); + Joueur joueur = new Joueur(nom, new Plateau(TailleCodeMaximum, TourMaximum)) + joueurs[nbJoueurs++] = joueur; + return joueur; } public Joueur JoueurCourant()