Merge branch 'Partie' into PartieCamille

master
Camille TURPIN-ETIENNE 11 months ago
commit eb91d4d278

@ -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 >= lesJetons.Count)
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.

@ -13,7 +13,7 @@ namespace MauiSpark.Convertisseurs
public static Color Blanc { get; private set; } = Color.FromRgb(255, 255, 255);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not Couleur) return Noir;
@ -36,7 +36,7 @@ namespace MauiSpark.Convertisseurs
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not Color) return Couleur.NOIR;

@ -5,7 +5,7 @@ namespace MauiSpark.Convertisseurs
{
public class IndicateurVersCouleurMAUI : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not Indicateur) return "black";
@ -20,7 +20,7 @@ namespace MauiSpark.Convertisseurs
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

@ -10,14 +10,14 @@ namespace MauiSpark.Convertisseurs
{
public class JetonVersTexte : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not Jeton) return "";
return "O";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

@ -96,11 +96,29 @@
</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>
<TapGestureRecognizer Tapped="ValiderCode"/>

@ -4,6 +4,7 @@ using CoreLibrary.Events;
using CoreLibrary.Exceptions;
using CoreLibrary.Joueurs;
using MauiSpark.Convertisseurs;
using System.Globalization;
namespace MauiSpark.Pages;
@ -54,13 +55,13 @@ public partial class Plateau : ContentPage
private void CouleurPresee(Object sender, EventArgs e)
{
Label label = (Label)sender;
Couleur couleur = (Couleur)new CouleurVersCouleurMAUI().ConvertBack(label.TextColor, null, null, null);
Couleur couleur = (Couleur)new CouleurVersCouleurMAUI().ConvertBack(label.TextColor, typeof(Couleur), null, CultureInfo.InvariantCulture);
try
{
code.AjouterJeton(new Jeton(couleur));
}
catch (CodeCompletException ignored)
catch (CodeCompletException)
{
DisplayAlert("Attention", "La code est plein", "OK");
}
@ -72,7 +73,7 @@ public partial class Plateau : ContentPage
{
code.SupprimerDernierJeton();
}
catch(CodeVideException ignored)
catch(CodeVideException)
{
DisplayAlert("Attention", "La code est vide", "OK");
}
@ -84,7 +85,7 @@ public partial class Plateau : ContentPage
{
joueur.Code(code);
}
catch (CodeIncompletException ignored)
catch (CodeIncompletException)
{
DisplayAlert("Attention", "La code n'est pas complet", "OK");
}

@ -11,7 +11,7 @@ namespace UnitTesting
{
Code code = new Code(4);
Assert.NotNull(code);
Assert.Equal(4, code.Jetons().Count());
Assert.Empty(code.Jetons);
Assert.Equal(0, code.NbJetons);
}
@ -29,7 +29,7 @@ namespace UnitTesting
Code code = new Code(jetons);
Assert.NotNull(code);
Assert.Equal(3, code.Jetons().Count());
Assert.Equal(3, code.Jetons.Count());
Assert.Equal(3, code.NbJetons);
}
@ -46,7 +46,7 @@ namespace UnitTesting
Code code = new Code(3);
code.AjouterJeton(jeton);
Assert.Equal(1, code.NbJetons);
Assert.Equal(jeton, code.Jetons().ElementAt(0));
Assert.Equal(jeton, code.Jetons.ElementAt(0));
}
[Fact]
@ -100,7 +100,7 @@ namespace UnitTesting
{
Jeton[] jetonsAttendus = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)];
Code code = new Code(jetonsAttendus);
IEnumerable<Jeton?> lesJetons = code.Jetons();
IEnumerable<Jeton?> lesJetons = code.Jetons;
Assert.Equal(jetonsAttendus.Length, lesJetons.Count());
@ -133,7 +133,7 @@ namespace UnitTesting
{
Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)];
Code code = new Code(jetons);
int tailleMaximale = code.TailleMaximale();
int tailleMaximale = code.NbJetonsMax;
Assert.Equal(jetons.Length, tailleMaximale);
}

