Skip to content

Commit

Permalink
refactored some code, added makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelong Cong committed Nov 13, 2013
1 parent f2a75b1 commit b6f4a21
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dist
cabal-dev
main
Main
*.o
*.hi
*.chi
Expand Down
43 changes: 21 additions & 22 deletions Enigma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ type State = [Rot_Loc]
data Direction = Fwd | Bwd deriving (Show, Eq)

data Conf = Conf
{ pb :: PB_Conf
, refl :: Ref_Conf
, rtype :: [Rot_Type]
, ring :: [Rot_Ring]}
{ get_pb :: PB_Conf
, get_refl :: Ref_Conf
, get_type :: [Rot_Type]
, get_ring :: [Rot_Ring]}
deriving (Show)


-- helper functions
alphs :: [Char]
alphs = ['A'..'Z']

rem26 :: Int -> Int
Expand Down Expand Up @@ -64,7 +65,7 @@ rotate_rotor x conf state
| otherwise = state
where
(part1,_:part2) = splitAt x state
pawl_loc = snd (rtype conf !! (x+1))
pawl_loc = snd (get_type conf !! (x+1))
right_loc = state !! (x+1)
current_loc = state !! x

Expand All @@ -74,31 +75,29 @@ reflector conf c = plugboard Bwd conf c


rotor :: Direction -> Rot_Type -> Rot_Ring -> Rot_Loc -> Char -> Char
rotor dir (rt,_) ring loc c -- where rt is rotor type
| dir == Fwd =
intToChar (rem26 $ (charToInt fwd_f) - iloc + iring)
| dir == Bwd =
intToChar (rem26 $ (charToInt bwd_f) - iloc + iring)
| otherwise = error "Unknown direction"
rotor dir (tpe,_) ring loc c = -- where tpe is rotor type
intToChar (rem26 $ (charToInt code) - iloc + iring)
where
ic = charToInt c
iloc = charToInt loc
iring = charToInt ring
fwd_f = rt!! rem26 (ic + iloc - iring)
bwd_f = alphs !! getIndex (intToChar (rem26 (ic + iloc - iring))) rt
code = if (dir == Fwd) -- for intermediate calculation
then tpe !! rem26 (ic + iloc - iring)
else alphs !! getIndex ((intToChar.rem26) $ ic + iloc - iring) tpe



enigma_char :: Conf -> State -> Char -> Char
enigma_char conf state c =
( plugboard Bwd (pb conf)
. rotor Bwd ((rtype conf) !! 2) ((ring conf) !! 2) (state !! 2)
. rotor Bwd ((rtype conf) !! 1) ((ring conf) !! 1) (state !! 1)
. rotor Bwd ((rtype conf) !! 0) ((ring conf) !! 0) (state !! 0)
. reflector (refl conf)
. rotor Fwd ((rtype conf) !! 0) ((ring conf) !! 0) (state !! 0)
. rotor Fwd ((rtype conf) !! 1) ((ring conf) !! 1) (state !! 1)
. rotor Fwd ((rtype conf) !! 2) ((ring conf) !! 2) (state !! 2)
. plugboard Fwd (pb conf)
( plugboard Bwd (get_pb conf)
. rotor Bwd ((get_type conf) !! 2) ((get_ring conf) !! 2) (state !! 2)
. rotor Bwd ((get_type conf) !! 1) ((get_ring conf) !! 1) (state !! 1)
. rotor Bwd ((get_type conf) !! 0) ((get_ring conf) !! 0) (state !! 0)
. reflector (get_refl conf)
. rotor Fwd ((get_type conf) !! 0) ((get_ring conf) !! 0) (state !! 0)
. rotor Fwd ((get_type conf) !! 1) ((get_ring conf) !! 1) (state !! 1)
. rotor Fwd ((get_type conf) !! 2) ((get_ring conf) !! 2) (state !! 2)
. plugboard Fwd (get_pb conf)
) c


Expand Down
4 changes: 4 additions & 0 deletions main.hs → Main.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@

import Enigma

myconf :: Conf
myconf = Conf plugs ref_b [rtypeI,rtypeII,rtypeIII] ['A','A','A']
mystate :: State
mystate = ['A','A','A']

run :: String -> String
run = enigma myconf mystate

main :: IO ()
main = do
msg <- getLine
putStrLn (run msg)
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
all:
ghc -Wall Main.hs
clean:
rm -rf *.o *.hi Main
12 changes: 6 additions & 6 deletions tests.hs → Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ rotor_test = (
-- detailed tests: encode A at rotor pos AAV using default settings
-------------------------------------------------------------------------------
rotor2_fwd_test =
(rotor Fwd ((rtype aaa_conf) !! 2) ((ring aaa_conf) !! 2) 'V' 'A')
(rotor Fwd ((get_type aaa_conf) !! 2) ((get_ring aaa_conf) !! 2) 'V' 'A')
==
'R'

-- carried forward from rotor2_fwd_test
rotor1_fwd_test =
(rotor Fwd ((rtype aaa_conf) !! 1) ((ring aaa_conf) !! 1) 'A' 'R')
(rotor Fwd ((get_type aaa_conf) !! 1) ((get_ring aaa_conf) !! 1) 'A' 'R')
==
'G'

-- carried forward from rotor1_fwd_test
rotor0_fwd_test =
(rotor Fwd ((rtype aaa_conf) !! 0) ((ring aaa_conf) !! 0) 'A' 'G')
(rotor Fwd ((get_type aaa_conf) !! 0) ((get_ring aaa_conf) !! 0) 'A' 'G')
==
'D'

Expand All @@ -51,17 +51,17 @@ reflector_test =

-- backwards now..
rotor0_bwd_test =
(rotor Bwd ((rtype aaa_conf) !! 0) ((ring aaa_conf) !! 0) 'A' 'H')
(rotor Bwd ((get_type aaa_conf) !! 0) ((get_ring aaa_conf) !! 0) 'A' 'H')
==
'P'

rotor1_bwd_test =
(rotor Bwd ((rtype aaa_conf) !! 1) ((ring aaa_conf) !! 1) 'A' 'P')
(rotor Bwd ((get_type aaa_conf) !! 1) ((get_ring aaa_conf) !! 1) 'A' 'P')
==
'U'

rotor2_bwd_test =
(rotor Bwd ((rtype aaa_conf) !! 2) ((ring aaa_conf) !! 2) 'V' 'U')
(rotor Bwd ((get_type aaa_conf) !! 2) ((get_ring aaa_conf) !! 2) 'V' 'U')
==
'M'

Expand Down

0 comments on commit b6f4a21

Please sign in to comment.