Skip to content
Snippets Groups Projects
Commit 9f39595a authored by ik1g19's avatar ik1g19
Browse files

progress on challenge 2

parent 5688b2c1
No related branches found
No related tags found
No related merge requests found
......@@ -161,16 +161,26 @@ insertString :: RandGrid -> String -> StdGen -> (RandGrid,StdGen)
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
| 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
addToGrid :: Orientation -> String -> (Int,Int) -> RandGrid
addToGrid dir (c:cs) (x',y') = addToGrid dir cs (nextPos dir (x',y')) charAdded
where
charAdded = insertAt2D (Letter c) (x',y') rg
--addToGrid dir = map (\(c,(m,n)) -> insertAt2D (Letter c) (m,n) rg) (zip s (take (length s) $ iterate (nextPos dir) (x,y)))
--inserts element at location in 2d array
insertAt2D :: a -> (Int,Int) -> [[a]] -> [[a]]
insertAt2D newElement (x,y) grid = insertAt a x (grid !! y)
--using code from https://stackoverflow.com/questions/43291442/haskell-insert-an-element-on-nth-position
insertAt :: a -> Int -> [a] -> [a]
insertAt newElement 0 as = newElement:as
insertAt newElement i (a:as) = a : insertAt newElement (i - 1) as
generatePos :: StdGen -> Int -> ((Int,Int),StdGen)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment