~vonfry/maze-escape

02b30963b61f4b9b1cc2cbde806df1b764cd175e — Vonfry 3 years ago c3ac45c
maze: frame shortest escape path
3 files changed, 22 insertions(+), 8 deletions(-)

M .nix/maze-escape.nix
M lib/Game/Maze.hs
M maze-escape.cabal
M .nix/maze-escape.nix => .nix/maze-escape.nix +3 -3
@@ 1,10 1,10 @@
{ mkDerivation, base, hspec, ilist, lib, mtl, utility-ht }:
{ mkDerivation, base, hspec, ilist, lib, mtl, utility-ht, containers }:
mkDerivation {
  pname = "maze-escape";
  version = "0.1.0.0";
  src = ../.;
  libraryHaskellDepends = [ base ilist mtl utility-ht ];
  testHaskellDepends = [ base hspec ilist mtl utility-ht ];
  libraryHaskellDepends = [ base ilist mtl utility-ht containers ];
  testHaskellDepends = [ base hspec ilist mtl utility-ht containers ];
  homepage = "https://gitlab.com/Vonfry/maze-escape";
  license = lib.licenses.gpl3Plus;
}

M lib/Game/Maze.hs => lib/Game/Maze.hs +18 -5
@@ 5,11 5,14 @@ module Game.Maze (MovableCell(..), Maze2D(..)) where
import Data.Function (on)
import Data.Foldable (minimumBy)
import Data.Ord (comparing)
import Data.Map (Map(..), empty)
import Control.Monad.State
import Control.Monad.Reader

class MovableCell cell where
  movable :: cell -> Bool

class Maze2D map pos cell where
class Eq pos => Maze2D map pos cell where
  adjacence :: map pos cell -> pos -> [pos]

  source :: map pos cell -> [pos]


@@ 19,8 22,18 @@ class Maze2D map pos cell where
  getCell :: map pos cell -> pos -> cell

  shortestEscapePath :: map pos cell -> [pos]
  shortestEscapePath = filterMinLength . fmap bfs . source
  shortestEscapePath = flip evalState empty . runBfs
    where
      filterMinLength = minimumBy $ comparing length
      -- TODO
      bfs _ = []
      runBfs = runReaderT =<< bfs . source
      bfs :: [pos] -> RMapEscape map pos
      bfs _ = pure []

data MapEscapeMark pos =
  MapEscapeMark { marked :: Bool
                , toDest :: [pos]
                }
type RMapEscapeState pos = State (Map pos (MapEscapeMark pos))
type RMapEscape map pos = ReaderT map (RMapEscapeState pos) [pos]

filterMinLength :: [[pos]] -> [pos]
filterMinLength = minimumBy $ comparing length

M maze-escape.cabal => maze-escape.cabal +1 -0
@@ 23,6 23,7 @@ common deps
                    , mtl
                    , ilist
                    , utility-ht
                    , containers

library
    import:           deps