Ejemplo n.º 1
0
        /// <summary>
        /// Rotate the rotor to the next position.
        /// </summary>
        /// <returns>true if the next should be rotate</returns>
        public bool Rotate()
        {
            bool shouldAdvance = false;

            foreach (char item in RotateAt)
            {
                if (OffsetPosition.Equals(item))
                {
                    shouldAdvance = true;
                    break;
                }
            }

            int offsetIndex = WiresLeft.ProjectCharacter(OffsetPosition);

            offsetIndex = offsetIndex + 1;
            if (offsetIndex >= OperatingAlphabet.Count)
            {
                offsetIndex = 0;
            }
            OffsetPosition = WiresLeft.ProjectIndex(offsetIndex);

            WiresLeft.Rotate();
            WiresRight = WiresLeft.Invert();

            return(shouldAdvance);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Rotate the rotor to the specified position.
        /// </summary>
        /// <returns></returns>
        public void RotateToPosition(char position)
        {
            if (!AlphabetContains(position))
            {
                throw new ArgumentException("Invalid rotor position: {0}".Format(position), nameof(position));
            }

            int positionIndex = WiresLeft.ProjectCharacter(position);
            int offsetIndex   = WiresLeft.ProjectCharacter(OffsetPosition);
            int delta         = positionIndex - offsetIndex;

            if (delta < 0)
            {
                delta = OperatingAlphabet.Count + delta;
            }

            for (int currentIndex = 0; currentIndex < delta; currentIndex++)
            {
                WiresLeft.Rotate();
                WiresRight = WiresLeft.Invert();

                offsetIndex = offsetIndex + 1;
                if (offsetIndex >= OperatingAlphabet.Count)
                {
                    offsetIndex = 0;
                }

                OffsetPosition = WiresLeft.ProjectIndex(offsetIndex);
            }
        }