Add Enigma.swift

This commit is contained in:
Eryn Wells 2015-07-18 22:23:29 -07:00
parent ed0b46b601
commit cb437b7d96
2 changed files with 56 additions and 0 deletions

View file

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
C04D337F1B5B3F6100E2888E /* Enigma.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04D337E1B5B3F6100E2888E /* Enigma.swift */; };
C0DA3A911B5AACB300D8D68E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DA3A901B5AACB300D8D68E /* AppDelegate.swift */; };
C0DA3A931B5AACB300D8D68E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DA3A921B5AACB300D8D68E /* ViewController.swift */; };
C0DA3A961B5AACB300D8D68E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C0DA3A941B5AACB300D8D68E /* Main.storyboard */; };
@ -35,6 +36,7 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
C04D337E1B5B3F6100E2888E /* Enigma.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Enigma.swift; sourceTree = "<group>"; };
C0DA3A8D1B5AACB300D8D68E /* Enigma.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Enigma.app; sourceTree = BUILT_PRODUCTS_DIR; };
C0DA3A901B5AACB300D8D68E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
C0DA3A921B5AACB300D8D68E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@ -106,6 +108,7 @@
C0DA3A991B5AACB300D8D68E /* LaunchScreen.storyboard */,
C0DA3A9C1B5AACB300D8D68E /* Info.plist */,
C0DA3ABE1B5AB49200D8D68E /* Rotor.swift */,
C04D337E1B5B3F6100E2888E /* Enigma.swift */,
);
path = Enigma;
sourceTree = "<group>";
@ -258,6 +261,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C04D337F1B5B3F6100E2888E /* Enigma.swift in Sources */,
C0DA3ABF1B5AB49200D8D68E /* Rotor.swift in Sources */,
C0DA3A931B5AACB300D8D68E /* ViewController.swift in Sources */,
C0DA3A911B5AACB300D8D68E /* AppDelegate.swift in Sources */,
@ -487,6 +491,7 @@
C0DA3AB71B5AACB300D8D68E /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
C0DA3AB81B5AACB300D8D68E /* Build configuration list for PBXNativeTarget "EnigmaTests" */ = {
isa = XCConfigurationList;
@ -495,6 +500,7 @@
C0DA3ABA1B5AACB300D8D68E /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
C0DA3ABB1B5AACB300D8D68E /* Build configuration list for PBXNativeTarget "EnigmaUITests" */ = {
isa = XCConfigurationList;
@ -503,6 +509,7 @@
C0DA3ABD1B5AACB300D8D68E /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};

49
Enigma/Enigma.swift Normal file
View file

@ -0,0 +1,49 @@
//
// Enigma.swift
// Enigma
//
// Created by Eryn Wells on 7/18/15.
// Copyright © 2015 Eryn Wells. All rights reserved.
//
import Foundation
class Enigma {
let rotors: [Rotor]
let reflector: Reflector
let plugboard: Plugboard
init(rotors: [Rotor], reflector: Reflector, plugboard: Plugboard) {
self.rotors = rotors
self.reflector = reflector
self.plugboard = plugboard
}
func encode(c: Character) throws -> Character {
var output = c
output = try plugboard.encode(output)
for rotor in rotors {
output = try rotor.encode(output)
}
output = try reflector.encode(output)
for rotor in rotors.reverse() {
output = try rotor.encode(output)
}
output = try plugboard.encode(output)
advanceRotors()
return output
}
func encode(string: String) throws -> String {
var output = ""
for character in string.characters {
output += String(try encode(character))
}
return output
}
private func advanceRotors() {
}
}