Created
March 27, 2016 23:37
-
-
Save JeremyLWright/c887ee6c6e1113ef764f to your computer and use it in GitHub Desktop.
Cannot compile old 2.9.1 scala program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Jeremy on 3/27/2016. | |
*/ | |
class Enigma(val reflector: Reflector, val rotors: List[Rotor]) { | |
def window : String = rotors.map(_.showing).mkString | |
def findPositionOfChar(c:Char): Int = Alphabets.realAlphabet.indexOf(c) | |
def rotate(rotors: List[Rotor]): Unit = { | |
rotors.foldLeft(true)((b, r) => {val n = r.isAtNotch; if (b || n) r.rotate; n}) | |
} | |
def encryptChar(c: Char) = { | |
val index: Int = findPositionOfChar(c.toUpper) | |
if(index < 0) c | |
else { | |
rotate(rotors.reverse) | |
val i = (rotors.reverse ++ List[{def translate(i: Int): Int}](reflector) ++ rotors.map(_.mirror)).foldLeft(index)((i, p) => p.translate(i)) | |
Alphabets.realAlphabet.charAt(i) | |
} | |
} | |
def encode(s: String) : String = { | |
s.map(encryptChar(_)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment