Esempio n. 1
0
        ///<inheritdoc/>
        public override string Run(string input, bool forward)
        {
            var output = new StringBuilder();

            int indexer = 0;

            foreach (var c in input)
            {
                if (indexer == Key.Length)
                {
                    indexer = 0;
                }
                var steps = Alphabet.IndexOf(Key[indexer]) * (forward ? 1 : -1);

                output.Append(Alphabet.WrapChar(c, steps));
                indexer++;
            }

            return(output.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// A special run for the Enigma. Simulates a key press and returns a result.
        /// </summary>
        /// <param name="input">The character that has been depressed</param>
        /// <returns>The output, i.e the character that will be lit in the lamp</returns>
        public virtual char Run(char input)
        {
            //Rotate the FastRotor
            Rotors[0].Rotate();

            //Get the index of the letter in the alphabet:
            int index = Alphabet.IndexOf(input);

            //Pass the current to the Stator:
            index = Stator.GetPath(index, true);

            //Pass the current through successive rotors:
            for (int i = 0; i < Rotors.Count; i++)
            {
                index = Rotors[i].GetPath(index, true);
            }

            //Pass the current the Reflector
            index = Reflector.GetPath(index, true);

            //Pass through the rotors in reverse:
            for (int i = Rotors.Count - 1; i > -1; i--)
            {
                var rotor = Rotors[i];
                index = rotor.GetPath(index, false);
            }

            //Pass the current through the Stator:
            index = Stator.GetPath(index, false);

            var result = Alphabet[index];

            //perform plugboard simulation:
            if (Plugboard != null)
            {
                result = Plugboard.Simulate(result);
            }
            return(result);
        }