-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.hs
45 lines (40 loc) · 1.3 KB
/
main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Data.Char
import Display
import Kakuro
data PuzzleAction
= Print
| Export FilePath
| Display
| Invalid
main :: IO ()
main = do
putStrLn "Welcome to the kakuro solver!"
putStrLn "What is the file name of the Kakuro?"
fileName <- getLine
putStrLn
"What would you like to do with this puzzle? (print / export <filename> / display)"
action <- getLine
doAction (parseAction action) fileName
parseAction :: String -> PuzzleAction
parseAction input
| null inputWords = stringToAction ("", Nothing)
| otherwise = stringToAction (action, path)
where
inputWords = Prelude.words input
action = map toLower $ head inputWords
path =
if action == "export"
then Just $ inputWords !! 1
else Nothing
stringToAction :: (String, Maybe FilePath) -> PuzzleAction
stringToAction ("print", Nothing) = Print
stringToAction ("export", Just path) = Export path
stringToAction ("display", Nothing) = Display
stringToAction _ = Invalid
doAction :: PuzzleAction -> FilePath -> IO ()
doAction Print importPath =
drawIOPuzzle . solveIOPuzzle . readIOPuzzle $ importPath
doAction (Export path) importPath = readSolveAndExport importPath path
doAction Display importPath =
(solveIOPuzzle . readIOPuzzle $ importPath) >>= puzzleWindow
doAction Invalid _ = putStrLn "Invalid action"