the scoreboard work
continuous-integration/drone/push Build is passing Details

test_old_branch
Jérémy Mouyon 11 months ago
parent fbcce2b5c8
commit 324c394d0a

@ -34,10 +34,10 @@ namespace QwirkleClassLibrary.Boards
} }
[DataMember] [DataMember]
public int Rows { get; set; } public double Rows { get; set; }
[DataMember] [DataMember]
public int Columns { get; set; } public double Columns { get; set; }
/// <summary> /// <summary>
/// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board. /// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board.

@ -5,12 +5,15 @@ using QwirkleClassLibrary.Tiles;
using QwirkleClassLibrary.Boards; using QwirkleClassLibrary.Boards;
using QwirkleClassLibrary.Events; using QwirkleClassLibrary.Events;
using QwirkleClassLibrary.Players; using QwirkleClassLibrary.Players;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using static System.Formats.Asn1.AsnWriter;
namespace QwirkleClassLibrary.Games namespace QwirkleClassLibrary.Games
{ {
[DataContract] [DataContract]
public class Game : IPlayer, IRules public class Game : IPlayer, IRules, INotifyPropertyChanged
{ {
[DataMember] [DataMember]
private TileBag? bag = null; private TileBag? bag = null;
@ -21,7 +24,7 @@ namespace QwirkleClassLibrary.Games
[DataMember] [DataMember]
private Board board = new(15, 12); private Board board = new(15, 12);
public bool PlayerSwapping { get; set; } public bool PlayerSwapping { get; set; }
public Board Board => board; public Board Board => board;
public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly(); public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly();
@ -30,10 +33,15 @@ namespace QwirkleClassLibrary.Games
private readonly List<Player> players = []; private readonly List<Player> players = [];
[DataMember] [DataMember]
private readonly Dictionary<string, int> scoreBoard = []; private readonly Dictionary<string, int> scoreBoard = new Dictionary<string, int>();
public ReadOnlyDictionary<string, int> ScoreBoard => scoreBoard.AsReadOnly(); public ReadOnlyDictionary<string, int> ScoreBoard => scoreBoard.AsReadOnly();
private ObservableCollection<KeyValuePair<string, int>> observableScoreBoard = new ObservableCollection<KeyValuePair<string, int>>();
public ReadOnlyObservableCollection<KeyValuePair<string, int>> ObservableScoreBoard =>
new ReadOnlyObservableCollection<KeyValuePair<string, int>>(observableScoreBoard);
[DataMember] [DataMember]
private readonly List<Cell> cellUsed = []; private readonly List<Cell> cellUsed = [];
@ -61,6 +69,14 @@ namespace QwirkleClassLibrary.Games
public event EventHandler<SwapTilesNotifiedEventArgs>? SwapTilesNotified; public event EventHandler<SwapTilesNotifiedEventArgs>? 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) protected virtual void OnSwapTiles(SwapTilesNotifiedEventArgs args)
=> SwapTilesNotified?.Invoke(this, args); => SwapTilesNotified?.Invoke(this, args);
@ -104,7 +120,7 @@ namespace QwirkleClassLibrary.Games
{ {
Player pl = CreatePlayer(tag); Player pl = CreatePlayer(tag);
players.Add(pl); players.Add(pl);
scoreBoard.Add(pl.NameTag, 0); SetScoreBoard(pl.NameTag, 0);
} }
OnPlayerNotified(new AddPlayerNotifiedEventArgs("Players were correctly added.")); 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 !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "you are swapping, you can't place tile !"));
return false; return false;
} }
if(!TileInbag(player, tile)) if (!TileInbag(player, tile))
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "you can't play")); OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "you can't play"));
return false; return false;
@ -621,7 +637,9 @@ namespace QwirkleClassLibrary.Games
if (!scoreBoard.TryAdd(player.NameTag, score)) if (!scoreBoard.TryAdd(player.NameTag, score))
{ {
scoreBoard[player.NameTag] += score;
scoreBoard.TryGetValue(player.NameTag, out int scoreold);
SetScoreBoard(player.NameTag, score + scoreold);
} }
return score; return score;
@ -769,11 +787,12 @@ namespace QwirkleClassLibrary.Games
{ {
List<int> playerTilesBagPos = CheckTilesBag(); List<int> 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)); OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
GameRunning = false; GameRunning = false;
scoreBoard[player.NameTag] += 6; scoreBoard.TryGetValue(player.NameTag, out int scoreold);
SetScoreBoard(player.NameTag, 6 + scoreold);
return true; return true;
} }
@ -789,5 +808,25 @@ namespace QwirkleClassLibrary.Games
board = CreateBoard(); board = CreateBoard();
GameRunning = false; 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));
}
} }
} }

