示例#1
0
        public PegPattern GetRandomPattern(IPegCollectionOptions options)
        {
            var sourcePegs  = GetPegs();
            var patternPegs = new List <int>();
            var random      = new Random();

            for (int i = 0; i < options.Size; i++)
            {
                var index = random.Next(sourcePegs.Count);
                var peg   = sourcePegs[index];
                if (!options.AllowDuplicates)
                {
                    sourcePegs.RemoveAt(index);
                }
                patternPegs.Add(peg);
            }
            return(new PegPattern(this, patternPegs));
        }
示例#2
0
        public LinkedList <PegPattern> GetAllPatterns(IPegCollectionOptions options)
        {
            if (options.Size < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(options.Size));
            }

            var palette      = new Palette(PegCount);
            var paletteSet   = ToSet();
            var patternQuery = paletteSet.Select(x => new[] { x });

            for (int i = 1; i < options.Size; i++)
            {
                patternQuery = patternQuery.SelectMany(_ => paletteSet, (pq, p) => pq.Append(p).ToArray());
            }
            return(new LinkedList <PegPattern>(patternQuery
                                               .Where(x => options.AllowDuplicates || x.Length == x.Distinct().Count())
                                               .Select(x => new PegPattern(palette, x))
                                               .ToList()));
        }
示例#3
0
        public static PegPattern PromptPegPattern(string question, IPegCollectionOptions options)
        {
            var pegs = Shell.Prompt(question, null, null, line =>
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    return(null);
                }
                return(Regex
                       .Split(line, @"\s+")
                       .Where(x => !string.IsNullOrWhiteSpace(x))
                       .Select(x => int.Parse(x))
                       .ToArray());
            }, value => value != null &&
                                    value.Length == options.Size &&
                                    value.All(options.Palette.Contains) &&
                                    (options.AllowDuplicates || value.Length == value.Distinct().Count())
                                    );

            return(new PegPattern(options.Palette, pegs));
        }