Example #1
0
        private void input_TextChanged(object sender, EventArgs e)
        {
            if (enigma == null)
            {
                try
                {
                    SetNewSettings();
                    enigma = new Enigma(wheels, reflector, plugboardSets);
                    enigma.SetWheelPositions(new char[] { rotorLab1.Text[0], rotorLab2.Text[0], rotorLab3.Text[0] });
                    SetUIToCurrentSets();
                    currentInputLength = 0;
                }
                catch (NullReferenceException)
                {
                    MessageBox.Show("Select all settings");
                    return;
                }
            }

            if (input.Text == "")
            {
                SetUIToCurrentSets();
                output.Text = "";
                enigma.SetWheelPositions(loadedCharPositions);
                return;
            }

            if (input.Text.Length == currentInputLength)
            {
                return;
            }

            if (input.Text.Length == currentInputLength - 1)
            {
                output.Text = output.Text.Substring(0, output.Text.Length - 1);
                enigma.RightWheel.RotateBack();
                SetUIToCurrentSets();
                return;
            }

            if (input.Text.Length < currentInputLength - 1 || input.Text.Length > currentInputLength + 1)
            {
                MessageBox.Show("That's an inappropriate move! You can only enter chars one by one. " +
                                "\r\n Process has been reseted.");
                input.Text  = "";
                output.Text = "";
                enigma.SetWheelPositions(loadedCharPositions);
                SetUIToCurrentSets();
                return;
            }

            if (!Array.TrueForAll(input.Text.ToCharArray(), s => IsLatin(s)))
            {
                MessageBox.Show("Only latin letters are allowed!");
                input.Text = input.Text.Remove(input.Text.Length - 1);
                return;
            }
            else
            {
                if (!CheckSettings())
                {
                    SetNewSettings();
                    enigma      = new Enigma(wheels, reflector, plugboardSets);
                    input.Text  = "";
                    output.Text = "";
                    SetUIToCurrentSets();
                    MessageBox.Show("New settings have appeared. Process has been restarted.");
                    return;
                }
            }
            output.Text += enigma.RotateAndEncode(input.Text.ToUpper()[input.Text.Length - 1]);
            SetUIToCurrentSets();
        }