diff --git a/Sources/CoreLibrary/Code.cs b/Sources/CoreLibrary/Code.cs index 27bf7d4..4651bcb 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,42 +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) { + 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-1] = null; - --NbJetons; + if(NbJetons == 0) + { + throw new CodeVideException(); + } + + lesJetons[--NbJetons] = null; } public Jeton RecupererJeton(int indice) { + if(indice < 0 || indice > TailleMaximale()) + 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; } @@ -64,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/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/CodeIndiceHorsDePorteeException.cs b/Sources/CoreLibrary/Exceptions/CodeIndiceHorsDePorteeException.cs deleted file mode 100644 index 6615035..0000000 --- a/Sources/CoreLibrary/Exceptions/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.Excpetion -{ - public class CodeIndiceHorsDePorteeException : Exception - { - public CodeIndiceHorsDePorteeException() : base("L'indice pointe en dehors du tableau") - { } - } -} 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/CodeJetonNullException.cs b/Sources/CoreLibrary/Exceptions/CodeJetonNullException.cs deleted file mode 100644 index 41297ab..0000000 --- a/Sources/CoreLibrary/Exceptions/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 -{ - public class CodeJetonNullException : Exception - { - public CodeJetonNullException() : base("le jeton est null") - { } - } -} diff --git a/Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsCompletException.cs b/Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsCompletException.cs deleted file mode 100644 index dfc3d44..0000000 --- a/Sources/CoreLibrary/Exceptions/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/Exceptions/CodeTableauLesJetonsIncompletException.cs b/Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsIncompletException.cs deleted file mode 100644 index 2205f9c..0000000 --- a/Sources/CoreLibrary/Exceptions/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/Exceptions/CodeTableauLesJetonsVideException.cs b/Sources/CoreLibrary/Exceptions/CodeTableauLesJetonsVideException.cs deleted file mode 100644 index d9300f8..0000000 --- a/Sources/CoreLibrary/Exceptions/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/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/PlateauCodeIncompletException.cs b/Sources/CoreLibrary/Exceptions/PlateauCodeIncompletException.cs deleted file mode 100644 index d522cb4..0000000 --- a/Sources/CoreLibrary/Exceptions/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/Exceptions/PlateauTailleCodeException.cs b/Sources/CoreLibrary/Exceptions/PlateauTailleCodeException.cs deleted file mode 100644 index a73ed73..0000000 --- a/Sources/CoreLibrary/Exceptions/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/Exceptions/PlateauTailleCodeIncompleteException.cs b/Sources/CoreLibrary/Exceptions/PlateauTailleCodeIncompleteException.cs deleted file mode 100644 index 38133c7..0000000 --- a/Sources/CoreLibrary/Exceptions/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/Exceptions/PlateauTailleGrilleException.cs b/Sources/CoreLibrary/Exceptions/PlateauTailleGrilleException.cs deleted file mode 100644 index 1305e65..0000000 --- a/Sources/CoreLibrary/Exceptions/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/Exceptions/ReglesClassiquesJoueurCourantNull.cs b/Sources/CoreLibrary/Exceptions/ReglesClassiquesJoueurCourantNull.cs deleted file mode 100644 index 89e6df5..0000000 --- a/Sources/CoreLibrary/Exceptions/ReglesClassiquesJoueurCourantNull.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace CoreLibrary -{ - public class ReglesClassiquesJoueurCourantNull : Exception - { - public ReglesClassiquesJoueurCourantNull() : base("Le joueur courant est null") - { } - } -} 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/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/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/ReglesClassiques.cs b/Sources/CoreLibrary/ReglesClassiques.cs index a7ac01e..53c3ef8 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 { @@ -20,15 +22,17 @@ } - 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() { if (!joueurCourant.HasValue) - throw new ReglesClassiquesJoueurCourantNull(); + throw new PartieNonCommenceeException(); return joueurs[joueurCourant.Value]; } @@ -37,7 +41,7 @@ { if (!joueurCourant.HasValue) { - throw new ReglesClassiquesJoueurCourantNull(); + throw new PartieNonCommenceeException(); } joueurCourant++;