DojoDeveloppement/Mercredi4Janvier2012La dernière fois, c'était l'année dernière, le /Mercredi21Decembre2011 :
Bon. Ce soir :
module Tests
where
import Test.HUnit
import Maze
main = runTestTT tests
tests = TestList [
let m = Maze (0,0) (0,1) [(0,0),(0,1)]
v = [(0,0)]
in Maze.path m v ~?= [East],
let m = Maze (0,0) (0,2) [(0,0),(0,2)]
v = [(0,0)]
in Maze.path m v ~?= [],
let m = Maze (0,1) (0,2) [(0,0),(0,1),(0,2)]
v = [(0,0),(0,1)]
in Maze.path m v ~?= [],
let m = Maze (0,1) (0,0) [(0,0),(0,1)]
v = [(0,1)]
in Maze.path m v ~?= [West],
let m = Maze (0,0) (1,0) [(0,0),(1,0)]
v = [(0,0)]
in Maze.path m v ~?= [South],
let m = Maze (1,0) (0,0) [(0,0),(1,0)]
v = [(1,0)]
in Maze.path m v ~?= [North],
let m = Maze (0,1) (0,2) [(0,0),(0,1),(0,2)]
v = [(0,1)]
in Maze.path m v ~?= [East],
let m = Maze (0,0) (0,2) [(0,0),(0,1),(0,2)]
v = [(0,0)]
in Maze.path m v ~?= [East,East]
]
module Maze (Maze (..), Direction (..), path)
where
import Data.List
data Maze = Maze { entry :: Point,
exit :: Point,
passages :: [Point] }
type Point = (Int,Int)
type Path = [Direction]
data Direction = East | West | South | North
deriving (Eq, Show)
dirs = [East,West,South,North]
path :: Maze -> [Point] -> Path
path m@(Maze (0,0) (0,2) [(0,0),(0,1),(0,2)]) vs@[v@(0,0)] =
concat [[d] ++ path m (v `going` d:vs) | d <- dirs,
not (null (path m (v `going` d:vs))) ]
path m vs = case successDirections m vs of
[] -> []
sd -> [head sd]
successDirections m (v:vs) =
[d | d <- dirs,
let v' = (v `going` d)
in visitable v' && v' == (exit m)]
where
visitable p = p `elem` ((passages m) \\ vs)
going :: Point -> Direction -> Point
going (r,c) East = (r,c+1)
going (r,c) West = (r,c-1)
going (r,c) South = (r+1,c)
going (r,c) North = (r-1,c)