using System; using System.Collections.Generic; namespace Olympus { // Track physical tiles, so repeated letters can each be used once. public sealed class TileAnswerSelection { public const int MaxLength = 16; readonly char[] pool; readonly bool[] used; readonly List order = new List(); public TileAnswerSelection(char[] candidates) { if (candidates == null) throw new ArgumentNullException(nameof(candidates)); pool = (char[])candidates.Clone(); used = new bool[pool.Length]; } public int Count => order.Count; public string Value { get { var letters = new char[order.Count]; for (int i = 0; i < letters.Length; i++) letters[i] = pool[order[i]]; return new string(letters); } } public bool CanSelect(int index) => index >= 0 && index < pool.Length && !used[index] && Count < MaxLength; public bool TrySelect(int index) { if (!CanSelect(index)) return false; used[index] = true; order.Add(index); return true; } public bool RemoveLast() { if (Count == 0) return false; used[order[Count - 1]] = false; order.RemoveAt(Count - 1); return true; } } }