Exemplo n.º 1
0
        public static Settings Random(MachineType m)
        {
            Machine mach = new Machine(m);

            Settings result = new Settings();

            result.MachineType   = m;
            result.ReflectorType = mach.AvailableReflectors[RandomUtil._rand.Next(mach.AvailableReflectors.Count)].ReflectorType;

            List <RotorName> availableNames = mach.AvailableRotors.Select(r => r.RotorName).ToList();

            if (m == MachineType.M4K)
            {
                availableNames.Remove(RotorName.Beta);
                availableNames.Remove(RotorName.Gamma);

                if (RandomUtil._rand.Next(2) == 1)
                {
                    result.Rotors.Add(new RotorSetting {
                        Name = RotorName.Beta, RingSetting = RandomUtil._rand.Next(Constants.ALPHABET.Length)
                    });
                }
                else
                {
                    result.Rotors.Add(new RotorSetting {
                        Name = RotorName.Gamma, RingSetting = RandomUtil._rand.Next(Constants.ALPHABET.Length)
                    });
                }
            }

            for (int i = 0; i < 3; i++)
            {
                RotorName name = availableNames[RandomUtil._rand.Next(availableNames.Count)];
                result.Rotors.Add(new RotorSetting {
                    Name = name, RingSetting = RandomUtil._rand.Next(Constants.ALPHABET.Length)
                });
                availableNames.Remove(name);
            }

            List <Tuple <string, double> > letters = new List <Tuple <string, double> >();

            foreach (char c in Constants.ALPHABET)
            {
                letters.Add(new Tuple <string, double>(c.ToString(), RandomUtil._rand.NextDouble()));
            }
            letters.Sort((a, b) => a.Item2.CompareTo(b.Item2));

            for (int i = 0; i < 10; i++)
            {
                var f = letters.First();
                var l = letters.Last();

                string a = f.Item1;
                string b = l.Item1;

                if (a.CompareTo(b) > 0)
                {
                    result.Plugs.Add(new PlugSetting {
                        LetterA = b, LetterB = a
                    });
                }
                else
                {
                    result.Plugs.Add(new PlugSetting {
                        LetterA = a, LetterB = b
                    });
                }

                letters.Remove(f);
                letters.Remove(l);
            }

            result.Plugs.Sort((a, b) => a.ToString().CompareTo(b.ToString()));

            if (result.MachineType != MachineType.M3)
            {
                result.Grund = RandomUtil.GenerateSequence(4, Constants.ALPHABET, false, false);

                if (result.MachineType == MachineType.M3K)
                {
                    result.Grund = result.Grund.Substring(0, 3);
                }

                result.Kenngruppen.Clear();
            }
            else
            {
                result.Grund = string.Empty;
                result.Kenngruppen.Clear();

                for (int i = 0; i < 4; i++)
                {
                    result.Kenngruppen.Add(RandomUtil.GenerateSequence(3, Constants.ALPHABET, false, false));
                }
            }

            return(result);
        }