-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputParser.hs
More file actions
33 lines (26 loc) · 1.12 KB
/
InputParser.hs
File metadata and controls
33 lines (26 loc) · 1.12 KB
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
module InputParser where
import Data.Array
import GameTypes
boardFromString :: String -> Board
boardFromString input = listArray boardBounds boardCells
where
boardRows = inputStringToBoardRows input
boardBounds = boardRowsToBoardBounds boardRows
boardCells = boardRowsToCells boardRows
inputStringToBoardRows :: String -> [[Char]]
inputStringToBoardRows input = removeComments (removeBlankLines (lines input))
removeComments :: [[Char]] -> [[Char]]
removeComments inputRows = filter (\x -> (head x) /= '!') inputRows
removeBlankLines :: [[Char]] -> [[Char]]
removeBlankLines inputRows = filter (/="") inputRows
boardRowsToBoardBounds :: [[Char]] -> Boundary
boardRowsToBoardBounds boardRows = ((0, 0), (xBoundary, yBoundary))
where
xBoundary = length boardRows - 1
yBoundary = foldl (\max row -> if length row > max then length row else max ) 0 boardRows - 1
boardRowsToCells :: [[Char]] -> [Cell]
boardRowsToCells boardRows = [charToCell x | x <- concat boardRows]
charToCell :: Char -> Cell
charToCell '*' = Alive
charToCell '.' = Dead
charToCell _ = error "Only periods, asterisks and comments are valid input"