diff --git a/Enigma.xcodeproj/project.pbxproj b/Enigma.xcodeproj/project.pbxproj index 346c2a7..ed054ba 100644 --- a/Enigma.xcodeproj/project.pbxproj +++ b/Enigma.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - C04D337F1B5B3F6100E2888E /* Enigma.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04D337E1B5B3F6100E2888E /* Enigma.swift */; }; + C04D337F1B5B3F6100E2888E /* Machine.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04D337E1B5B3F6100E2888E /* Machine.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 */; }; @@ -36,7 +36,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - C04D337E1B5B3F6100E2888E /* Enigma.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Enigma.swift; sourceTree = ""; }; + C04D337E1B5B3F6100E2888E /* Machine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Machine.swift; sourceTree = ""; }; 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 = ""; }; C0DA3A921B5AACB300D8D68E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -108,7 +108,7 @@ C0DA3A991B5AACB300D8D68E /* LaunchScreen.storyboard */, C0DA3A9C1B5AACB300D8D68E /* Info.plist */, C0DA3ABE1B5AB49200D8D68E /* Components.swift */, - C04D337E1B5B3F6100E2888E /* Enigma.swift */, + C04D337E1B5B3F6100E2888E /* Machine.swift */, ); path = Enigma; sourceTree = ""; @@ -261,7 +261,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - C04D337F1B5B3F6100E2888E /* Enigma.swift in Sources */, + C04D337F1B5B3F6100E2888E /* Machine.swift in Sources */, C0DA3ABF1B5AB49200D8D68E /* Components.swift in Sources */, C0DA3A931B5AACB300D8D68E /* ViewController.swift in Sources */, C0DA3A911B5AACB300D8D68E /* AppDelegate.swift in Sources */, diff --git a/Enigma/Machine.swift b/Enigma/Machine.swift new file mode 100644 index 0000000..9aad8b8 --- /dev/null +++ b/Enigma/Machine.swift @@ -0,0 +1,49 @@ +// +// Machine.swift +// Enigma +// +// Created by Eryn Wells on 7/18/15. +// Copyright © 2015 Eryn Wells. All rights reserved. +// + +import Foundation + + +class Machine { + 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() { + + } +} \ No newline at end of file