From 324c394d0a75ab068d6fab91487dd322a9cb0dc1 Mon Sep 17 00:00:00 2001 From: "jeremy.mouyon" Date: Tue, 4 Jun 2024 14:22:50 +0200 Subject: [PATCH] the scoreboard work --- Qwirkle/QwirkleClassLibrary/Boards/Board.cs | 4 +- Qwirkle/QwirkleClassLibrary/Games/Game.cs | 81 ++++++++++++++----- Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs | 12 ++- Qwirkle/QwirkleViews/Pages/Gameboard.xaml | 8 +- Qwirkle/QwirkleViews/Views/Scoreboard.xaml | 21 ++++- Qwirkle/QwirkleViews/Views/Scoreboard.xaml.cs | 62 ++++++++------ Qwirkle/QwirkleViews/Views/TileView.xaml.cs | 6 -- 7 files changed, 131 insertions(+), 63 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs index 76088ea..7dfc7e5 100644 --- a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs +++ b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs @@ -34,10 +34,10 @@ namespace QwirkleClassLibrary.Boards } [DataMember] - public int Rows { get; set; } + public double Rows { get; set; } [DataMember] - public int Columns { get; set; } + public double Columns { get; set; } /// /// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board. diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index db72c31..90549e9 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -5,38 +5,46 @@ using QwirkleClassLibrary.Tiles; using QwirkleClassLibrary.Boards; using QwirkleClassLibrary.Events; using QwirkleClassLibrary.Players; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using static System.Formats.Asn1.AsnWriter; namespace QwirkleClassLibrary.Games { [DataContract] - public class Game : IPlayer, IRules + public class Game : IPlayer, IRules, INotifyPropertyChanged { [DataMember] private TileBag? bag = null; - + [DataMember] public bool GameRunning { get; set; } [DataMember] private Board board = new(15, 12); - public bool PlayerSwapping { get; set; } + public bool PlayerSwapping { get; set; } public Board Board => board; - + public ReadOnlyCollection PlayerList => players.AsReadOnly(); - + [DataMember] private readonly List players = []; - + [DataMember] - private readonly Dictionary scoreBoard = []; - + private readonly Dictionary scoreBoard = new Dictionary(); + public ReadOnlyDictionary ScoreBoard => scoreBoard.AsReadOnly(); + private ObservableCollection> observableScoreBoard = new ObservableCollection>(); + + public ReadOnlyObservableCollection> ObservableScoreBoard => + new ReadOnlyObservableCollection>(observableScoreBoard); + [DataMember] private readonly List cellUsed = []; - + public ReadOnlyCollection CellsUsed => cellUsed.AsReadOnly(); public event EventHandler? PlayerAddNotified; @@ -58,9 +66,17 @@ namespace QwirkleClassLibrary.Games protected virtual void OnEndOfGame(EndOfGameNotifiedEventArgs args) => EndOfGameNotified?.Invoke(this, args); - + public event EventHandler? SwapTilesNotified; - + + + public event PropertyChangedEventHandler? PropertyChanged; + + protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + protected virtual void OnSwapTiles(SwapTilesNotifiedEventArgs args) => SwapTilesNotified?.Invoke(this, args); @@ -104,7 +120,7 @@ namespace QwirkleClassLibrary.Games { Player pl = CreatePlayer(tag); players.Add(pl); - scoreBoard.Add(pl.NameTag, 0); + SetScoreBoard(pl.NameTag, 0); } OnPlayerNotified(new AddPlayerNotifiedEventArgs("Players were correctly added.")); @@ -323,7 +339,7 @@ namespace QwirkleClassLibrary.Games OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "you are swapping, you can't place tile !")); return false; } - if(!TileInbag(player, tile)) + if (!TileInbag(player, tile)) { OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "you can't play")); return false; @@ -381,7 +397,7 @@ namespace QwirkleClassLibrary.Games OnSwapTiles(new SwapTilesNotifiedEventArgs("You can't swap tiles after placing some !")); return false; } - + if (tilesToSwap.Count == 0) { OnSwapTiles(new SwapTilesNotifiedEventArgs("You must select at least one tile to swap !")); @@ -583,7 +599,7 @@ namespace QwirkleClassLibrary.Games int score = cellsPlayed.Count; int nbCellsInLine = cellsPlayed.Count; - + if (cellsPlayed.Count == 6) { score += 6; @@ -621,7 +637,9 @@ namespace QwirkleClassLibrary.Games if (!scoreBoard.TryAdd(player.NameTag, score)) { - scoreBoard[player.NameTag] += score; + + scoreBoard.TryGetValue(player.NameTag, out int scoreold); + SetScoreBoard(player.NameTag, score + scoreold); } return score; @@ -648,7 +666,7 @@ namespace QwirkleClassLibrary.Games b.GetCell(cell.GetX, cell.GetY + 1), b.GetCell(cell.GetX, cell.GetY - 1) }; - + var checkedSurroundingCells = new List(); foreach (var adjacentCell in surroundingCells) @@ -660,9 +678,9 @@ namespace QwirkleClassLibrary.Games int dx = adjacentCell.GetX - cell.GetX; int dy = adjacentCell.GetY - cell.GetY; - + score += CalculateLineScore(cellsPlayed, cell, new Tuple(dx, dy), b, new Tuple(cellsX, cellsY), ref nbCellsInLine); - + checkedSurroundingCells.Add(adjacentCell); } @@ -769,11 +787,12 @@ namespace QwirkleClassLibrary.Games { List playerTilesBagPos = CheckTilesBag(); - if (playerTilesBagPos.Count != 0 && !CheckPlacementPossibilities(playerTilesBagPos) || bag!.TilesBag!.Count == 0 && players[GetPlayingPlayerPosition()].Tiles.Count==0) + if (playerTilesBagPos.Count != 0 && !CheckPlacementPossibilities(playerTilesBagPos) || bag!.TilesBag!.Count == 0 && players[GetPlayingPlayerPosition()].Tiles.Count == 0) { OnEndOfGame(new EndOfGameNotifiedEventArgs(player)); GameRunning = false; - scoreBoard[player.NameTag] += 6; + scoreBoard.TryGetValue(player.NameTag, out int scoreold); + SetScoreBoard(player.NameTag, 6 + scoreold); return true; } @@ -789,5 +808,25 @@ namespace QwirkleClassLibrary.Games board = CreateBoard(); GameRunning = false; } + + public void SetScoreBoard(string name, int score) + { + + if (scoreBoard.ContainsKey(name)) + { + scoreBoard[name] = score; + } + else + { + scoreBoard.Add(name, score); + } + + observableScoreBoard.Clear(); + foreach (var item in scoreBoard) + { + observableScoreBoard.Add(item); + } + OnPropertyChanged(nameof(ObservableScoreBoard)); + } } } \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs index 6f4035a..b200327 100644 --- a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs +++ b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs @@ -108,6 +108,8 @@ public partial class Gameboard : ContentPage ColorBC3 = Colors.Transparent; ColorBC4 = Colors.Transparent; OnPropertyChanged(nameof(ColorBC1)); + OnPropertyChanged(nameof(ColorBC2)); + OnPropertyChanged(nameof(ColorBC3)); OnPropertyChanged(nameof(ColorBC4)); } if (game.GetPlayingPlayerPosition() == 1) @@ -118,6 +120,8 @@ public partial class Gameboard : ContentPage ColorBC4 = Colors.Transparent; OnPropertyChanged(nameof(ColorBC1)); OnPropertyChanged(nameof(ColorBC2)); + OnPropertyChanged(nameof(ColorBC3)); + OnPropertyChanged(nameof(ColorBC4)); } if (game.GetPlayingPlayerPosition() == 2) { @@ -125,8 +129,10 @@ public partial class Gameboard : ContentPage ColorBC1 = Colors.Transparent; ColorBC2 = Colors.Transparent; ColorBC4 = Colors.Transparent; - OnPropertyChanged(nameof(ColorBC3)); + OnPropertyChanged(nameof(ColorBC1)); OnPropertyChanged(nameof(ColorBC2)); + OnPropertyChanged(nameof(ColorBC3)); + OnPropertyChanged(nameof(ColorBC4)); } if (game.GetPlayingPlayerPosition() == 3) { @@ -134,8 +140,10 @@ public partial class Gameboard : ContentPage ColorBC1 = Colors.Transparent; ColorBC3 = Colors.Transparent; ColorBC2 = Colors.Transparent; - OnPropertyChanged(nameof(ColorBC4)); + OnPropertyChanged(nameof(ColorBC1)); + OnPropertyChanged(nameof(ColorBC2)); OnPropertyChanged(nameof(ColorBC3)); + OnPropertyChanged(nameof(ColorBC4)); } } diff --git a/Qwirkle/QwirkleViews/Pages/Gameboard.xaml b/Qwirkle/QwirkleViews/Pages/Gameboard.xaml index 057cd6f..098db2a 100644 --- a/Qwirkle/QwirkleViews/Pages/Gameboard.xaml +++ b/Qwirkle/QwirkleViews/Pages/Gameboard.xaml @@ -7,13 +7,11 @@ x:Name="root"> - - 75 + 75 75 1 1 - - + @@ -176,7 +174,7 @@ - + diff --git a/Qwirkle/QwirkleViews/Views/Scoreboard.xaml b/Qwirkle/QwirkleViews/Views/Scoreboard.xaml index 2128384..65604d0 100644 --- a/Qwirkle/QwirkleViews/Views/Scoreboard.xaml +++ b/Qwirkle/QwirkleViews/Views/Scoreboard.xaml @@ -15,7 +15,26 @@ - + + + + + + diff --git a/Qwirkle/QwirkleViews/Views/Scoreboard.xaml.cs b/Qwirkle/QwirkleViews/Views/Scoreboard.xaml.cs index 56402d9..1bbe5de 100644 --- a/Qwirkle/QwirkleViews/Views/Scoreboard.xaml.cs +++ b/Qwirkle/QwirkleViews/Views/Scoreboard.xaml.cs @@ -5,42 +5,52 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; -namespace Qwirkle.Views; - -public partial class Scoreboard : ContentView, INotifyPropertyChanged +namespace Qwirkle.Views { - private Game game = ((App)Application.Current!).Game; - - private IOrderedEnumerable> scoreboard; - - private ObservableCollection>? scoreboardList; - public ObservableCollection> ScoreboardList + public partial class Scoreboard : ContentView, INotifyPropertyChanged { - get => scoreboardList!; - set + private Game game = ((App)Application.Current!).Game; + + private ObservableCollection> scoreboardList; + public ObservableCollection> ScoreboardList { - if (scoreboardList != value) + get => scoreboardList; + set { - scoreboardList = value; - OnPropertyChanged(nameof(ScoreboardList)); + if (scoreboardList != value) + { + scoreboardList = value; + OnPropertyChanged(nameof(ScoreboardList)); + } } } - } + public Scoreboard() + { + InitializeComponent(); + BindingContext = this; - public Scoreboard() - { - InitializeComponent(); - BindingContext = this; - scoreboard = game.ScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key); - ScoreboardList = new ObservableCollection>(scoreboard); + UpdateScoreboard(); - } + game.PropertyChanged += OnScoreChanged; + } - public event PropertyChangedEventHandler? PropertyChanged; + private void UpdateScoreboard() + { + var scoreboard = game.ObservableScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key); + ScoreboardList = new ObservableCollection>(scoreboard); + } - protected void OnPropertyChanged(string propertyName) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + private void OnScoreChanged(object sender, EventArgs e) + { + UpdateScoreboard(); + } + + public event PropertyChangedEventHandler PropertyChanged; + + protected void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } } diff --git a/Qwirkle/QwirkleViews/Views/TileView.xaml.cs b/Qwirkle/QwirkleViews/Views/TileView.xaml.cs index beadd41..d7f0ad1 100644 --- a/Qwirkle/QwirkleViews/Views/TileView.xaml.cs +++ b/Qwirkle/QwirkleViews/Views/TileView.xaml.cs @@ -18,12 +18,6 @@ public partial class TileView : ContentView public TileView() { InitializeComponent(); - ((App)Application.Current!).Game.Board.PropertyChanged += Board_PropertyChanged; - } - - private void Board_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) - { - Console.WriteLine(Shape); } public static readonly BindableProperty ColorProperty =