From f8450183c15be1c3868e52d1cfd880b85fcdfeb2 Mon Sep 17 00:00:00 2001 From: s2224834 <46319860+s2224834@users.noreply.github.com> Date: Thu, 3 Jan 2019 17:51:20 +0000 Subject: [PATCH] Enigma: fix 4th rotor ringstellung --- src/core/operations/Enigma.mjs | 33 +++++++------- tests/operations/tests/Enigma.mjs | 73 +++++++++++++++++++------------ 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/src/core/operations/Enigma.mjs b/src/core/operations/Enigma.mjs index d43d780d..38456ebf 100644 --- a/src/core/operations/Enigma.mjs +++ b/src/core/operations/Enigma.mjs @@ -82,6 +82,11 @@ class EnigmaOp extends Operation { value: Enigma.ROTORS_OPTIONAL, defaultIndex: 10 }, + { + name: "4th rotor ring setting", + type: "option", + value: Enigma.LETTERS + }, { name: "4th rotor initial value", type: "option", @@ -130,25 +135,17 @@ class EnigmaOp extends Operation { * @returns {string} */ run(input, args) { - const [ - rotor1str, rotor1ring, rotor1pos, - rotor2str, rotor2ring, rotor2pos, - rotor3str, rotor3ring, rotor3pos, - rotor4str, rotor4pos, - reflectorstr, plugboardstr, - removeOther - ] = args; + const reflectorstr = args[12]; + const plugboardstr = args[13]; + const removeOther = args[14]; const rotors = []; - const [rotor1wiring, rotor1steps] = this.parseRotorStr(rotor1str, 1); - rotors.push(new Enigma.Rotor(rotor1wiring, rotor1steps, rotor1ring, rotor1pos)); - const [rotor2wiring, rotor2steps] = this.parseRotorStr(rotor2str, 2); - rotors.push(new Enigma.Rotor(rotor2wiring, rotor2steps, rotor2ring, rotor2pos)); - const [rotor3wiring, rotor3steps] = this.parseRotorStr(rotor3str, 3); - rotors.push(new Enigma.Rotor(rotor3wiring, rotor3steps, rotor3ring, rotor3pos)); - if (rotor4str !== "") { - // Fourth rotor doesn't have a ring setting - A is equivalent to no setting - const [rotor4wiring, rotor4steps] = this.parseRotorStr(rotor4str, 4); - rotors.push(new Enigma.Rotor(rotor4wiring, rotor4steps, "A", rotor4pos)); + for (let i=0; i<4; i++) { + if (i === 3 && args[i*3] === "") { + // No fourth rotor + break; + } + const [rotorwiring, rotorsteps] = this.parseRotorStr(args[i*3], 1); + rotors.push(new Enigma.Rotor(rotorwiring, rotorsteps, args[i*3 + 1], args[i*3 + 2])); } const reflector = new Enigma.Reflector(reflectorstr); const plugboard = new Enigma.Plugboard(plugboardstr); diff --git a/tests/operations/tests/Enigma.mjs b/tests/operations/tests/Enigma.mjs index a1acaf33..f8776b42 100644 --- a/tests/operations/tests/Enigma.mjs +++ b/tests/operations/tests/Enigma.mjs @@ -21,7 +21,7 @@ TestRegister.addTests([ "BDFHJLCPRTXVZNYEIWGAKMUSQO