/// <summary> /// Initializes the form /// </summary> public EnigmaMachineUI() { InitializeComponent(); this.KeyPreview = true; // makes the form the starting point for key presses // sets the sizes this.MaximumSize = this.Size; this.MinimumSize = this.Size; this.MaximizeBox = false; this.MinimizeBox = false; // sets the Rotor defaults Rotors.SetRotorDefaults = FileManagement.GetInformationFromFile(FileNames.Rotors); // initializes three rotors Rotors[] rotors = new Rotors[3]; // runs through each control in the rotor panel for (int i = 0; i < pnRotorNumbers.Controls.Count; i++) { // attempts to convert the control to a combobox ComboBox cb = pnRotorNumbers.Controls[i] as ComboBox; // if successful if (cb != null) { // sets the rotor information rotors[i] = new Rotors(i); RotorLoad(cb, rotors[i]); cb.SelectedIndex = i; } } // loads the reflector combo box ReflectorLoad(); // initializes a new machine machine = new Machine(rotors, Reflector.GetReflectorString(cbReflector.SelectedIndex), ""); // adds the labels to the three letter panels AddLabels(pnLamps); AddLabels(pnKeys); AddLabels(pnPlugboard); // counts the number of rotors on the form and sets the offset controls int num = pnRotorNumbers.Controls.Count; for (int i = 0; i < num; i++) { OffsetLoad(i); } // initializes the colours used array colourUsed = new bool[colours.Length]; // sets the plugboard header to the default foreground colour lbPlugboardHeader.ForeColor = DEFAULT_LETTER_FG_COLOUR; // centers the clear button btnClearPlugboard.Location = new Point(this.Width / 2 - btnClearPlugboard.Width / 2, 595); }
/// <summary> /// Encrypts the letter /// </summary> /// <param name="c">the character to encrypt</param> public void EncryptNewValue(char c) { // sends the letter through the machine to encrypt char encrypted = machine.DetermineEncryptedLetter(c); // the string to append string encryptedString = encrypted.ToString(); // determines if a space is needed CheckOutputMultiple(ref encryptedString); // if the output textbox is 0, clear the output file if (tbOutput.Text.Length <= 0) { FileManagement.ClearOutput(); } // appends the string to the output textbox and output file tbOutput.Text += encryptedString; FileManagement.AppendToOutput(encryptedString); // lights up the lamp for the encrypted letter foreach (Label l in pnLamps.Controls) { if (l.Text == encrypted.ToString()) { l.BackColor = Color.Yellow; l.ForeColor = Color.Black; break; } } RotateControls(2); // rotates the center rotor if required if (machine.CheckIfRotate(2)) { RotateControls(1); // rotates the left rotor if required if (machine.CheckIfRotate(1)) { RotateControls(0); } } }