@ -141,7 +141,7 @@ namespace UnitTesting
Code? codeSecret = (Code?)fieldInfo.GetValue(plateau);
Assert.NotNull(codeSecret);
Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray();
Jeton[] jetons = codeSecret.Jetons.Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray();
int i = 0;
int j = 1;
@ -156,7 +156,7 @@ namespace UnitTesting
plateau = new Plateau(2, 10);
codeSecret = (Code?)fieldInfo.GetValue(plateau);
Assert.NotNull(codeSecret);
jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray();
jetons = codeSecret.Jetons.Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray();
i = 0;
j = 1;
@ -186,7 +186,7 @@ namespace UnitTesting
Code? codeSecret = (Code?)fieldInfo.GetValue(plateau);
Assert.NotNull(codeSecret);
Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray();
Jeton[] jetons = codeSecret.Jetons.Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray();
for (int i=0; i<jetons.Length; ++i)
{
@ -219,9 +219,9 @@ namespace UnitTesting
IEnumerable<IEnumerable<Jeton?>> grille = plateau.Grille();
Assert.Equal(4, grille.First().Count());
Assert.Equal(4, grille.Last().Count());
Assert.Equal(code1.Jetons(), grille.ElementAt(0));
Assert.Equal(code2.Jetons(), grille.ElementAt(1));
Assert.Empty(grille.Last());
Assert.Equal(code1.Jetons, grille.ElementAt(0));
Assert.Equal(code2.Jetons, grille.ElementAt(1));
}

