From 112ba19bd1df10cdb24e99dd3853dd99576c1610 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 1 May 2021 20:14:33 -0400 Subject: [PATCH] read from file done --- Ekitai.hs | 16 +++++++++++++--- Sim.hs | 16 +++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Ekitai.hs b/Ekitai.hs index 6c1609e..6b9806f 100644 --- a/Ekitai.hs +++ b/Ekitai.hs @@ -11,16 +11,26 @@ import Render -- ui :: B.Widget () -- ui = B.str "hello" <+> B.str "World" +hGetLines :: Handle -> IO [String] +hGetLines h = do + line <- hGetLine h + isEof <- hIsEOF h + if isEof then return [line] + else do + lines <- hGetLines h + return (line:lines) + main = do argv <- getArgs (opts, fname) <- ekitaiOpts argv handle <- openFile fname ReadMode - contents <- hGetContents handle - putStr contents + contents <- hGetLines handle hClose handle - initialState <- buildInitialState $ stringToSim 10 10 contents + -- putStrLn $ show $ stringToSim contents + initialState <- buildInitialState $ stringToSim contents endState <- B.defaultMain ekitaiApp initialState print endState + return 0 -- main :: IO () -- main = do diff --git a/Sim.hs b/Sim.hs index 8f20343..5769c47 100644 --- a/Sim.hs +++ b/Sim.hs @@ -95,14 +95,20 @@ insert n y xs = countdown n xs where countdown _ [] = [] countdown m (x:xs) = x:countdown (m-1) xs -stringToSim :: Int -> Int -> [Char] -> Simulation -stringToSim w h st = - _stringToSim st [(x, y) | x <- [0..w-1], y <- [0..h-1]] (initSimSpace w h) +-- stringToSim :: [String] -> Simulation +stringToSim strings = + _stringToSim st grid (initSimSpace w h) + where stripped = strings + w = maximum $ [(length s) | s <- stripped] + h = length stripped + grid = [(a,b) | a <- [0..(length stripped)-1], b <- [0..(length $ stripped !! a)-1]] + st = concat stripped + _stringToSim st grid acc = if null grid || null st then acc else _stringToSim (tail st) (tail grid) next - where x = fst $ head grid - y = snd $ head grid + where y = fst $ head grid -- not exactly sure why y and x got switched here + x = snd $ head grid next = simSet acc (charToChunk $ head st) x y -- maps each chunktype to an ascii character -- 2.20.1