diff --git a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs index 496f0e3..b13c4f8 100644 --- a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs +++ b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Security.Cryptography; using System.Text; @@ -14,12 +16,21 @@ namespace QwirkleClassLibrary.Boards /// This class is used to create the board for our Qwirkle Game. It uses others classes, such as Cell and Tile, to take care of the tiles placements during the game. /// [DataContract] - public class Board + public class Board : INotifyPropertyChanged { - public ReadOnlyCollection ReadCells => cells.AsReadOnly(); - + public event PropertyChangedEventHandler? PropertyChanged; + [DataMember] - private readonly List cells = new(); + private ObservableCollection cells = new ObservableCollection(); + public ObservableCollection ReadCells + { + get { return cells; } + set + { + cells = value; + OnPropertyChanged(nameof(ReadCells)); + } + } public int Rows { get; } public int Columns { get; } @@ -54,6 +65,7 @@ namespace QwirkleClassLibrary.Boards { if (!cell.IsFree) { + OnPropertyChanged(nameof(ReadCells)); return true; } } @@ -74,9 +86,11 @@ namespace QwirkleClassLibrary.Boards if (t.GetX != x || t.GetY != y) continue; if (t.IsFree) { + OnPropertyChanged(nameof(ReadCells)); return t.SetTile(tile); } } + OnPropertyChanged(nameof(ReadCells)); return false; } @@ -87,16 +101,7 @@ namespace QwirkleClassLibrary.Boards /// The cells of the board in a List format. public List GetCells() { - return cells; - } - - /// - /// A getter for the ReadCells attribute of the Board Class. - /// - /// The cells of the board, but in a IReadOnlyCollection format. - public IReadOnlyCollection GetReadCells() - { - return ReadCells; + return cells.ToList(); } /// @@ -117,5 +122,9 @@ namespace QwirkleClassLibrary.Boards return null; } + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } } diff --git a/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs b/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs index 5a894f0..ea9353b 100644 --- a/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs +++ b/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs @@ -1,4 +1,5 @@ // ReSharper disable All +using System.ComponentModel; using System.Runtime.CompilerServices; using System.Runtime.Serialization; using QwirkleClassLibrary.Tiles; @@ -6,16 +7,18 @@ using QwirkleClassLibrary.Tiles; namespace QwirkleClassLibrary.Boards; [DataContract] -public class Cell +public class Cell : INotifyPropertyChanged { + public event PropertyChangedEventHandler? PropertyChanged; + [DataMember] private readonly int x; - + [DataMember] private readonly int y; - + [DataMember] - private Tile? tile = null; + public Tile? Tile { get; private set;} /// /// This is the constructor for a Cell. @@ -58,17 +61,9 @@ public class Cell /// True if the cell is empty, false if the cell contains a tile. public bool IsFree { - get { return tile == null; } + get { return Tile == null; } } - /// - /// A getter for the tile in the cell. - /// - /// The tile if the cell isn't empty, null otherwise. - public Tile? GetTile - { - get { return tile; } - } /// /// A setter for the tile in the cell. @@ -77,9 +72,11 @@ public class Cell /// True if added succefully (if the cell didn't already contain a tile), false if there already was a tile in this cell. public bool SetTile(Tile addedTile) { - if (tile == null) + if (Tile == null) { - tile = addedTile; + + Tile = addedTile; + OnPropertyChanged(nameof(Tile)); return true; } else @@ -87,4 +84,9 @@ public class Cell return false; } } + + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index 0615ff5..02c974a 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -32,8 +32,6 @@ namespace QwirkleClassLibrary.Games private Board board = new Board(15, 12); public Board Board => board; - public ObservableCollection GetCellsInBoard => new ObservableCollection(board!.GetCells()); - public ReadOnlyCollection PlayerList => players.AsReadOnly(); private readonly List players = new(); @@ -411,18 +409,18 @@ namespace QwirkleClassLibrary.Games { var extendedCell = b.GetCell(x + i * dx, y + i * dy); - if (extendedCell?.GetTile == null) + if (extendedCell?.Tile == null) { break; } - if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape) + if (extendedCell.Tile.GetColor != tile.GetColor && extendedCell.Tile.GetShape != tile.GetShape) { OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Color / Shape does not match with the surrounding tiles !")); return false; } - if (extendedCell.GetTile.GetColor == tile.GetColor && extendedCell.GetTile.GetShape == tile.GetShape) + if (extendedCell.Tile.GetColor == tile.GetColor && extendedCell.Tile.GetShape == tile.GetShape) { OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Tile already placed on the same line / column !")); return false; @@ -496,7 +494,7 @@ namespace QwirkleClassLibrary.Games return true; } - if (b.GetCell(x, y)!.GetTile == null) + if (b.GetCell(x, y)!.Tile == null) { OnPlaceTile(new PlaceTileNotifiedEventArgs(t, ": Cell already used !")); } @@ -511,18 +509,18 @@ namespace QwirkleClassLibrary.Games foreach (var cell in surroundingCells) { - if (cell?.GetTile == null) + if (cell?.Tile == null) { continue; } - if (cell.GetTile.GetColor != t.GetColor && cell.GetTile.GetShape != t.GetShape) + if (cell.Tile.GetColor != t.GetColor && cell.Tile.GetShape != t.GetShape) { OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Colors / Shapes do not match with the surrounding tiles !")); return false; } - if (cell.GetTile.GetColor == t.GetColor && cell.GetTile.GetShape == t.GetShape) + if (cell.Tile.GetColor == t.GetColor && cell.Tile.GetShape == t.GetShape) { OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !")); @@ -545,7 +543,7 @@ namespace QwirkleClassLibrary.Games return false; } - if (surroundingCells.All(cell => cell?.GetTile == null)) + if (surroundingCells.All(cell => cell?.Tile == null)) { OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't place a tile that isn't adjacent to another one !")); return false; @@ -641,7 +639,7 @@ namespace QwirkleClassLibrary.Games foreach (var adjacentCell in surroundingCells) { - if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell) || checkedSurroundingCells.Contains(adjacentCell)) + if (adjacentCell?.Tile == null || cellsPlayed.Contains(adjacentCell) || checkedSurroundingCells.Contains(adjacentCell)) { continue; } @@ -675,7 +673,7 @@ namespace QwirkleClassLibrary.Games { var extendedCell = b.GetCell(cell.GetX + i * direction.Item1, cell.GetY + i * direction.Item2); - if (extendedCell?.GetTile == null || cellsPlayed.Contains(extendedCell)) + if (extendedCell?.Tile == null || cellsPlayed.Contains(extendedCell)) { break; } diff --git a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs index a67c81f..7e2f923 100644 --- a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs +++ b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs @@ -7,9 +7,10 @@ using System.Collections.ObjectModel; using System.Windows.Input; using System.ComponentModel; using Cell = QwirkleClassLibrary.Boards.Cell; + namespace Qwirkle.Pages; -public partial class Gameboard : ContentPage, INotifyPropertyChanged +public partial class Gameboard : ContentPage { public ICommand OnDrop => new Command(OnDropTile); @@ -17,31 +18,15 @@ public partial class Gameboard : ContentPage, INotifyPropertyChanged private Tile? tiledrag; - private ObservableCollection playerCells1; - public ObservableCollection PlayerCells1 - { - get => playerCells1; - set - { - playerCells1 = value; - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PlayerCells1")); - } - } public Gameboard() { InitializeComponent(); BindingContext = game; - PlayerCells1 = new ObservableCollection(game.PlayerList[0].Tiles.ToList()); - - DisplayAlert("List", playerCells1[0].ToString(), "chut"); } - - - private void OnDragOver(object sender, DragEventArgs e) { - foreach (var t in PlayerCells1) + foreach (var t in game.PlayerList[game.GetPlayingPlayerPosition()].Tiles) { if (e.Data.Text == t.ToString()) { @@ -62,14 +47,10 @@ public partial class Gameboard : ContentPage, INotifyPropertyChanged game.PlaceTile(game.GetPlayingPlayer(), tiledrag!, x, y); game.PlaceTileNotified -= Game_PlaceTileNotified; - PlayerCells1 = new ObservableCollection(game.PlayerList[0].Tiles.ToList()); - } private void Game_PlaceTileNotified(object? sender, PlaceTileNotifiedEventArgs args) { DisplayAlert("Tile place notified", args.Reason, "<3"); } - - public event PropertyChangedEventHandler? PropertyChanged; } \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/Pages/Gameboard.xaml b/Qwirkle/QwirkleViews/Pages/Gameboard.xaml index 218a1b5..057eaeb 100644 --- a/Qwirkle/QwirkleViews/Pages/Gameboard.xaml +++ b/Qwirkle/QwirkleViews/Pages/Gameboard.xaml @@ -9,7 +9,7 @@ - @@ -26,13 +26,13 @@ DropCommand="{Binding OnDrop, Source={x:Reference root}}" DropCommandParameter="{Binding .}" /> - - @@ -51,8 +51,34 @@ + + + + + + + + + + + + + + + + + + diff --git a/Qwirkle/TestBase/TestBoard.cs b/Qwirkle/TestBase/TestBoard.cs index d7e1c3a..cd48a03 100644 --- a/Qwirkle/TestBase/TestBoard.cs +++ b/Qwirkle/TestBase/TestBoard.cs @@ -66,25 +66,6 @@ public class TestBoard Assert.Equal((12 * 12), board.GetCells().Count); } - [Fact] - public void Test_GetReadCells() - { - Board board = new Board(12, 12); - - List cells = new List(); - - for (int a = 0; a < 12; a++) - { - for (int b = 0; b < 12; b++) - { - Cell localcell = new(a, b); - cells.Add(localcell); - } - } - ReadOnlyCollection readCells = cells.AsReadOnly(); - - Assert.Equal(readCells.Count, board.GetReadCells().Count); - } [Theory] [InlineData(true)]