@ -15,242 +15,5 @@ namespace UnitTesting
{
Assert.Equal("Règles classiques", new ReglesClassiques().Nom);
}
[Fact]
public void TestNbJoueur()
{
ReglesClassiques regles = new ReglesClassiques();
Assert.Equal(0, regles.NbJoueurs);
regles.AjouterJoueur("Bonjour");
Assert.Equal(1, regles.NbJoueurs);
regles.AjouterJoueur("Bonsoir");
Assert.Equal(2, regles.NbJoueurs);
}
[Fact]
public void GenererCode()
{
ReglesClassiques regles = new ReglesClassiques();
Assert.Equal(new Code(4).NbJetons, regles.GenererCode().NbJetons);
Assert.Equal(4, regles.GenererCode().TailleMaximale());
}
[Fact]
public void TestJoueurCourantPartieNonCommencee()
{
ReglesClassiques regles = new ReglesClassiques();
Assert.Throws<PartieNonCommenceeException>(() => regles.JoueurCourant());
}
[Fact]
public void TestPasserLaMainPartieNonCommencee()
{
ReglesClassiques regles = new ReglesClassiques();
Assert.Throws<PartieNonCommenceeException>(() => regles.PasserLaMain());
}
[Fact]
public void TestPasserLaMain()
{
ReglesClassiques regles = new ReglesClassiques();
Type type = typeof(ReglesClassiques);
FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo);
regles.AjouterJoueur("céleste");
regles.AjouterJoueur("pauline");
Assert.Throws<PartieNonCommenceeException>(() => regles.PasserLaMain());
int? joueurCourantPasDemarree = (int?)fieldInfo.GetValue(regles);
Assert.Null(joueurCourantPasDemarree);
regles.CommencerLaPartie();
int? joueurCourantAvant = (int?) fieldInfo.GetValue(regles);
Joueur courantAvant = regles.JoueurCourant().Item1;
Assert.NotNull(joueurCourantAvant);
Assert.Equal(0, joueurCourantAvant);
regles.PasserLaMain();
int? joueurCourantApres = (int?)fieldInfo.GetValue(regles);
Joueur courantApres = regles.JoueurCourant().Item1;
Assert.NotNull(joueurCourantApres);
Assert.Equal(1, joueurCourantApres);
Assert.NotEqual(joueurCourantAvant, joueurCourantApres);
Assert.NotEqual(courantAvant, courantApres);
regles.PasserLaMain();
int? joueurCourantBoucle = (int?)fieldInfo.GetValue(regles);
Joueur courantBoucle = regles.JoueurCourant().Item1;
Assert.NotNull(joueurCourantBoucle);
Assert.Equal(0, joueurCourantBoucle);
Assert.NotEqual(joueurCourantApres, joueurCourantBoucle);
Assert.Equal(joueurCourantAvant, joueurCourantBoucle);
Assert.NotEqual(courantApres, courantBoucle);
}
[Fact]
public void TestEstTermineeVictoire()
{
ReglesClassiques regles = new ReglesClassiques();
regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2");
Assert.False(regles.EstTerminee());
Type type = typeof(ReglesClassiques);
FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo);
fieldInfo.SetValue(regles, 1);
Assert.False(regles.EstTerminee());
fieldInfo.SetValue(regles, 0);
regles.CommencerLaPartie();
regles.PasserLaMain();
Assert.False(regles.EstTerminee());
regles.PasserLaMain();
Plateau plateauj1 = regles.JoueurCourant().Item2;
type = typeof(Plateau);
fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo);
Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1);
Assert.NotNull(codeSecret);
regles.JoueurCourant().Item2.AjouterCode(codeSecret);
bool estTerminee = regles.EstTerminee();
Assert.True(estTerminee);
}
[Fact]
public void TestGagants()
{
ReglesClassiques regles = new ReglesClassiques();
Partie partie = new Partie(regles);
regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2");
regles.CommencerLaPartie();
Plateau plateauj1 = regles.JoueurCourant().Item2;
Type type = typeof(Plateau);
FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo);
Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1);
Assert.NotNull(codeSecret);
regles.JoueurCourant().Item2.AjouterCode(codeSecret);
IEnumerable<Joueur> gagnants = regles.Gagnants();
Assert.Single(gagnants);
Assert.Contains(regles.JoueurCourant().Item1, gagnants);
}
[Fact]
public void TestPerdants()
{
ReglesClassiques regles = new ReglesClassiques();
Partie partie = new Partie(regles);
regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2");
regles.CommencerLaPartie();
Plateau plateauj1 = regles.JoueurCourant().Item2;
Type type = typeof(Plateau);
FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo);
Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1);
Assert.NotNull(codeSecret);
regles.JoueurCourant().Item2.AjouterCode(codeSecret);
regles.PasserLaMain();
IEnumerable<Joueur> perdants = regles.Perdants();
Assert.Single(perdants);
Assert.Contains(regles.JoueurCourant().Item1, perdants);
}
[Fact]
public void EstTermineeMaxTour()
{
ReglesClassiques regles = new ReglesClassiques();
regles.AjouterJoueur("1");
regles.AjouterJoueur("2");
regles.CommencerLaPartie();
for (int i = 0; i < 24; ++i)
{
Plateau plateau = regles.JoueurCourant().Item2;
Code code = new Code(4);
code.AjouterJeton(new Jeton(Couleur.ROUGE));
code.AjouterJeton(new Jeton(Couleur.ROUGE));
code.AjouterJeton(new Jeton(Couleur.ROUGE));
code.AjouterJeton(new Jeton(Couleur.ROUGE));
plateau.AjouterCode(code);
regles.PasserLaMain();
}
Assert.True(regles.EstTerminee());
}
[Fact]
public void EstTermineeNon()
{
ReglesClassiques regles = new ReglesClassiques();
regles.AjouterJoueur("1");
regles.AjouterJoueur("2");
regles.CommencerLaPartie();
for (int i = 0; i < 12; ++i)
regles.PasserLaMain();
Assert.False(regles.EstTerminee());
}
[Fact]
public void EstTermineeCodeTrouve()
{
ReglesClassiques regles = new ReglesClassiques();
regles.AjouterJoueur("1");
regles.AjouterJoueur("2");
regles.CommencerLaPartie();
Plateau plateauj1 = regles.JoueurCourant().Item2;
Type type = typeof(Plateau);
FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo);
Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1);
Assert.NotNull(codeSecret);
regles.JoueurCourant().Item2.AjouterCode(codeSecret);
Assert.True(regles.EstTerminee());
}
}
}

Loading…
Cancel
Save