diff --git a/cw/src/Challenges.hs b/cw/src/Challenges.hs index f5ff8e17b84b1f50ea9fb4ba445d56f085d176cc..a124d68dbbaf9cd98ee6342ac196321263f0cdef 100644 --- a/cw/src/Challenges.hs +++ b/cw/src/Challenges.hs @@ -66,21 +66,19 @@ findLocation css (x,y) s@(l:ls) | x > limit && y > limit = N {--| checks for hidden word in possible directions |--} findPlacement :: WordSearchGrid -> Posn -> String -> Maybe Placement -findPlacement css (x,y) s | checkWordDir css (x,y) Forward s = Just ((x,y),Forward) - | checkWordDir css (x,y) Back s = Just ((x,y),Back) - | checkWordDir css (x,y) Up s = Just ((x,y),Up) - | checkWordDir css (x,y) Down s = Just ((x,y),Down) - | checkWordDir css (x,y) UpForward s = Just ((x,y),UpForward) - | checkWordDir css (x,y) UpBack s = Just ((x,y),UpBack) - | checkWordDir css (x,y) DownForward s = Just ((x,y),DownForward) - | checkWordDir css (x,y) DownBack s = Just ((x,y),DownBack) - | otherwise = Nothing - - -checkWordDir :: WordSearchGrid -> Posn -> Orientation -> String -> Bool -checkWordDir css (x,y) dir (l:[]) | nextElem css (x,y) dir == Just l = True +findPlacement css (x,y) s = findP dirs + where + findP [] = Nothing + findP (d:ds) | checkWordDir css (x,y) s d = Just ( (x,y),d ) + | otherwise = findP ds + + dirs = [Forward,Back,Up,Down,UpForward,UpBack,DownForward,DownBack] + + +checkWordDir :: WordSearchGrid -> Posn -> String -> Orientation -> Bool +checkWordDir css (x,y) (l:[]) dir | nextElem css (x,y) dir == Just l = True | otherwise = False -checkWordDir css (x,y) dir (l:ls) | nextElem css (x,y) dir == Just l = checkWordDir css (nextPos dir (x,y)) dir ls +checkWordDir css (x,y) (l:ls) dir | nextElem css (x,y) dir == Just l = checkWordDir css (nextPos dir (x,y)) ls dir | otherwise = False