roguelike/src/Dungeon.hs

33 lines
747 B
Haskell
Raw Normal View History

2021-02-11 22:40:00 +00:00
module Dungeon where
2021-02-15 19:57:45 +00:00
import Data.Matrix hiding ((<|>))
import Data.Tuple
import Data.Maybe
import Control.Applicative ((<|>))
2021-02-11 22:40:00 +00:00
2021-02-15 19:57:45 +00:00
data Cell = Solid
| Empty
deriving (Eq)
2021-02-11 22:40:00 +00:00
instance Show Cell where
2021-02-15 19:57:45 +00:00
show cell = [fromJust (lookup cell cellChars <|> Just '?')]
cellChars :: [(Cell, Char)]
cellChars =
[ (Empty, '.')
, (Solid, '#')
]
2021-02-11 22:40:00 +00:00
newtype Dungeon = Dungeon (Matrix Cell)
instance Show Dungeon where
show (Dungeon m) = unlines . map (concatMap show) $ toLists m
2021-02-15 19:57:45 +00:00
makeDungeonFromFile :: String -> IO Dungeon
makeDungeonFromFile f = do
contents <- readFile f
return $ Dungeon $ fromLists $ map (fromJust . (`lookup` map swap cellChars)) <$> lines contents
2021-02-11 22:40:00 +00:00
dungeonToLists :: Dungeon -> [[Cell]]
dungeonToLists (Dungeon m) = toLists m