using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace QwirkleClassLibrary.Tiles { [DataContract] public class TileBag { [DataMember] private readonly List tiles = []; public ReadOnlyCollection? TilesBag { get; private set; } /// /// This is the constructor for the TileBag. It will create a tile of each of the possibilities among the Color and Shape Enums. /// /// This parameter is used to indicate the number of copies we want to create. /// 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()); } 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); } } } Init(); } /// /// This method is used to add a tile in the tile bag. /// /// The tile we want to add in the bag. public void AddTileInBag(Tile tile) { tiles.Add(tile); } /// /// Remove a tile in the tile bag. /// /// The tile you want to remove from the bag. /// True if the remove was successfull, false if it failed. public bool RemoveTileInBag(Tile tile) { for (int i = 0; i < tiles.Count; i++) { if (tiles[i] == tile) { tiles.RemoveAt(i); return true; } } return false; } [OnDeserialized] private void Init(StreamingContext sc = new()) { TilesBag = tiles.AsReadOnly(); } } }