diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index 732fdb2..c61d56d 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -34,13 +34,11 @@ namespace QwirkleClassLibrary.Games [DataMember] private readonly Dictionary scoreBoard = new Dictionary(); - public ReadOnlyDictionary ScoreBoard => scoreBoard.AsReadOnly(); private readonly ObservableCollection> observableScoreBoard = []; - public ReadOnlyObservableCollection> ObservableScoreBoard => - new ReadOnlyObservableCollection>(observableScoreBoard); + new(observableScoreBoard); [DataMember] private readonly List cellUsed = []; @@ -274,7 +272,7 @@ namespace QwirkleClassLibrary.Games { foreach (var p in players) { - for (int j = 0; j < 6; j++) + for (int j = 0; j < 1; j++) // 6 { if (bag != null && p.Tiles.Count < 6) { @@ -761,7 +759,7 @@ namespace QwirkleClassLibrary.Games { List playerTilesBagPos = []; - if (bag!.TilesBag!.Count == 0) + if (bag!.TilesBag!.Count <= 12) { for (int i = 0; i < players.Count; i++) { @@ -830,6 +828,7 @@ namespace QwirkleClassLibrary.Games players.Clear(); scoreBoard.Clear(); cellUsed.Clear(); + observableScoreBoard.Clear(); bag = null; board = CreateBoard(); GameRunning = false; diff --git a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs index e77bda7..2aa6fad 100644 --- a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs +++ b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs @@ -1,7 +1,10 @@ -using System; +using QwirkleClassLibrary.Tiles; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using System.Runtime.Serialization; using System.Text; @@ -10,14 +13,29 @@ using System.Threading.Tasks; namespace QwirkleClassLibrary.Players { [DataContract] - public class Leaderboard + public class Leaderboard : INotifyPropertyChanged { - public ReadOnlyCollection Lb => leaderboard.AsReadOnly(); - [DataMember] - private readonly List leaderboard = new(); - - + private readonly ObservableCollection leaderboard = new(); + + public event PropertyChangedEventHandler? PropertyChanged; + + protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + public ReadOnlyObservableCollection Lb + { + get + { + return (ReadOnlyObservableCollection)leaderboard.AsReadOnly(); + } + } + + + + /// /// Returns the index of the player in the leaderboard, -1 if the player is not in the leaderboard /// @@ -57,11 +75,13 @@ namespace QwirkleClassLibrary.Players if (first) { leaderboard[i].Victories++; + OnPropertyChanged(nameof(leaderboard)); } if (pair.Value > leaderboard[i].Points) { leaderboard[i].Points = pair.Value; + OnPropertyChanged(nameof(leaderboard)); } } @@ -74,6 +94,7 @@ namespace QwirkleClassLibrary.Players } Score score = new Score(pair.Key, now, pair.Value, v); leaderboard.Add(score); + OnPropertyChanged(nameof(leaderboard)); } first = false; diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs index 291a4d0..9088b05 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs @@ -24,22 +24,28 @@ namespace QwirkleClassLibrary.Tiles /// Throw an exception if the number of copies is negative (impossible) or superior to 3 (contradiction with the rules). public TileBag(int nbSet) { - if (nbSet < 0 || nbSet > 3) - { - throw new ArgumentException(nbSet.ToString()); - } + /* if (nbSet < 0 || nbSet > 3) + { + throw new ArgumentException(nbSet.ToString()); + } - for (int i = 0; i < nbSet; i++) - { - foreach (Shape s in Enum.GetValues(typeof(Shape))) - { - foreach (Color c in Enum.GetValues(typeof(Color))) - { - Tile t = new(s, c); - tiles.Add(t); - } - } - } + for (int i = 0; i < nbSet; i++) + { + foreach (Shape s in Enum.GetValues(typeof(Shape))) + { + foreach (Color c in Enum.GetValues(typeof(Color))) + { + Tile t = new(s, c); + tiles.Add(t); + } + } + }*/ + + Tile t1 = new Tile(Shape.Club, Color.Red); + Tile t2 = new Tile(Shape.Round, Color.Orange); + + tiles.Add(t1); + tiles.Add(t2); Init(); } diff --git a/Qwirkle/QwirkleViews/App.xaml.cs b/Qwirkle/QwirkleViews/App.xaml.cs index 97133c1..12606b5 100644 --- a/Qwirkle/QwirkleViews/App.xaml.cs +++ b/Qwirkle/QwirkleViews/App.xaml.cs @@ -2,6 +2,7 @@ using Microsoft.Maui.Controls; using Qwirkle.Pages; using QwirkleClassLibrary.Games; +using QwirkleClassLibrary.Players; namespace Qwirkle { @@ -17,9 +18,12 @@ namespace Qwirkle Routing.RegisterRoute(nameof(Gameboard), typeof(Gameboard)); Routing.RegisterRoute(nameof(Rules), typeof(Rules)); Routing.RegisterRoute(nameof(MainPage), typeof(MainPage)); + Routing.RegisterRoute(nameof(Qwirkle.Pages.Leaderboard), typeof(Qwirkle.Pages.Leaderboard)); } public Game Game { get; set; } = new(); + public QwirkleClassLibrary.Players.Leaderboard LD { get; set; } = new(); + } } diff --git a/Qwirkle/QwirkleViews/MainPage.xaml b/Qwirkle/QwirkleViews/MainPage.xaml index 87660c6..607867c 100644 --- a/Qwirkle/QwirkleViews/MainPage.xaml +++ b/Qwirkle/QwirkleViews/MainPage.xaml @@ -26,7 +26,8 @@ InfoClicked="OnInfoClicked" /> - + diff --git a/Qwirkle/QwirkleViews/MainPage.xaml.cs b/Qwirkle/QwirkleViews/MainPage.xaml.cs index 99a35e1..c36e134 100644 --- a/Qwirkle/QwirkleViews/MainPage.xaml.cs +++ b/Qwirkle/QwirkleViews/MainPage.xaml.cs @@ -38,6 +38,11 @@ namespace Qwirkle Navigation.PushAsync(new Credits()); } + public void OnCLeaderboardClicked(object sender, EventArgs e) + { + Navigation.PushAsync(new Leaderboard()); + } + } } diff --git a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs index 92c4fd9..0ed74ae 100644 --- a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs +++ b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs @@ -9,6 +9,7 @@ using System.ComponentModel; using Cell = QwirkleClassLibrary.Boards.Cell; using Color = Microsoft.Maui.Graphics.Color; using System.Drawing; +using CommunityToolkit.Maui.Views; namespace Qwirkle.Pages; @@ -42,9 +43,13 @@ public partial class Gameboard : ContentPage private void Game_EndOfGameNotified(object? sender, EndOfGameNotifiedEventArgs e) { - DisplayAlert("THE END.", "FAUT QU'ON PARLE DE CE QUE QU'ON VEUT FAIRE ICI !!!" ,"<3"); + PopUpEnd(); Navigation.PushAsync(new MainPage()); - game.ClearGame(); + } + + private async void PopUpEnd() + { + await this.ShowPopupAsync(new PopUpEndGame()); } private void OnDragStarting(object sender, DragStartingEventArgs e) @@ -107,12 +112,19 @@ public partial class Gameboard : ContentPage game.DrawTiles(game.GetPlayingPlayer()); } - game.CheckGameOver(game.GetPlayingPlayer()); - game.SetNextPlayer(); + if (!game.CheckGameOver(game.GetPlayingPlayer())) + { + game.SetNextPlayer(); + ChangeColorBC(); + } + else + { + ((App)Application.Current!).LD.AddScoreInLead(game.ScoreBoard); + game.ClearGame(); + } game.NextPlayerNotified -= Game_NextPlayerNotified; game.EndOfGameNotified -= Game_EndOfGameNotified; - ChangeColorBC(); } private void ChangeColorBC() diff --git a/Qwirkle/QwirkleViews/PopUpEndGame.xaml b/Qwirkle/QwirkleViews/PopUpEndGame.xaml new file mode 100644 index 0000000..cb70e4b --- /dev/null +++ b/Qwirkle/QwirkleViews/PopUpEndGame.xaml @@ -0,0 +1,55 @@ + + + + + + + \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/PopUpEndGame.xaml.cs b/Qwirkle/QwirkleViews/PopUpEndGame.xaml.cs new file mode 100644 index 0000000..fe153d9 --- /dev/null +++ b/Qwirkle/QwirkleViews/PopUpEndGame.xaml.cs @@ -0,0 +1,38 @@ +using CommunityToolkit.Maui.Views; +using QwirkleClassLibrary.Games; +using System.Collections.ObjectModel; + +namespace Qwirkle; + +public partial class PopUpEndGame : Popup +{ + public PopUpEndGame() + { + InitializeComponent(); + + var scoreboard = game.ObservableScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key); + ScoreboardList = new ObservableCollection>(scoreboard); + } + + private Game game = ((App)Application.Current!).Game; + + private ObservableCollection>? scoreboardList; + public ObservableCollection>? ScoreboardList + { + get => scoreboardList; + set + { + if (scoreboardList != value) + { + scoreboardList = value; + OnPropertyChanged(nameof(ScoreboardList)); + } + } + } + + public async void OnButtonNextClick(object sender, EventArgs e) + { + await CloseAsync(); + } + +} diff --git a/Qwirkle/QwirkleViews/Resources/AppIcon/qwirkle.svg b/Qwirkle/QwirkleViews/Resources/AppIcon/qwirkle.svg new file mode 100644 index 0000000..df17ee1 --- /dev/null +++ b/Qwirkle/QwirkleViews/Resources/AppIcon/qwirkle.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/Views/ScoreboardLine.xaml b/Qwirkle/QwirkleViews/Views/ScoreboardLine.xaml deleted file mode 100644 index 7fde565..0000000 --- a/Qwirkle/QwirkleViews/Views/ScoreboardLine.xaml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/Views/ScoreboardLine.xaml.cs b/Qwirkle/QwirkleViews/Views/ScoreboardLine.xaml.cs deleted file mode 100644 index ca8f273..0000000 --- a/Qwirkle/QwirkleViews/Views/ScoreboardLine.xaml.cs +++ /dev/null @@ -1,43 +0,0 @@ - - -namespace Qwirkle.Views -{ - public partial class ScoreboardLine : ContentView - { - public static readonly BindableProperty NameplayerProperty = - BindableProperty.Create(nameof(Nameplayer), typeof(string), typeof(ScoreboardLine), default(string), propertyChanged: OnPlayerChanged); - - public string Nameplayer - { - get => (string)GetValue(NameplayerProperty); - set => SetValue(NameplayerProperty, value); - } - - private static void OnPlayerChanged(BindableObject bindable, object oldValue, object newValue) - { - var bin = (ScoreboardLine)bindable; - bin.OnPropertyChanged(nameof(Nameplayer)); - } - - public static readonly BindableProperty ScoreProperty = - BindableProperty.Create(nameof(Score), typeof(int), typeof(ScoreboardLine), default(int), propertyChanged: OnScoreChanged); - - public int Score - { - get => (int)GetValue(ScoreProperty); - set => SetValue(ScoreProperty, value); - } - - private static void OnScoreChanged(BindableObject bindable, object oldValue, object newValue) - { - var bin = (ScoreboardLine)bindable; - bin.OnPropertyChanged(nameof(Score)); - } - - public ScoreboardLine() - { - InitializeComponent(); - BindingContext = this; - } - } -}