Rename Enigma.swift -> Machine.swift
This commit is contained in:
		
							parent
							
								
									bb1ba6a1fc
								
							
						
					
					
						commit
						6a2e4fd44e
					
				
					 2 changed files with 53 additions and 4 deletions
				
			
		
							
								
								
									
										49
									
								
								Enigma/Machine.swift
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								Enigma/Machine.swift
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue