From 59fd2ce561fa9a3ec4d8d6190f117fd7bb81a868 Mon Sep 17 00:00:00 2001 From: Ivy Date: Thu, 18 Feb 2021 20:35:11 +0100 Subject: [PATCH] Cleaned the code --- src/Dungeon.hs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Dungeon.hs b/src/Dungeon.hs index 6a4d21a..a362043 100644 --- a/src/Dungeon.hs +++ b/src/Dungeon.hs @@ -1,20 +1,19 @@ module Dungeon where -import Data.Matrix hiding ((<|>)) +import Data.Matrix import Linear.V2 import Data.Tuple import Data.Maybe -import Control.Applicative ((<|>)) data Cell = Solid | Empty deriving (Eq) instance Show Cell where - show cell = [fromJust (lookup cell cellChars <|> Just '?')] + show cell = [fromMaybe '?' (lookup cell cellMapping)] -cellChars :: [(Cell, Char)] -cellChars = +cellMapping :: [(Cell, Char)] +cellMapping = [ (Empty, '.') , (Solid, '#') ] @@ -27,7 +26,10 @@ instance Show Dungeon where makeDungeonFromFile :: String -> IO Dungeon makeDungeonFromFile f = do 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 m) = toLists m