Plateau fonctionnel mais pas beau. Pas de fin de partie
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
40607cd2ee
commit
457e16041d
@ -0,0 +1,78 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||||
|
x:Class="MauiSpark.Pages.Plateau"
|
||||||
|
Title="Plateau">
|
||||||
|
|
||||||
|
<ScrollView>
|
||||||
|
<StackLayout Padding="20">
|
||||||
|
<Label Text="Enter the player's name:"
|
||||||
|
FontSize="Medium"
|
||||||
|
VerticalOptions="CenterAndExpand" />
|
||||||
|
<Entry x:Name="PlayerNameEntry1"
|
||||||
|
Placeholder="Player Name"
|
||||||
|
FontSize="Medium"/>
|
||||||
|
<Entry x:Name="PlayerNameEntry2"
|
||||||
|
Placeholder="Player Name"
|
||||||
|
FontSize="Medium"/>
|
||||||
|
<Button Text="Submit" Clicked="OnSubmitClicked" VerticalOptions="EndAndExpand" />
|
||||||
|
<Label Text="Tour en cours :" FontSize="Large" />
|
||||||
|
<Label Text="{Binding Tour}" FontSize="Large" />
|
||||||
|
<Label Text="Joueur :" FontSize="Large" />
|
||||||
|
<Label Text="{Binding Nom}" FontSize="Large" />
|
||||||
|
<ListView ItemsSource="{Binding Jetons}">
|
||||||
|
<ListView.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<ViewCell>
|
||||||
|
<Label Text="{Binding Couleur}" />
|
||||||
|
</ViewCell>
|
||||||
|
</DataTemplate>
|
||||||
|
</ListView.ItemTemplate>
|
||||||
|
</ListView>
|
||||||
|
|
||||||
|
<Grid x:Name="PlateauGrid" RowSpacing="5" ColumnSpacing="5">
|
||||||
|
<Grid ColumnDefinitions="*,*" Margin="50" BackgroundColor="White">
|
||||||
|
<ListView Grid.Column="0" ItemsSource="{Binding Grille}">
|
||||||
|
<ListView.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<ViewCell>
|
||||||
|
<Grid ColumnDefinitions="*,*,*,*" Grid.Column="0">
|
||||||
|
<Label Grid.Column="0" Text="{Binding [0].Couleur}" TextColor="Black"/>
|
||||||
|
<Label Grid.Column="1" Text="{Binding [1].Couleur}" TextColor="Black" />
|
||||||
|
<Label Grid.Column="2" Text="{Binding [2].Couleur}" TextColor="Black" />
|
||||||
|
<Label Grid.Column="3" Text="{Binding [3].Couleur}" TextColor="Black" />
|
||||||
|
</Grid>
|
||||||
|
</ViewCell>
|
||||||
|
</DataTemplate>
|
||||||
|
</ListView.ItemTemplate>
|
||||||
|
</ListView>
|
||||||
|
<ListView Grid.Column="1" ItemsSource="{Binding Indicateurs}" >
|
||||||
|
<ListView.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<ViewCell>
|
||||||
|
<Grid ColumnDefinitions="*,*,*,*" Grid.Column="0">
|
||||||
|
<Label Grid.Column="0" Text="{Binding [0]}" TextColor="Black"/>
|
||||||
|
<Label Grid.Column="1" Text="{Binding [1]}" TextColor="Black"/>
|
||||||
|
<Label Grid.Column="2" Text="{Binding [2]}" TextColor="Black"/>
|
||||||
|
<Label Grid.Column="3" Text="{Binding [3]}" TextColor="Black"/>
|
||||||
|
</Grid>
|
||||||
|
</ViewCell>
|
||||||
|
</DataTemplate>
|
||||||
|
</ListView.ItemTemplate>
|
||||||
|
</ListView>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
<Label Text="Sélectionnez une couleur de jeton : " />
|
||||||
|
<VerticalStackLayout Spacing="1">
|
||||||
|
<Button Text="Rouge" Clicked="OnColorButtonClicked" />
|
||||||
|
<Button Text="Vert" Clicked="OnColorButtonClicked" />
|
||||||
|
<Button Text="Bleu" Clicked="OnColorButtonClicked" />
|
||||||
|
<Button Text="Jaune" Clicked="OnColorButtonClicked" />
|
||||||
|
<Button Text="Blanc" Clicked="OnColorButtonClicked" />
|
||||||
|
<Button Text="Noir" Clicked="OnColorButtonClicked" />
|
||||||
|
</VerticalStackLayout>
|
||||||
|
<Button Text="Valider" Clicked="OnValidateButtonClicked" />
|
||||||
|
<Button Text="Supprimmer le dernier Jeton" Clicked="OnSupprimmerButtonClicked" />
|
||||||
|
</StackLayout>
|
||||||
|
</ScrollView>
|
||||||
|
</ContentPage>
|
@ -0,0 +1,220 @@
|
|||||||
|
using CoreLibrary.Events;
|
||||||
|
using CoreLibrary.Manager;
|
||||||
|
using CoreLibrary;
|
||||||
|
using CoreLibrary.Regles;
|
||||||
|
using CoreLibrary.Joueurs;
|
||||||
|
using Microsoft.Maui.Controls;
|
||||||
|
using CoreLibrary.Core;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using CoreLibrary.Exceptions;
|
||||||
|
|
||||||
|
namespace MauiSpark.Pages
|
||||||
|
{
|
||||||
|
public partial class Plateau : ContentPage, INotifyPropertyChanged
|
||||||
|
{
|
||||||
|
|
||||||
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
|
|
||||||
|
public void QuandPropertyChanged([CallerMemberName] string propertyName = "")
|
||||||
|
{
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
|
}
|
||||||
|
private TaskCompletionSource<CouleurSelectionResult> CouleurSelectedTask;
|
||||||
|
public Couleur? selectedColor;
|
||||||
|
public Jeton selectedJeton;
|
||||||
|
public List<string> Noms { get; set; }
|
||||||
|
public string Nom { get; set; }
|
||||||
|
public IRegles regles;
|
||||||
|
public Partie partie;
|
||||||
|
Joueur joueur;
|
||||||
|
public int Tour { get; set; }
|
||||||
|
public IEnumerable<IEnumerable<Jeton?>> Grille { get; set; }
|
||||||
|
public IEnumerable<IEnumerable<Indicateur>> Indicateurs { get; set; }
|
||||||
|
public ObservableCollection<Jeton?> Jetons { get; set; }
|
||||||
|
|
||||||
|
public Plateau()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
regles = new ReglesClassiques();
|
||||||
|
partie = new Partie(regles);
|
||||||
|
partie.DemanderNom += DemanderNom;
|
||||||
|
partie.DebutPartie += OnDebutPartie;
|
||||||
|
partie.NouveauTour += NouveauTourAsync;
|
||||||
|
partie.PartieTerminee += OnPartieTerminee;
|
||||||
|
Jetons = new ObservableCollection<Jeton?>();
|
||||||
|
BindingContext = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnSubmitClicked(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Noms = new List<string> { PlayerNameEntry1.Text, PlayerNameEntry2.Text };
|
||||||
|
partie.Jouer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DemanderNom(object sender, DemanderNomEventArgs e)
|
||||||
|
{
|
||||||
|
int joueurIndex = e.Indice - 1;
|
||||||
|
if (joueurIndex >= 0 && joueurIndex < Noms.Count())
|
||||||
|
{
|
||||||
|
e.JoueurBuilder.Nom(Noms[joueurIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDebutPartie(object sender, DebutPartieEventArgs e)
|
||||||
|
{
|
||||||
|
DisplayAlert("Début de la partie", "La partie commence !", "OK");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnPartieTerminee(object sender, PartieTermineeEventArgs e)
|
||||||
|
{
|
||||||
|
DisplayAlert("Partie terminée", "La partie est terminée !", "OK");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnColorButtonClicked(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Button button = (Button)sender;
|
||||||
|
string colorText = button.Text;
|
||||||
|
selectedColor = GetColorFromString(colorText);
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Couleur sélectionnée: {selectedColor}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnValidateButtonClicked(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (selectedColor.HasValue)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Couleur validée: {selectedColor.Value}");
|
||||||
|
OnJetonSelected(selectedColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnSupprimmerButtonClicked(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
OnJetonSelected(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void NouveauTourAsync(Object? sender, NouveauTourEventArgs e)
|
||||||
|
{
|
||||||
|
Code code = e.Code;
|
||||||
|
Tour = e.Tour;
|
||||||
|
Grille = e.Grille;
|
||||||
|
Indicateurs = e.Indicateurs;
|
||||||
|
joueur = regles.JoueurCourant().Item1;
|
||||||
|
Nom = joueur.Nom;
|
||||||
|
QuandPropertyChanged(nameof(joueur));
|
||||||
|
QuandPropertyChanged(nameof(Tour));
|
||||||
|
QuandPropertyChanged(nameof(Grille));
|
||||||
|
QuandPropertyChanged(nameof(Indicateurs));
|
||||||
|
QuandPropertyChanged(nameof(Nom));
|
||||||
|
|
||||||
|
while (!code.EstComplet())
|
||||||
|
{
|
||||||
|
await DisplayAlert("Cliquer sur une couleur", "Cliquer sur une couleur", "OK");
|
||||||
|
System.Diagnostics.Debug.WriteLine("En attente de la sélection d'un jeton...");
|
||||||
|
|
||||||
|
CouleurSelectionResult result = await WaitForCouleurSelection();
|
||||||
|
if (result.IsSuppression)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("Supprimer le dernier jeton suite à une suppression");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
code.SupprimerDernierJeton();
|
||||||
|
}
|
||||||
|
catch (CodeVideException)
|
||||||
|
{
|
||||||
|
await DisplayAlert("Le code est deja vide", "Vous ne pouvez pas supprimmer", "OK");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Jeton jetonSelectionne = new Jeton(result.Couleur.Value);
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Jeton sélectionné: {jetonSelectionne.Couleur}");
|
||||||
|
code.AjouterJeton(jetonSelectionne);
|
||||||
|
}
|
||||||
|
|
||||||
|
Jetons.Clear();
|
||||||
|
foreach (Jeton? jeton in code.Jetons())
|
||||||
|
{
|
||||||
|
if (jeton.HasValue)
|
||||||
|
{
|
||||||
|
Jetons.Add(jeton.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.Joueur.Code(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private Task<CouleurSelectionResult> WaitForCouleurSelection()
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("WaitForJetonSelection appelé");
|
||||||
|
CouleurSelectedTask = new TaskCompletionSource<CouleurSelectionResult>();
|
||||||
|
return CouleurSelectedTask.Task;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnJetonSelected(Couleur? selectedColor)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("OnJetonSelected appelé");
|
||||||
|
|
||||||
|
if (CouleurSelectedTask != null && !CouleurSelectedTask.Task.IsCompleted)
|
||||||
|
{
|
||||||
|
if (selectedColor.HasValue)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"OnJetonSelected - complétion de la tâche avec: {selectedColor.Value}");
|
||||||
|
CouleurSelectedTask.SetResult(CouleurSelectionResult.FromCouleur(selectedColor.Value));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("OnJetonSelected - complétion de la tâche avec un jeton nul (suppression)");
|
||||||
|
CouleurSelectedTask.SetResult(CouleurSelectionResult.Suppression());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("OnJetonSelected - tâche déjà complétée ou null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Couleur? GetColorFromString(string colorText)
|
||||||
|
{
|
||||||
|
return colorText.ToLower() switch
|
||||||
|
{
|
||||||
|
"rouge" => Couleur.ROUGE,
|
||||||
|
"vert" => Couleur.VERT,
|
||||||
|
"bleu" => Couleur.BLEU,
|
||||||
|
"jaune" => Couleur.JAUNE,
|
||||||
|
"blanc" => Couleur.BLANC,
|
||||||
|
"noir" => Couleur.NOIR,
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class CouleurSelectionResult
|
||||||
|
{
|
||||||
|
public Couleur? Couleur { get; }
|
||||||
|
public bool IsSuppression { get; }
|
||||||
|
|
||||||
|
private CouleurSelectionResult(Couleur? couleur, bool isSuppression)
|
||||||
|
{
|
||||||
|
Couleur = couleur;
|
||||||
|
IsSuppression = isSuppression;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CouleurSelectionResult FromCouleur(Couleur couleur)
|
||||||
|
{
|
||||||
|
return new CouleurSelectionResult(couleur, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CouleurSelectionResult Suppression()
|
||||||
|
{
|
||||||
|
return new CouleurSelectionResult(null, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue