| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- 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<int> order = new List<int>();
- 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;
- }
- }
- }
|