示例#1
0
文件: Rotor.cs 项目: wcharczuk/enigma
        public void RotateToPosition(Char position)
        {
            if(!Char.IsLetter(position))
            {
                throw new EnigmaException("Invalid rotor position: {0}".Format(position));
            }

            var positionIndex = WireMatrix.ProjectCharacter(position);
            var offsetIndex = WireMatrix.ProjectCharacter(this.OffsetPosition);
            var delta = positionIndex - offsetIndex;
            if (delta < 0)
            {
                delta = 26 + delta;
            }

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

                offsetIndex = offsetIndex + 1;
                if (offsetIndex >= 26)
                {
                    offsetIndex = 0;
                }

                this.OffsetPosition = WireMatrix.ProjectIndex(offsetIndex);
            }
        }
示例#2
0
文件: Rotor.cs 项目: wcharczuk/enigma
 private void _initWires(IEnumerable<Char> wires)
 {
     this.WiresLeft = new WireMatrix(wires);
     this.WiresRight = this.WiresLeft.Invert();
 }
示例#3
0
文件: Rotor.cs 项目: wcharczuk/enigma
        public bool Rotate()
        {
            var shouldAdvance = this.OffsetPosition.Equals(this.RotateAt);
            if (this.RotateAtSecondary != null)
            {
                shouldAdvance = shouldAdvance || this.OffsetPosition.Equals(this.RotateAtSecondary.Value);
            }

            var offsetIndex = WireMatrix.ProjectCharacter(this.OffsetPosition);
            offsetIndex = offsetIndex + 1;
            if (offsetIndex >= 26)
            {
                offsetIndex = 0;
            }
            this.OffsetPosition = WireMatrix.ProjectIndex(offsetIndex);

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

            return shouldAdvance;
        }