-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnigma.cpp
139 lines (103 loc) · 3.39 KB
/
Enigma.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
-----------------------------------------------------------------------------------
Laboratoire : 02
Fichier : Enigma.cpp
Auteur(s) : Doran Kayoumi, Jérémie Melly, Pierre-Olivier Sandoz
Date : 07.03.2019
But : Implémentation de la classe Enigma
Remarque(s) : -
Compilateur : MinGW-g++ 6.3.0
-----------------------------------------------------------------------------------
*/
#include "Enigma.h"
using namespace std;
/**
* @brief Display the results of the conversion of a char
*
* @param first char to display
* @param second char to display
* @param direction in which the conversion is done
*/
void displayResult(const char &first, const char &second, bool direction);
Enigma::Enigma(const Rotor &LEFT_ROTOR, const Rotor &MIDDLE_ROTOR, const Rotor &RIGHT_ROTOR,
const Reflector &REFLECTOR, bool debug) : debug(debug), reflector(REFLECTOR) {
this->rotors.push_back(RIGHT_ROTOR);
this->rotors.push_back(MIDDLE_ROTOR);
this->rotors.push_back(LEFT_ROTOR);
}
void Enigma::toggleDebug() {
this->debug = !this->debug;
}
void Enigma::changeRotor(const RotorPosition &ROTOR_TO_CHANGE, const Rotor &NEW_ROTOR) {
this->rotors.at((size_t) ROTOR_TO_CHANGE) = NEW_ROTOR;
}
void Enigma::changeReflector(const Reflector &NEW_REFLECTOR) {
this->reflector = NEW_REFLECTOR;
}
void Enigma::changeRotorPosition(const RotorPosition &ROTOR, char position) {
this->rotors.at((size_t) ROTOR).setPosition(position);
}
void Enigma::turnRotors() {
if (this->rotors.at(0).reachedNotch()) {
if (this->rotors.at(1).reachedNotch()) {
this->rotors.at(2).turn();
}
this->rotors.at(1).turn();
}
this->rotors.at(0).turn();
}
char Enigma::convert(char toConvert) {
char current;
char converted = toupper(toConvert);
this->turnRotors();
for (size_t i = 0; i < this->rotors.size(); ++i) {
current = converted;
converted = this->rotors.at(i).convert(converted);
if (this->debug) {
cout << this->rotors.at(i);
displayResult(converted, current, false);
}
}
current = converted;
converted = this->reflector.getCharReflect(converted);
if (this->debug) {
cout << this->reflector;
displayResult(converted, current, true);
}
for (size_t i = this->rotors.size(); i > 0; --i) {
current = converted;
converted = this->rotors.at(i - 1).decode(converted);
if (this->debug) {
cout << this->rotors.at(i - 1);
displayResult(current, converted, true);
}
}
return converted;
}
string Enigma::convert(const std::string &toConvert) {
if (this->debug) {
cout << *this << endl;
}
string result;
for (char c : toConvert) {
result += this->convert(c);
}
return result;
}
ostream &operator<<(std::ostream &os, const Enigma &ENIGMA) {
string position[3];
position[0] = "RIGHT";
position[1] = "MIDDLE";
position[2] = "LEFT";
for (size_t i = 0; i < 3; i++) {
os << position[i] << " rotor" << endl
<< ENIGMA.rotors.at(i) << endl
<< endl;
}
cout << "Reflector" << endl;
os << ENIGMA.reflector;
return os;
}
void displayResult(const char &first, const char &second, bool direction) {
cout << "result : " << first << (direction ? "=>" : "<=") << second << endl << endl;
}