Skip to content
Permalink
Browse files

Use type synonym

  • Loading branch information...
jhb563 committed Mar 30, 2019
1 parent 88684e0 commit 0869a0358e096d14ab2536c5ebb113d748c2f6e8
Showing with 15 additions and 13 deletions.
  1. +9 −9 src/MazeParser.hs
  2. +2 −2 src/Runner.hs
  3. +3 −1 src/Types.hs
  4. +1 −1 test/Spec.hs
@@ -17,7 +17,7 @@ import Text.Megaparsec (Parsec)
import qualified Text.Megaparsec as M
import qualified Text.Megaparsec.Char as M

import Types (Location, CellBoundaries(..), BoundaryType(..), World)
import Types (Location, CellBoundaries(..), BoundaryType(..), World, Maze)

-- top-right-bottom-left
-- 0 = 0000
@@ -42,7 +42,7 @@ type MParser = Parsec Void Text
parseWorldFromFile :: FilePath -> IO World
parseWorldFromFile = undefined

sampleMaze :: Map.Map Location CellBoundaries
sampleMaze :: Maze
sampleMaze = fromRight undefined $ M.runParser (mazeParser (5,5)) "" testMaze

testMaze :: Text
@@ -54,7 +54,7 @@ testMaze = pack $ unlines
, "32EB6"
]

mazeParser :: (Int, Int) -> MParser (Map.Map Location CellBoundaries)
mazeParser :: (Int, Int) -> MParser Maze
mazeParser (numRows, numColumns) = do
rows <- forM [(numRows - 1),(numRows - 2)..0] $ \i -> do
columns <- forM [0..(numColumns - 1)] $ \j -> do
@@ -104,10 +104,10 @@ charToBoundsSet 'e' = (True, True, True, False)
charToBoundsSet 'f' = (True, True, True, True)
charToBoundsSet _ = error "Invalid character!"-}

dumpMaze :: Map.Map Location CellBoundaries -> Text
dumpMaze :: Maze -> Text
dumpMaze maze = pack $ (unlines . reverse) (rowToString <$> cellsByRow)
where
transposedMap :: Map.Map Location CellBoundaries
transposedMap :: Maze
transposedMap = Map.mapKeys (\(x, y) -> (y, x)) maze

cellsByRow :: [[(Location, CellBoundaries)]]
@@ -132,14 +132,14 @@ dumpMaze maze = pack $ (unlines . reverse) (rowToString <$> cellsByRow)
_ -> 1
in toUpper $ intToDigit (top + right + down + left)

generateRandomMaze :: StdGen -> (Int, Int) -> Map.Map Location CellBoundaries
generateRandomMaze :: StdGen -> (Int, Int) -> Maze
generateRandomMaze gen (numRows, numColumns) = currentBoundaries (execState dfsSearch initialState)
where
(startX, g1) = randomR (0, numColumns - 1) gen
(startY, g2) = randomR (0, numRows - 1) g1
initialState = SearchState g2 [(startX, startY)] initialBounds Set.empty

initialBounds :: Map.Map Location CellBoundaries
initialBounds :: Maze
initialBounds = case M.runParser (mazeParser (numRows, numColumns)) "" fullString of
Left _ -> error "Maze can't be parsed!"
Right success -> success
@@ -152,7 +152,7 @@ generateRandomMaze gen (numRows, numColumns) = currentBoundaries (execState dfsS
data SearchState = SearchState
{ randomGen :: StdGen
, locationStack :: [Location]
, currentBoundaries :: Map.Map Location CellBoundaries
, currentBoundaries :: Maze
, visitedCells :: Set.Set Location
}

@@ -168,7 +168,7 @@ dfsSearch = do
else chooseCandidate candidateLocs >> dfsSearch

where
findCandidates :: Location -> Map.Map Location CellBoundaries -> Set.Set Location -> [(Location, CellBoundaries, Location, CellBoundaries)]
findCandidates :: Location -> Maze -> Set.Set Location -> [(Location, CellBoundaries, Location, CellBoundaries)]
findCandidates currentLocation@(x, y) bounds visited =
let currentLocBounds = fromJust $ Map.lookup currentLocation bounds
upLoc = (x, y + 1)
@@ -10,7 +10,7 @@ import Graphics.Gloss
import Graphics.Gloss.Interface.IO.Interact

import MazeParser (generateRandomMaze, sampleMaze)
import Types (Location, CellCoordinates(..), BoundaryType(..), CellBoundaries(..), World(..))
import Types (Location, CellCoordinates(..), BoundaryType(..), CellBoundaries(..), World(..), Maze)

globalCellSize :: Float
globalCellSize = 25
@@ -27,7 +27,7 @@ maxCellIndex = 24
windowDisplay :: Display
windowDisplay = InWindow "Window" (625, 625) (10, 10)

boundariesMap :: (Int, Int) -> Map.Map Location CellBoundaries
boundariesMap :: (Int, Int) -> Maze
boundariesMap (numColumns, numRows) = Map.fromList
(buildBounds <$> (range ((0,0), (numColumns - 1, numRows - 1))))
where
@@ -26,9 +26,11 @@ data CellBoundaries = CellBoundaries
}
deriving (Show, Eq)

type Maze = Map.Map Location CellBoundaries

data World = World
{ playerLocation :: Location
, startLocation :: Location
, endLocation :: Location
, worldBoundaries :: Map.Map Location CellBoundaries
, worldBoundaries :: Maze
}
@@ -35,7 +35,7 @@ testMazeString = pack $ unlines
, "32EB6"
]

testSolution :: Map.Map Location CellBoundaries
testSolution :: Maze
testSolution = Map.fromList
[ ((0,0), CellBoundaries (AdjacentCell (0,1)) (AdjacentCell (1,0)) WorldBoundary WorldBoundary)
, ((1,0), CellBoundaries (AdjacentCell (1,1)) (AdjacentCell (2,0)) WorldBoundary (AdjacentCell (0,0)))

0 comments on commit 0869a03

Please sign in to comment.
You can’t perform that action at this time.