-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotorComplex.cpp
More file actions
53 lines (48 loc) · 1.62 KB
/
RotorComplex.cpp
File metadata and controls
53 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "RotorComplex.h"
#include "data.h"
#include <string>
#include <list>
#include <stdexcept>
#include <iostream>
RotorComplex::RotorComplex(const std::list<Rotor>& rotorList, const std::string ringSettings,
const std::string startPositions, const Scrambler* entry_, const Scrambler* reflector_)
: reflector(reflector_->clone()), entry(entry_->clone()) {
int i = 0;
if(ringSettings.size() != rotorList.size() || startPositions.size() != rotorList.size()){
throw std::runtime_error("invalid ring-settings or startpositions");
}
for(auto& rotor : rotorList) {
rotors.push_back(Rotor(rotor));
++i;
}
}
void RotorComplex::setSettings(const std::string positions, const std::string ringSettings) {
int i = 0;
if(ringSettings.size() != rotors.size() || positions.size() != rotors.size()){
throw std::runtime_error("invalid ring-settings or startpositions");
}
for(auto& rotor : rotors) {
rotor.setSettings(ringSettings[i], positions[i]);
++i;
}
}
char RotorComplex::scramble(const char input) {
char output = entry->scramble(input);
auto it = rotors.rbegin();
bool turn = it->checkOnTurnover();
it->rotate(); //right rotor always rotates
++it;
while(turn && it != rotors.rend()) {
turn = it->checkOnTurnover();
it->rotate();
++it;
}
for(auto it = rotors.rbegin(); it != rotors.rend(); ++it) {
output = it->scramble(output);
}
output = reflector->scramble(output);
for(auto& rotor : rotors) {
output = rotor.scramble(output);
}
return entry->scramble(output);
}