La taille du code en cours de saisi s'adapte automatiquement
continuous-integration/drone/push Build is failing Details

master
Céleste BARBOSA 11 months ago
parent b355e26c61
commit dc21e90c76

@ -1,31 +1,26 @@
using CoreLibrary.Exceptions;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace CoreLibrary.Core
{
/// <summary>
/// Classe représentant un code composé de jetons et ses différentes méthodes.
/// </summary>
public class Code : INotifyPropertyChanged
public class Code
{
public event PropertyChangedEventHandler? PropertyChanged;
private readonly ObservableCollection<Jeton?> lesJetons = new ObservableCollection<Jeton?>();
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private readonly Jeton?[] lesJetons;
/// <summary>
/// Le nombre maximum de jetons dans le code.
/// </summary>
public int NbJetonsMax { get; private set; }
/// <summary>
/// Le nombre de jetons dans le code.
/// </summary>
public int NbJetons { get; private set; } = 0;
public int NbJetons { get => lesJetons.Count; }
public IEnumerable<Jeton?> Jetons => lesJetons;
public ObservableCollection<Jeton?> Jetons => lesJetons;
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="Code"/> avec la longueur de code spécifiée.
@ -34,12 +29,12 @@ namespace CoreLibrary.Core
/// <exception cref="TailleCodeException">Levée lorsque la longueur du code spécifiée est inférieure ou égale à zéro.</exception>
public Code(int tailleCode)
{
NbJetonsMax = tailleCode;
if (tailleCode <= 0)
{
throw new TailleCodeException(tailleCode);
}
lesJetons = new Jeton?[tailleCode];
}
/// <summary>
@ -54,7 +49,10 @@ namespace CoreLibrary.Core
throw new TailleCodeException(jetons.Count());
}
lesJetons = new Jeton?[jetons.Count()];
NbJetonsMax = jetons.Count();
foreach (Jeton jeton in jetons)
{
AjouterJeton(jeton);
@ -68,13 +66,12 @@ namespace CoreLibrary.Core
/// <exception cref="CodeCompletException">Levée lorsque le code est plein.</exception>
public void AjouterJeton(Jeton jeton)
{
if (NbJetons == TailleMaximale())
if (lesJetons.Count == NbJetonsMax)
{
throw new CodeCompletException();
}
lesJetons[NbJetons++] = jeton;
OnPropertyChanged(nameof(Jetons));
lesJetons.Add(jeton);
}
/// <summary>
@ -83,13 +80,12 @@ namespace CoreLibrary.Core
/// <exception cref="CodeVideException">Levée lorsque le code est vide.</exception>
public void SupprimerDernierJeton()
{
if (NbJetons == 0)
if (lesJetons.Count == 0)
{
throw new CodeVideException();
}
lesJetons[--NbJetons] = null;
OnPropertyChanged(nameof(Jetons));
lesJetons.RemoveAt(lesJetons.Count - 1);
}
/// <summary>
@ -100,13 +96,13 @@ namespace CoreLibrary.Core
/// <exception cref="IndiceCodeException">Levée lorsque l'indice est supérieur à la taille maximale du code, inférieur à 0 ou qu'il n'y a pas de jeton à l'indice spécifié</exception>
public Jeton RecupererJeton(int indice)
{
if (indice < 0 || indice >= TailleMaximale())
throw new IndiceCodeException(indice, NbJetons - 1);
if (indice < 0 || indice >= NbJetonsMax)
throw new IndiceCodeException(indice, lesJetons.Count - 1);
Jeton? jeton = lesJetons[indice];
if (!jeton.HasValue)
throw new IndiceCodeException(indice, NbJetons - 1);
throw new IndiceCodeException(indice, lesJetons.Count - 1);
return jeton.Value;
}
@ -117,16 +113,7 @@ namespace CoreLibrary.Core
/// <returns>True si le code est complet, sinon False.</returns>
public bool EstComplet()
{
return NbJetons == lesJetons.Length;
}
/// <summary>
/// Recupère la taille maximale du code.
/// </summary>
/// <returns>Taille maximale du code.</returns>
public int TailleMaximale()
{
return lesJetons.Length;
return lesJetons.Count == NbJetonsMax;
}
/// <summary>
@ -164,8 +151,6 @@ namespace CoreLibrary.Core
{
Jeton? sonJeton = sesJetons[i];
if (sonJeton.HasValue && mesJetons.Contains(sonJeton.Value))
{
indicateurs = indicateurs.Append(Indicateur.BONNECOULEUR).ToArray();

@ -82,9 +82,9 @@ namespace CoreLibrary.Core
/// <exception cref="CodeIncompletException">Levée lorsque le code fourni est incomplet.</exception>
public void AjouterCode(Code code)
{
if (code.TailleMaximale() != tailleCode)
if (code.NbJetonsMax != tailleCode)
{
throw new CodeInvalideException(code.TailleMaximale(), tailleCode);
throw new CodeInvalideException(code.NbJetonsMax, tailleCode);
}
if (!code.EstComplet())
@ -111,9 +111,9 @@ namespace CoreLibrary.Core
/// <exception cref="CodeIncompletException">Levée lorsque le code fourni est incomplet.</exception>
public bool EstBonCode(Code code)
{
if (code.TailleMaximale() != tailleCode)
if (code.NbJetonsMax != tailleCode)
{
throw new CodeInvalideException(code.TailleMaximale(), tailleCode);
throw new CodeInvalideException(code.NbJetonsMax, tailleCode);
}
if (!code.EstComplet())

@ -1,8 +1,4 @@
using CoreLibrary.Core;
using CoreLibrary.Exceptions;
using CoreLibrary.Joueurs;
namespace CoreLibrary.Regles
namespace CoreLibrary.Regles
{
/// <summary>
/// Classe définissant les règles classiques du jeu.

@ -96,10 +96,28 @@
</Label.GestureRecognizers>
</Label>
<Label FontSize="Medium" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Column="1" TextColor="{Binding Code.Jetons[0].Couleur, Converter={StaticResource CouleurVersCouleurMAUI}}" Text="{Binding Code.Jetons[0], Converter={StaticResource JetonVersTexte}}"/>
<Label FontSize="Medium" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Column="2" TextColor="{Binding Code.Jetons[1].Couleur, Converter={StaticResource CouleurVersCouleurMAUI}}" Text="{Binding Code.Jetons[1], Converter={StaticResource JetonVersTexte}}"/>
<Label FontSize="Medium" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Column="3" TextColor="{Binding Code.Jetons[2].Couleur, Converter={StaticResource CouleurVersCouleurMAUI}}" Text="{Binding Code.Jetons[2], Converter={StaticResource JetonVersTexte}}"/>
<Label FontSize="Medium" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Column="4" TextColor="{Binding Code.Jetons[3].Couleur, Converter={StaticResource CouleurVersCouleurMAUI}}" Text="{Binding Code.Jetons[3], Converter={StaticResource JetonVersTexte}}"/>
<FlexLayout
VerticalOptions="Center"
Grid.Column="1"
Grid.ColumnSpan="4"
Direction="Row"
Wrap="Wrap"
JustifyContent="SpaceAround"
AlignItems="Center"
BindableLayout.ItemsSource="{Binding Code.Jetons}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label
FontSize="Medium"
HorizontalOptions="Center"
VerticalOptions="Center"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
TextColor="{Binding Couleur, Converter={StaticResource CouleurVersCouleurMAUI}}"
Text="{Binding ., Converter={StaticResource JetonVersTexte}}"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
<Label FontSize="Medium" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Column="5" Text="V">
<Label.GestureRecognizers>

Loading…
Cancel
Save