Skip to content

Instantly share code, notes, and snippets.

@JeremyLWright
Created March 27, 2016 23:37
Show Gist options
  • Save JeremyLWright/c887ee6c6e1113ef764f to your computer and use it in GitHub Desktop.
Save JeremyLWright/c887ee6c6e1113ef764f to your computer and use it in GitHub Desktop.
Cannot compile old 2.9.1 scala program
/**
* 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