Ajout de toutes les nouvelles exceptions
continuous-integration/drone/push Build is failing Details

master
Céleste BARBOSA 12 months ago
commit ff4edfe0e4

@ -1,118 +1,136 @@
namespace CoreLibrary
{
public class Code
{
private readonly Jeton?[] lesJetons;
public int NbJetons { get; private set; } = 0;
public Code(int tailleCode)
{
lesJetons = new Jeton?[tailleCode];
}
public Code(IEnumerable<Jeton> jetons)
{
lesJetons = new Jeton?[jetons.Count()];
foreach(Jeton jeton in jetons)
AjouterJeton(jeton);
}
public void AjouterJeton(Jeton jeton)
{
if (EstComplet())
throw new CodeTableauLesJetonsCompletException();
lesJetons[NbJetons++] = jeton;
}
public void SupprimerDernierJeton()
{
if(NbJetons <= 0)
throw new CodeTableauLesJetonsVideException();
lesJetons[NbJetons-1] = null;
--NbJetons;
}
public Jeton RecupererJeton(int indice)
{
if(indice < 0 || indice > TailleMaximale())
throw new CodeIndiceHorsDePorteeException();
Jeton? jeton = lesJetons[indice];
if (!jeton.HasValue)
throw new CodeJetonNullException();
return jeton.Value;
}
public IEnumerable<Jeton?> Jetons()
{
return lesJetons;
}
public bool EstComplet()
{
return NbJetons == lesJetons.Length;
}
public int TailleMaximale()
{
return lesJetons.Length;
}
public IEnumerable<Indicateur> Comparer(Code autreCode)
{
// Mon code est le code correct, l'autre code est celui qui teste
if (!autreCode.EstComplet())
throw new CodeTableauLesJetonsIncompletException();
Indicateur[] indicateurs = [];
Jeton?[] mesJetons = Jetons().ToArray();
Jeton?[] sesJetons = autreCode.Jetons().ToArray();
for (int i = 0; i < mesJetons.Length; ++i)
{
Jeton? monJeton = mesJetons[i];
Jeton? sonJeton = sesJetons[i];
if (monJeton.HasValue && sonJeton.HasValue && monJeton.Value.Couleur.Equals(sonJeton.Value.Couleur))
{
indicateurs = indicateurs.Append(Indicateur.BONNEPLACE).ToArray();
mesJetons[i] = null;
sesJetons[i] = null;
}
}
for (int i = 0; i < sesJetons.Length; ++i)
{
Jeton? sonJeton = sesJetons[i];
if (sonJeton.HasValue)
{
for (int j = 0; j < mesJetons.Length; ++j)
{
Jeton? monJeton = mesJetons[j];
if (monJeton.HasValue && sonJeton.Value.Couleur.Equals(monJeton.Value.Couleur))
{
indicateurs = indicateurs.Append(Indicateur.BONNECOULEUR).ToArray();
mesJetons[j] = null;
sesJetons[i] = null;
break;
}
}
}
}
return indicateurs;
}
}
}
using CoreLibrary.Exceptions;
namespace CoreLibrary
{
public class Code
{
private readonly Jeton?[] lesJetons;
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<Jeton> 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 (NbJetons == TailleMaximale())
{
throw new CodeCompletException();
}
lesJetons[NbJetons++] = jeton;
}
public void SupprimerDernierJeton()
{
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 IndiceCodeException(indice, NbJetons-1);
return jeton.Value;
}
public IEnumerable<Jeton?> Jetons()
{
return lesJetons;
}
public bool EstComplet()
{
return NbJetons == lesJetons.Length;
}
public int TailleMaximale()
{
return lesJetons.Length;
}
public IEnumerable<Indicateur> Comparer(Code autreCode)
{
// Mon code est le code correct, l'autre code est celui qui teste
Indicateur[] indicateurs = [];
if (EstComplet() || !autreCode.EstComplet())
return indicateurs;
Jeton?[] mesJetons = Jetons().ToArray();
Jeton?[] sesJetons = autreCode.Jetons().ToArray();
for (int i = 0; i < mesJetons.Length; ++i)
{
Jeton? monJeton = mesJetons[i];
Jeton? sonJeton = sesJetons[i];
if (monJeton.HasValue && sonJeton.HasValue && monJeton.Value.Couleur.Equals(sonJeton.Value.Couleur))
{
indicateurs = indicateurs.Append(Indicateur.BONNEPLACE).ToArray();
mesJetons[i] = null;
sesJetons[i] = null;
}
}
for (int i = 0; i < sesJetons.Length; ++i)
{
Jeton? sonJeton = sesJetons[i];
if (sonJeton.HasValue)
{
for (int j = 0; j < mesJetons.Length; ++j)
{
Jeton? monJeton = mesJetons[j];
if (monJeton.HasValue && sonJeton.Value.Couleur.Equals(monJeton.Value.Couleur))
{
indicateurs = indicateurs.Append(Indicateur.BONNECOULEUR).ToArray();
mesJetons[j] = null;
sesJetons[i] = null;
break;
}
}
}
}
return indicateurs;
}
}
}

@ -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")
{ }
}
}

@ -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")
{ }
}
}

@ -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")
{ }
}
}

@ -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")
{ }
}
}

@ -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")
{ }
}
}

@ -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.")
{ }
}
}

@ -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.")
{ }
}
}

@ -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}.")
{ }
}
}

@ -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.")
{ }
}
}

@ -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.")
{ }
}
}

@ -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}.")
{ }
}
}

@ -0,0 +1,9 @@
namespace CoreLibrary.Exceptions
{
public class PartieNonCommenceeException : Exception
{
public PartieNonCommenceeException() :
base("La partie n'a pas encore commencée.")
{ }
}
}

@ -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}.")
{ }
}
}

@ -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}.")
{ }
}
}

@ -10,7 +10,7 @@
int NbJoueurs { get; }
int NbJoueursMaximum { get; }
void AjouterJoueur(string nom);
Joueur AjouterJoueur(string nom);
Joueur JoueurCourant();
void PasserLaMain();

@ -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<Indicateur> indicateurs = codeSecret.Comparer(code);
if (indicateurs.Count() != tailleCode)

@ -1,8 +0,0 @@
namespace CoreLibrary
{
public class PlateauCodeIncompletException : Exception
{
public PlateauCodeIncompletException() : base("Le code est incomplet")
{ }
}
}

@ -1,7 +0,0 @@
namespace CoreLibrary
{
public class PlateauTailleCodeException : Exception
{
public PlateauTailleCodeException() : base("La taille du code doit être positive non nulle.") { }
}
}

@ -1,7 +0,0 @@
namespace CoreLibrary
{
public class PlateauTailleCodeIncompleteException : Exception
{
public PlateauTailleCodeIncompleteException() : base("Le code n'est pas remplit au maximum") { }
}
}

@ -1,8 +0,0 @@
namespace CoreLibrary
{
public class PlateauTailleGrilleException : Exception
{
public PlateauTailleGrilleException() : base("La taille de la grille doit être égale positive non nulle.")
{ }
}
}

@ -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++;

@ -1,8 +0,0 @@
namespace CoreLibrary
{
public class ReglesClassiquesJoueurCourantNull : Exception
{
public ReglesClassiquesJoueurCourantNull() : base("Le joueur courant est null")
{ }
}
}
Loading…
Cancel
Save