TileAnswerSelection.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Olympus
  4. {
  5. // Track physical tiles, so repeated letters can each be used once.
  6. public sealed class TileAnswerSelection
  7. {
  8. public const int MaxLength = 16;
  9. readonly char[] pool;
  10. readonly bool[] used;
  11. readonly List<int> order = new List<int>();
  12. public TileAnswerSelection(char[] candidates)
  13. {
  14. if (candidates == null) throw new ArgumentNullException(nameof(candidates));
  15. pool = (char[])candidates.Clone();
  16. used = new bool[pool.Length];
  17. }
  18. public int Count => order.Count;
  19. public string Value
  20. {
  21. get
  22. {
  23. var letters = new char[order.Count];
  24. for (int i = 0; i < letters.Length; i++) letters[i] = pool[order[i]];
  25. return new string(letters);
  26. }
  27. }
  28. public bool CanSelect(int index) => index >= 0 && index < pool.Length && !used[index] && Count < MaxLength;
  29. public bool TrySelect(int index)
  30. {
  31. if (!CanSelect(index)) return false;
  32. used[index] = true;
  33. order.Add(index);
  34. return true;
  35. }
  36. public bool RemoveLast()
  37. {
  38. if (Count == 0) return false;
  39. used[order[Count - 1]] = false;
  40. order.RemoveAt(Count - 1);
  41. return true;
  42. }
  43. }
  44. }