diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index fc9e32a..475a617 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -10,8 +10,36 @@ ReglesClassiques partie = new ReglesClassiques(); Utils.AfficherTitre(); Utils.AfficherTitre("Joueurs"); -string joueur1 = Utils.SaisirNom(); -string joueur2 = Utils.SaisirNom(); + +string joueur1; +string joueur2; + +while (true) +{ + try + { + joueur1 = Utils.SaisirNom(); + break; + } + catch(UtilsNomJoueurNullException) + { + Console.WriteLine("Nom invalide pour le joueur ! Ressaisir le nom"); + } +} + +while (true) +{ + try + { + joueur2 = Utils.SaisirNom(); + break; + } + catch (UtilsNomJoueurNullException) + { + Console.WriteLine("Nom invalide pour le joueur 2 ! Ressaisir le nom"); + } +} + Utils.AfficherSeparateur(); partie.AjouterJoueur(joueur1); diff --git a/Sources/ConsoleApp/Utils.cs b/Sources/ConsoleApp/Utils.cs index b19d651..76b1885 100644 --- a/Sources/ConsoleApp/Utils.cs +++ b/Sources/ConsoleApp/Utils.cs @@ -62,8 +62,13 @@ namespace ConsoleApp Console.WriteLine(nom); Console.Write(">>> "); nom = Console.ReadLine() ?? nom; + if (nom == "") + { + --nombreJoueurs; + throw new UtilsNomJoueurNullException(); + } Console.WriteLine(); - + return nom; } @@ -218,7 +223,15 @@ namespace ConsoleApp else { Console.Write("\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"); - code.SupprimerDernierJeton(); + try + { + code.SupprimerDernierJeton(); + } + catch(CodeTableauLesJetonsVideException) + { + Console.WriteLine("Il n'y a pas de jetons! Impossible de supprimer"); + } + } } } diff --git a/Sources/ConsoleApp/UtilsNomJoueurNullException.cs b/Sources/ConsoleApp/UtilsNomJoueurNullException.cs new file mode 100644 index 0000000..20187e7 --- /dev/null +++ b/Sources/ConsoleApp/UtilsNomJoueurNullException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp +{ + public class UtilsNomJoueurNullException : Exception + { + public UtilsNomJoueurNullException() : base("Le nom du joueur est null") + { } + } +} diff --git a/Sources/CoreLibrary/Code.cs b/Sources/CoreLibrary/Code.cs index facfacb..d48f42d 100644 --- a/Sources/CoreLibrary/Code.cs +++ b/Sources/CoreLibrary/Code.cs @@ -7,7 +7,7 @@ public int NbJetons { get; private set; } = 0; public Code(int tailleCode) - { + { lesJetons = new Jeton?[tailleCode]; } @@ -19,22 +19,31 @@ } public void AjouterJeton(Jeton jeton) - { + { + if (EstComplet()) + throw new CodeTableauLesJetonsCompletException(); + lesJetons[NbJetons++] = jeton; } public void SupprimerDernierJeton() { + if(NbJetons <= 0) + throw new CodeTableauLesJetonsVideException(); + lesJetons[NbJetons--] = null; } public Jeton RecupererJeton(int indice) { + if(indice < 0 || indice > TailleMaximale()) + throw new CodeIndiceHorsDePorteeException(); + Jeton? jeton = lesJetons[indice]; if (!jeton.HasValue) - throw new Exception(); - + throw new CodeJetonNullException(); + return jeton.Value; } @@ -56,6 +65,8 @@ 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 = []; @@ -102,3 +113,5 @@ } } } + + diff --git a/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs b/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs new file mode 100644 index 0000000..36c0929 --- /dev/null +++ b/Sources/CoreLibrary/CodeIndiceHorsDePorteeException.cs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 0000000..9fcc9f3 --- /dev/null +++ b/Sources/CoreLibrary/CodeJetonNullException.cs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 0000000..dfc3d44 --- /dev/null +++ b/Sources/CoreLibrary/CodeTableauLesJetonsCompletException.cs @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..2205f9c --- /dev/null +++ b/Sources/CoreLibrary/CodeTableauLesJetonsIncompletException.cs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 0000000..d9300f8 --- /dev/null +++ b/Sources/CoreLibrary/CodeTableauLesJetonsVideException.cs @@ -0,0 +1,14 @@ +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/Plateau.cs b/Sources/CoreLibrary/Plateau.cs index 2017fa7..d8b0c16 100644 --- a/Sources/CoreLibrary/Plateau.cs +++ b/Sources/CoreLibrary/Plateau.cs @@ -15,6 +15,16 @@ public Plateau(int tailleCode, int tailleGrille) { + if(tailleCode <= 0) + { + throw new PlateauTailleCodeException(); + } + + if (tailleGrille <= 0) + { + throw new PlateauTailleGrilleException(); + } + codeSecret = new Code(tailleCode); grille = new Code?[tailleGrille]; indicateurs = new IEnumerable[tailleGrille]; @@ -22,11 +32,6 @@ this.tailleCode = tailleCode; GenererCodeAleatoire(); - - foreach (Jeton? jeton in codeSecret.Jetons()) - { - Console.WriteLine(jeton.Value.Couleur); - } } private void GenererCodeAleatoire() @@ -46,6 +51,16 @@ public void AjouterCode(Code code) { + if (code.TailleMaximale() != tailleCode) + { + throw new PlateauTailleCodeException(); + } + + if (!code.EstComplet()) + { + throw new PlateauCodeIncompletException(); + } + indicateurs[Tour - 1] = codeSecret.Comparer(code); grille[Tour - 1] = code; ++Tour; @@ -94,3 +109,4 @@ } } } + diff --git a/Sources/CoreLibrary/PlateauCodeIncompletException.cs b/Sources/CoreLibrary/PlateauCodeIncompletException.cs new file mode 100644 index 0000000..d522cb4 --- /dev/null +++ b/Sources/CoreLibrary/PlateauCodeIncompletException.cs @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000..a73ed73 --- /dev/null +++ b/Sources/CoreLibrary/PlateauTailleCodeException.cs @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000..38133c7 --- /dev/null +++ b/Sources/CoreLibrary/PlateauTailleCodeIncompleteException.cs @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000..1305e65 --- /dev/null +++ b/Sources/CoreLibrary/PlateauTailleGrilleException.cs @@ -0,0 +1,8 @@ +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 632c259..a7ac01e 100644 --- a/Sources/CoreLibrary/ReglesClassiques.cs +++ b/Sources/CoreLibrary/ReglesClassiques.cs @@ -10,7 +10,6 @@ public int TourMaximum { get => 12; } public int TailleCodeMaximum { get => 4; } - public int NbJoueurs { get => nbJoueurs; } public int NbJoueursMaximum { get => 2; } @@ -29,7 +28,7 @@ public Joueur JoueurCourant() { if (!joueurCourant.HasValue) - throw new Exception(); + throw new ReglesClassiquesJoueurCourantNull(); return joueurs[joueurCourant.Value]; } @@ -37,7 +36,9 @@ public void PasserLaMain() { if (!joueurCourant.HasValue) - throw new Exception(); + { + throw new ReglesClassiquesJoueurCourantNull(); + } joueurCourant++; if (joueurCourant >= joueurs.Length) diff --git a/Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs b/Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs new file mode 100644 index 0000000..89e6df5 --- /dev/null +++ b/Sources/CoreLibrary/ReglesClassiquesJoueurCourantNull.cs @@ -0,0 +1,8 @@ +namespace CoreLibrary +{ + public class ReglesClassiquesJoueurCourantNull : Exception + { + public ReglesClassiquesJoueurCourantNull() : base("Le joueur courant est null") + { } + } +}