-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnigma.cpp
More file actions
65 lines (53 loc) · 1.22 KB
/
Enigma.cpp
File metadata and controls
65 lines (53 loc) · 1.22 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
54
55
56
57
58
59
60
61
62
63
64
#include "Enigma.h"
#include <iostream>
Enigma::Enigma(){
plugboard = new Plugboard();
reflector = new Reflector();
}
Enigma::~Enigma(){
delete plugboard;
delete reflector;
m_rotors.~vector();
}
void Enigma::addRotor(std::string setup){
Rotor* newRotor = new Rotor(setup);
if(newRotor != NULL){
m_rotors.push_back(newRotor);
} else std::cout<<"Could not add Rotor"<<std::endl;
}
void Enigma::configurePlugboard(std::string setup){
plugboard->configure(setup);
}
void Enigma::encrypt(int* pChr){
//plugboard in
plugboard->mapChar(pChr);
//rotors in
Enigma::mapIn(pChr);
//reflect character
reflector->reflect(pChr);
//rotors out
Enigma::mapOut(pChr);
//plugboard out
plugboard->mapChar(pChr);
//print character to output board
char result = 'A' + *pChr;
std::cout<<result;
/*rotate the appropriate rotors.
* rotate() returns true when a full rotation is made,
*/
int i=0;
while(i<m_rotors.size() && m_rotors[i]->rotate()){
i++;
}
}
void Enigma::mapIn(int* chr){
for(int i=0; i<m_rotors.size(); i++){
m_rotors[i]->mapIn(chr);
}
}
void Enigma::mapOut(int* chr){
std::vector<Rotor*>::reverse_iterator ritr;
for(ritr = m_rotors.rbegin(); ritr < m_rotors.rend(); ++ritr){
(*ritr)->mapOut(chr);
}
}