Mercurial > public > cpp-enigma
comparison enigma/rotor.h @ 1:1459e74fda3f
Finished creating rotor class and factories.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 22 Jun 2012 20:15:11 -0500 |
parents | 74ebb2150658 |
children |
comparison
equal
deleted
inserted
replaced
0:74ebb2150658 | 1:1459e74fda3f |
---|---|
6 // | 6 // |
7 // rotor.h - This file contains the rotor class. | 7 // rotor.h - This file contains the rotor class. |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include "enigma_types.h" | 10 #include "enigma_types.h" |
11 #include "enigma_utils.h" | |
11 | 12 |
12 | 13 |
13 namespace enigma | 14 namespace enigma |
14 { | 15 { |
15 class rotor_error : public enigma_error | 16 class rotor_error : public enigma_error |
91 // alphabetic labels A-Z. In reality, the Heer & Luftwaffe devices used | 92 // alphabetic labels A-Z. In reality, the Heer & Luftwaffe devices used |
92 // numbers 01-26, and Kriegsmarine devices used A-Z. Our usage of A-Z is | 93 // numbers 01-26, and Kriegsmarine devices used A-Z. Our usage of A-Z is |
93 // simply for simulation convenience. | 94 // simply for simulation convenience. |
94 // display. | 95 // display. |
95 | 96 |
96 rotor(const char* name, const char* wiring, int ring_setting = 0, const char* stepping = 0); | 97 rotor(const char* name, const char* wiring, int ring_setting = 0, const char* stepping = nullptr); |
97 | 98 |
98 // Returns the rotor name: | 99 // Returns the rotor name: |
99 const std::string& name() const { return rotor_name; } | 100 const std::string& name() const { return rotor_name; } |
100 | 101 |
101 // Spin the rotor such that the string val appears in the operator window: | 102 // Spin the rotor such that the char val appears in the operator window: |
102 void set_display(const char* val); | 103 void set_display(char val) |
104 { | |
105 pos = display_map[val - 'A']; | |
106 display_val = val; | |
107 } | |
103 | 108 |
104 // Returns what is currently being displayed in the operator window: | 109 // Returns what is currently being displayed in the operator window: |
105 std::string get_display() const; | 110 char get_display() const { return display_val; } |
111 | |
112 | |
113 // sets the ring setting to n, where n [0-25]: | |
114 void set_ring_setting(int n); | |
115 | |
116 // get the current ring setting: | |
117 int get_ring_setting() const { return ring_setting; } | |
106 | 118 |
107 // Simulate a signal entering the rotor from the right at a given pin: | 119 // Simulate a signal entering the rotor from the right at a given pin: |
108 // n must be an integer between 0 and 25. | 120 // n must be an integer between 0 and 25. |
109 // Returns the contact number of the output signal (0-25). | 121 // Returns the contact number of the output signal (0-25). |
110 int signal_in(int n) const; | 122 int signal_in(int n) const |
123 { | |
124 // determine what pin we have at that position due to rotation | |
125 const int pin = (n + pos) % 26; | |
126 | |
127 // run it through the internal wiring | |
128 const int contact = entry_map[pin]; | |
129 | |
130 // turn back into a position due to rotation | |
131 return alpha_mod(contact - pos); | |
132 } | |
111 | 133 |
112 // Simulate a signal entering the rotor from the left at a given contact position n. | 134 // Simulate a signal entering the rotor from the left at a given contact position n. |
113 // n must be an integer between 0 and 25. | 135 // n must be an integer between 0 and 25. |
114 // Returns the pin number of the output signal (0-25). | 136 // Returns the pin number of the output signal (0-25). |
115 int signal_out(int n) const; | 137 int signal_out(int n) const |
138 { | |
139 // determine what contact we have at that position due to rotation | |
140 const int contact = (n + pos) % 26; | |
141 | |
142 // run it through the internal wiring | |
143 const int pin = exit_map[contact]; | |
144 | |
145 // turn back into a position due to rotation | |
146 return alpha_mod(pin - pos); | |
147 } | |
116 | 148 |
117 // Return true if this rotor has a notch in the stepping position and false otherwise: | 149 // Return true if this rotor has a notch in the stepping position and false otherwise: |
118 bool notch_over_pawl() const; | 150 bool notch_over_pawl() const |
151 { | |
152 return step_map[display_val - 'A']; | |
153 } | |
119 | 154 |
120 // Rotate the rotor forward one step: | 155 // Rotate the rotor forward one step: |
121 void rotate(); | 156 void rotate() |
157 { | |
158 pos = (pos + 1) % 26; | |
159 display_val = pos_map[pos]; | |
160 } | |
122 | 161 |
123 private: | 162 private: |
124 std::string rotor_name; | 163 std::string rotor_name; |
125 std::string wiring_str; | 164 std::string wiring_str; |
126 int ring_setting; | 165 int ring_setting; |
127 int pos; | 166 int pos; |
128 int rotations; | 167 char display_val; |
168 alpha_int_array entry_map; | |
169 alpha_int_array exit_map; | |
170 alpha_int_array display_map; | |
171 alpha_int_array pos_map; | |
172 alpha_bool_array step_map; | |
129 }; | 173 }; |
130 } | 174 } |
131 | 175 |
132 #endif | 176 #endif |