diff --git a/cw/src/Challenges.hs b/cw/src/Challenges.hs index d4e76a629f90b2d8c7f6c12f1992b105c0e7979d..422a9a75f5ab89b0ae6bd76f0df1c2eed1b5455f 100644 --- a/cw/src/Challenges.hs +++ b/cw/src/Challenges.hs @@ -7,7 +7,7 @@ -- DO NOT MODIFY THE FOLLOWING LINES OF CODE module Challenges (WordSearchGrid,Placement,Posn,Orientation(..),solveWordSearch, createWordSearch, LamMacroExpr(..),LamExpr(..),prettyPrint, parseLamMacro, - cpsTransform,innerRedn1,outerRedn1,compareInnerOuter,generatePos) where + cpsTransform,innerRedn1,outerRedn1,compareInnerOuter) where -- Import standard library and parsing definitions from Hutton 2016, Chapter 13 -- We import System.Random - make sure that your installation has it installed - use stack ghci and stack ghc @@ -74,7 +74,7 @@ findPlacement css (x,y) s | checkWordDir css (x,y) Forward s = Just (( checkWordDir :: WordSearchGrid -> (Int,Int) -> Orientation -> String -> Bool checkWordDir css (x,y) dir (l:[]) | 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 (x,y) dir) dir ls +checkWordDir css (x,y) dir (l:ls) | nextElem css (x,y) dir == Just l = checkWordDir css (nextPos dir (x,y)) dir ls | otherwise = False @@ -82,15 +82,15 @@ checkWordDir css (x,y) dir (l:ls) | nextElem css (x,y) dir == Just l = checkWo --------------------pattern matching for traversing the grid-------------------- --returns position of movement in a given direction -nextPos :: (Int,Int) -> Orientation -> (Int,Int) -nextPos (x,y) Forward = (x+1,y) -nextPos (x,y) Back = (x-1,y) -nextPos (x,y) Up = (x,y-1) -nextPos (x,y) Down = (x,y+1) -nextPos (x,y) UpForward = (x+1,y-1) -nextPos (x,y) UpBack = (x-1,y-1) -nextPos (x,y) DownForward = (x+1,y+1) -nextPos (x,y) DownBack = (x-1,y+1) +nextPos :: Orientation -> (Int,Int) -> (Int,Int) +nextPos Forward (x,y) = (x+1,y) +nextPos Back (x,y) = (x-1,y) +nextPos Up (x,y) = (x,y-1) +nextPos Down (x,y) = (x,y+1) +nextPos UpForward (x,y) = (x+1,y-1) +nextPos UpBack (x,y) = (x-1,y-1) +nextPos DownForward (x,y) = (x+1,y+1) +nextPos DownBack (x,y) = (x-1,y+1) elemAt :: [[a]] -> (Int,Int) -> a @@ -102,7 +102,7 @@ nextElem :: [[a]] -> (Int,Int) -> Orientation -> Maybe a nextElem css (x,y) dir | x' < 0 || y' < 0 || x' > length css - 1 || y' > length css - 1 = Nothing | otherwise = Just (elemAt css (x',y')) where - (x',y') = nextPos (x,y) dir + (x',y') = nextPos dir (x,y) -- Two examples for you to try out, the first of which is in the instructions @@ -126,8 +126,7 @@ createWordSearch ss den = do gen <- getStdGen where charInInput = sum (map length ss) longestWordLen = max (map length ss) - dim = head [x | x <- [0..], x^2 > (charInInput / den), x >= longestWordLen] --calculates needed dimension of grid according - --to the density + dim = head [x | x <- [0..], x^2 > (charInInput / den), x >= longestWordLen] --calculates needed dimension of grid according to the density createGrid :: [String] -> Int -> StdGen -> WordSearchGrid @@ -159,13 +158,19 @@ checkDir rg s (x,y) dir = --adds an individual string to a given grid --returns new grid and new generator insertString :: RandGrid -> String -> StdGen -> (RandGrid,StdGen) -insertString rg s gen | - | length vDirs == 0 = insertString rg s newGen - | +insertString rg s gen | elemAt rg (x,y) /= Rand && + elemAt rg (x,y) /= Letter (head s) = insertString rg s newGen --guard:if position is invalid, generate new position + | length vDirs == 0 = insertString rg s newGen --guard:if no valid orientations exist, generate new position + | otherwise where ((x,y),newGen) = generatePos gen (length rg) vDirs = validDirs rg s (x,y) + addToGrid :: String -> Orientation -> RandGrid + addToGrid s dir = iterate + --iterate to get positions + --zip together with string + --map to add to grid generatePos :: StdGen -> Int -> ((Int,Int),StdGen)