# HG changeset patch # User Brian Neal # Date 1341679351 18000 # Node ID 280facb82b8051e901309782367821a48b0165a7 # Parent 9e02d8696e67bd8e9643040a3ba67f99cb5d1040 Add the ability to change ring settings at the machine level. diff -r 9e02d8696e67 -r 280facb82b80 enigma/machine.h --- a/enigma/machine.h Wed Jul 04 19:52:41 2012 -0500 +++ b/enigma/machine.h Sat Jul 07 11:42:31 2012 -0500 @@ -6,10 +6,12 @@ // // machine.h - This file contains the main Enigma machine class. +#include #include #include #include #include +#include #include "enigma_types.h" #include "rotor.h" #include "plugboard.h" @@ -97,6 +99,66 @@ return result; } + // Returns the number of rotors in the machine (this count does not include + // the reflector). + std::size_t num_rotors() const + { + return rotors.size() - 1; + } + + // For changing the ring setting on a rotor inside the machine. + // Parameters: + // rotor - identifies the rotor to change the ring setting; must be + // in the range 0 - (num_rotors() - 1). 0 is the leftmost rotor. + // ring_setting - the ring setting value, 0-25 + // + void set_ring_setting(int rotor, int ring_setting) + { + rotors[rotor + 1].set_ring_setting(ring_setting); + } + + // For getting the ring setting on a rotor inside the machine. + // Parameters: + // rotor - identifies the rotor to change the ring setting; must be + // in the range 0 - (num_rotors() - 1). 0 is the leftmost rotor. + // + int get_ring_setting(int rotor) const + { + return rotors[rotor + 1].get_ring_setting(); + } + + // For changing the ring settings on all rotors inside the machine. + // Parameters: + // settings - a vector of ring settings, 0-25. The size of this + // vector must match num_rotors(). + // + void set_ring_settings(const std::vector& settings) + { + if (settings.size() == num_rotors()) + { + rotor* r = &rotors[1]; // skip the reflector; + for (auto s : settings) + { + r->set_ring_setting(s); + ++r; + } + } + else + { + throw enigma_machine_error("set_ring_settings rotor/settings size mismatch"); + } + } + + // For getting the ring settings as a vector of integers. Element 0 corresponds + // to the leftmost rotor. + std::vector get_ring_settings() const + { + std::vector result(num_rotors()); + std::transform(rotors.begin() + 1, rotors.end(), result.begin(), + [](const rotor& r) { return r.get_ring_setting(); }); + return result; + } + // simulate front panel key press; returns the lamp character that is lit char key_press(char c) { @@ -120,6 +182,7 @@ } } + // Process a buffer of text from a string, returning the result as a string. std::string process_text(const std::string& input) { std::string result; diff -r 9e02d8696e67 -r 280facb82b80 enigma/tests/test_machine.t.h --- a/enigma/tests/test_machine.t.h Wed Jul 04 19:52:41 2012 -0500 +++ b/enigma/tests/test_machine.t.h Sat Jul 07 11:42:31 2012 -0500 @@ -253,3 +253,62 @@ } }; + +class ring_settings_test_suite : public CxxTest::TestSuite +{ +public: + + void test_ring_settings() + { + enigma_machine machine({"Beta", "II", "IV", "I"}, {0, 2, 8, 21}, "B-Thin"); + + TS_ASSERT_EQUALS(machine.get_ring_setting(0), 0); + TS_ASSERT_EQUALS(machine.get_ring_setting(1), 2); + TS_ASSERT_EQUALS(machine.get_ring_setting(2), 8); + TS_ASSERT_EQUALS(machine.get_ring_setting(3), 21); + + std::vector rings(machine.get_ring_settings()); + std::vector expected{ 0, 2, 8, 21 }; + TS_ASSERT_EQUALS(rings, expected); + + machine.set_ring_setting(0, 25); + TS_ASSERT_EQUALS(machine.get_ring_setting(0), 25); + TS_ASSERT_EQUALS(machine.get_ring_setting(1), 2); + TS_ASSERT_EQUALS(machine.get_ring_setting(2), 8); + TS_ASSERT_EQUALS(machine.get_ring_setting(3), 21); + + expected = { 25, 2, 8, 21 }; + TS_ASSERT_EQUALS(machine.get_ring_settings(), expected); + + machine.set_ring_setting(1, 18); + TS_ASSERT_EQUALS(machine.get_ring_setting(0), 25); + TS_ASSERT_EQUALS(machine.get_ring_setting(1), 18); + TS_ASSERT_EQUALS(machine.get_ring_setting(2), 8); + TS_ASSERT_EQUALS(machine.get_ring_setting(3), 21); + + expected = { 25, 18, 8, 21 }; + TS_ASSERT_EQUALS(machine.get_ring_settings(), expected); + + machine.set_ring_setting(2, 11); + TS_ASSERT_EQUALS(machine.get_ring_setting(0), 25); + TS_ASSERT_EQUALS(machine.get_ring_setting(1), 18); + TS_ASSERT_EQUALS(machine.get_ring_setting(2), 11); + TS_ASSERT_EQUALS(machine.get_ring_setting(3), 21); + + expected = { 25, 18, 11, 21 }; + TS_ASSERT_EQUALS(machine.get_ring_settings(), expected); + + machine.set_ring_setting(3, 3); + TS_ASSERT_EQUALS(machine.get_ring_setting(0), 25); + TS_ASSERT_EQUALS(machine.get_ring_setting(1), 18); + TS_ASSERT_EQUALS(machine.get_ring_setting(2), 11); + TS_ASSERT_EQUALS(machine.get_ring_setting(3), 3); + + expected = { 25, 18, 11, 3 }; + TS_ASSERT_EQUALS(machine.get_ring_settings(), expected); + + expected = { 8, 9, 10, 11 }; + machine.set_ring_settings(expected); + TS_ASSERT_EQUALS(machine.get_ring_settings(), expected); + } +};