comparison enigma/machine.h @ 16:280facb82b80

Add the ability to change ring settings at the machine level.
author Brian Neal <bgneal@gmail.com>
date Sat, 07 Jul 2012 11:42:31 -0500
parents 9e02d8696e67
children b04dea5f71a3
comparison
equal deleted inserted replaced
15:9e02d8696e67 16:280facb82b80
4 // This file is part of Cpp-Enigma, the Enigma Machine simulation. 4 // This file is part of Cpp-Enigma, the Enigma Machine simulation.
5 // Cpp-Enigma is released under the MIT License (see License.txt). 5 // Cpp-Enigma is released under the MIT License (see License.txt).
6 // 6 //
7 // machine.h - This file contains the main Enigma machine class. 7 // machine.h - This file contains the main Enigma machine class.
8 8
9 #include <algorithm>
9 #include <memory> 10 #include <memory>
10 #include <string> 11 #include <string>
11 #include <vector> 12 #include <vector>
12 #include <cassert> 13 #include <cassert>
14 #include <cstddef>
13 #include "enigma_types.h" 15 #include "enigma_types.h"
14 #include "rotor.h" 16 #include "rotor.h"
15 #include "plugboard.h" 17 #include "plugboard.h"
16 18
17 namespace enigma 19 namespace enigma
95 result += rotors[i].get_display(); 97 result += rotors[i].get_display();
96 } 98 }
97 return result; 99 return result;
98 } 100 }
99 101
102 // Returns the number of rotors in the machine (this count does not include
103 // the reflector).
104 std::size_t num_rotors() const
105 {
106 return rotors.size() - 1;
107 }
108
109 // For changing the ring setting on a rotor inside the machine.
110 // Parameters:
111 // rotor - identifies the rotor to change the ring setting; must be
112 // in the range 0 - (num_rotors() - 1). 0 is the leftmost rotor.
113 // ring_setting - the ring setting value, 0-25
114 //
115 void set_ring_setting(int rotor, int ring_setting)
116 {
117 rotors[rotor + 1].set_ring_setting(ring_setting);
118 }
119
120 // For getting the ring setting on a rotor inside the machine.
121 // Parameters:
122 // rotor - identifies the rotor to change the ring setting; must be
123 // in the range 0 - (num_rotors() - 1). 0 is the leftmost rotor.
124 //
125 int get_ring_setting(int rotor) const
126 {
127 return rotors[rotor + 1].get_ring_setting();
128 }
129
130 // For changing the ring settings on all rotors inside the machine.
131 // Parameters:
132 // settings - a vector of ring settings, 0-25. The size of this
133 // vector must match num_rotors().
134 //
135 void set_ring_settings(const std::vector<int>& settings)
136 {
137 if (settings.size() == num_rotors())
138 {
139 rotor* r = &rotors[1]; // skip the reflector;
140 for (auto s : settings)
141 {
142 r->set_ring_setting(s);
143 ++r;
144 }
145 }
146 else
147 {
148 throw enigma_machine_error("set_ring_settings rotor/settings size mismatch");
149 }
150 }
151
152 // For getting the ring settings as a vector of integers. Element 0 corresponds
153 // to the leftmost rotor.
154 std::vector<int> get_ring_settings() const
155 {
156 std::vector<int> result(num_rotors());
157 std::transform(rotors.begin() + 1, rotors.end(), result.begin(),
158 [](const rotor& r) { return r.get_ring_setting(); });
159 return result;
160 }
161
100 // simulate front panel key press; returns the lamp character that is lit 162 // simulate front panel key press; returns the lamp character that is lit
101 char key_press(char c) 163 char key_press(char c)
102 { 164 {
103 step_rotors(); 165 step_rotors();
104 return electric_signal(c - 'A') + 'A'; 166 return electric_signal(c - 'A') + 'A';
118 { 180 {
119 *output++ = key_press(*input++); 181 *output++ = key_press(*input++);
120 } 182 }
121 } 183 }
122 184
185 // Process a buffer of text from a string, returning the result as a string.
123 std::string process_text(const std::string& input) 186 std::string process_text(const std::string& input)
124 { 187 {
125 std::string result; 188 std::string result;
126 result.reserve(input.size()); 189 result.reserve(input.size());
127 190