Cleaned the code

This commit is contained in:
Ivy 2021-02-18 20:35:11 +01:00
parent 5254c8429e
commit 59fd2ce561
1 changed files with 8 additions and 6 deletions

View File

@ -1,20 +1,19 @@
module Dungeon where module Dungeon where
import Data.Matrix hiding ((<|>)) import Data.Matrix
import Linear.V2 import Linear.V2
import Data.Tuple import Data.Tuple
import Data.Maybe import Data.Maybe
import Control.Applicative ((<|>))
data Cell = Solid data Cell = Solid
| Empty | Empty
deriving (Eq) deriving (Eq)
instance Show Cell where instance Show Cell where
show cell = [fromJust (lookup cell cellChars <|> Just '?')] show cell = [fromMaybe '?' (lookup cell cellMapping)]
cellChars :: [(Cell, Char)] cellMapping :: [(Cell, Char)]
cellChars = cellMapping =
[ (Empty, '.') [ (Empty, '.')
, (Solid, '#') , (Solid, '#')
] ]
@ -27,7 +26,10 @@ instance Show Dungeon where
makeDungeonFromFile :: String -> IO Dungeon makeDungeonFromFile :: String -> IO Dungeon
makeDungeonFromFile f = do makeDungeonFromFile f = do
contents <- readFile f contents <- readFile f
return $ Dungeon $ fromLists $ map (fromJust . (`lookup` map swap cellChars)) <$> lines contents let cellMappingR = map swap cellMapping
charToCell c = fromMaybe (error "Invalid cell in the .map file") (c `lookup` cellMappingR)
cellLists = map charToCell <$> lines contents
return . Dungeon . fromLists $ cellLists
dungeonToLists :: Dungeon -> [[Cell]] dungeonToLists :: Dungeon -> [[Cell]]
dungeonToLists (Dungeon m) = toLists m dungeonToLists (Dungeon m) = toLists m