@ -108,6 +108,8 @@ public partial class Gameboard : ContentPage
ColorBC3 = Colors.Transparent; ColorBC3 = Colors.Transparent;
ColorBC4 = Colors.Transparent; ColorBC4 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC1)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4)); OnPropertyChanged(nameof(ColorBC4));
} }
if (game.GetPlayingPlayerPosition() == 1) if (game.GetPlayingPlayerPosition() == 1)
@ -118,6 +120,8 @@ public partial class Gameboard : ContentPage
ColorBC4 = Colors.Transparent; ColorBC4 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC1)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2)); OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4));
} }
if (game.GetPlayingPlayerPosition() == 2) if (game.GetPlayingPlayerPosition() == 2)
{ {
@ -125,8 +129,10 @@ public partial class Gameboard : ContentPage
ColorBC1 = Colors.Transparent; ColorBC1 = Colors.Transparent;
ColorBC2 = Colors.Transparent; ColorBC2 = Colors.Transparent;
ColorBC4 = Colors.Transparent; ColorBC4 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC3)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2)); OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4));
} }
if (game.GetPlayingPlayerPosition() == 3) if (game.GetPlayingPlayerPosition() == 3)
{ {
@ -134,8 +140,10 @@ public partial class Gameboard : ContentPage
ColorBC1 = Colors.Transparent; ColorBC1 = Colors.Transparent;
ColorBC3 = Colors.Transparent; ColorBC3 = Colors.Transparent;
ColorBC2 = Colors.Transparent; ColorBC2 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC4)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3)); OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4));
} }
} }

@ -7,13 +7,11 @@
x:Name="root"> x:Name="root">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary> <x:Double x:Key="CellWidth">75</x:Double>
<x:Double x:Key="CellWidth">75</x:Double>
<x:Double x:Key="CellHeight">75</x:Double> <x:Double x:Key="CellHeight">75</x:Double>
<x:Double x:Key="VerticalSpacing">1</x:Double> <x:Double x:Key="VerticalSpacing">1</x:Double>
<x:Double x:Key="HorizontalSpacing">1</x:Double> <x:Double x:Key="HorizontalSpacing">1</x:Double>
<toolkit:MultiMathExpressionConverter x:Key="multiMathExpressionConverter" /> <toolkit:MultiMathExpressionConverter x:Key="multiMathExpressionConverter" />
</ResourceDictionary>
</ContentPage.Resources> </ContentPage.Resources>
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
@ -176,7 +174,7 @@
</CollectionView> </CollectionView>
</ScrollView> </ScrollView>
<controls:Scoreboard HorizontalOptions="End" Grid.Row="1" Grid.Column="1" ></controls:Scoreboard> <controls:Scoreboard HorizontalOptions="End" Grid.Row="1" Grid.Column="1" ></controls:Scoreboard>

@ -15,7 +15,26 @@
</CollectionView.ItemsLayout> </CollectionView.ItemsLayout>
<CollectionView.ItemTemplate> <CollectionView.ItemTemplate>
<DataTemplate> <DataTemplate>
<controls:ScoreboardLine Nameplayer="{Binding Key}" Score="{Binding Value}"/> <StackLayout>
<Grid ColumnDefinitions="4*, auto, 2*"
RowDefinitions="50">
<Label
Grid.Column="0"
Text="{Binding Key}"
Style="{StaticResource ContentTab}"/>
<Rectangle
Style="{StaticResource RectangleTab}"
Grid.Column="1"/>
<Label
Grid.Column="2"
Text="{Binding Value}"
Style="{StaticResource ContentTab}"/>
</Grid>
<Rectangle/>
</StackLayout>
</DataTemplate> </DataTemplate>
</CollectionView.ItemTemplate> </CollectionView.ItemTemplate>
</CollectionView> </CollectionView>

@ -5,42 +5,52 @@ using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
namespace Qwirkle.Views; namespace Qwirkle.Views
public partial class Scoreboard : ContentView, INotifyPropertyChanged
{ {
private Game game = ((App)Application.Current!).Game; public partial class Scoreboard : ContentView, INotifyPropertyChanged
private IOrderedEnumerable<KeyValuePair<string, int>> scoreboard;
private ObservableCollection<KeyValuePair<string, int>>? scoreboardList;
public ObservableCollection<KeyValuePair<string, int>> ScoreboardList
{ {
get => scoreboardList!; private Game game = ((App)Application.Current!).Game;
set
private ObservableCollection<KeyValuePair<string, int>> scoreboardList;
public ObservableCollection<KeyValuePair<string, int>> ScoreboardList
{ {
if (scoreboardList != value) get => scoreboardList;
set
{ {
scoreboardList = value; if (scoreboardList != value)
OnPropertyChanged(nameof(ScoreboardList)); {
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); UpdateScoreboard();
ScoreboardList = new ObservableCollection<KeyValuePair<string, int>>(scoreboard);
} 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<KeyValuePair<string, int>>(scoreboard);
}
protected void OnPropertyChanged(string propertyName) private void OnScoreChanged(object sender, EventArgs e)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); UpdateScoreboard();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} }
} }

@ -18,12 +18,6 @@ public partial class TileView : ContentView
public TileView() public TileView()
{ {
InitializeComponent(); 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 = public static readonly BindableProperty ColorProperty =

Loading…
Cancel
Save