/// <summary> /// Generate an array of connection from the mapping input. /// </summary> /// <param name="mapping">A rotor wiring such as "HQZGPJTMOBLNCIFDYAWVEUSRKX"</param> /// <returns>An array of connections</returns> private Connection[] GenerateConnectionsFromMapping(string mapping, bool ignoreInvalidMapping = false) { bool valid = AlphabetUtils.IsValidMapping(mapping); if (valid || ignoreInvalidMapping) { Connection[] result = new Connection[mapping.Length]; // Maps the regular alphabet order (ABCDEF...) to the input mapping. for (int i = 0; i < mapping.Length; i++) { result[i] = new Connection(AlphabetUtils.Alphabet[i], mapping[i]); } return result; } else { throw new InvalidMappingException(string.Format("{0} is not a valid mapping", mapping)); } }
/// <summary> /// Generates an rotor of non random connections where the nth letter of the alphabet is mapped to the 26-nth letter. /// Example : A -> Z, B -> Y, ..., Z -> A /// </summary> /// <param name="number">The number of connections. Generally 26 for the whole alphabet.</param> /// <returns>An array of connections.</returns> private Connection[] GenerateReverseAlphabeticalConnections(int number) { if (number < 1 || number > AlphabetUtils.Alphabet.Length) { throw new ArgumentOutOfRangeException(); } Connection[] result = new Connection[number]; for (int i = 0; i < number; i++) { result[i] = new Connection(AlphabetUtils.Alphabet[i], AlphabetUtils.Alphabet[number - 1 - i]); } return result; }
/// <summary> /// Offsets the rotor of x steps clockwise. That is, the rotor key is translated to an offset. /// In reality, makes the drum rotate of x notches, setting new paths for the current. /// </summary> public void OffsetRotor(int offset) { // Rebuilds the rotor Connection[] incrementedRotor = new Connection[length]; for (int i = 0; i < length; i++) { incrementedRotor[i] = initialMapping[(i + offset) % length]; } this.offset = offset; mapping = incrementedRotor; }