Skip to content
Snippets Groups Projects
Commit 54291f1b authored by ik1g19's avatar ik1g19
Browse files

fixed addToGrid, insertAt2D and insertAt

parent 9f39595a
No related branches found
No related tags found
No related merge requests found
...@@ -94,7 +94,7 @@ nextPos DownBack (x,y) = (x-1,y+1) ...@@ -94,7 +94,7 @@ nextPos DownBack (x,y) = (x-1,y+1)
elemAt :: [[a]] -> (Int,Int) -> a elemAt :: [[a]] -> (Int,Int) -> a
elemAt ass (x,y) = (ass !! y) !! x --ass means list of list of a's, not associated with other meaning elemAt ass (x,y) = (ass !! y) !! x --ass means list of list of a's, not associated with any other meaning
--returns specified adjacent element in grid, relative to given position --returns specified adjacent element in grid, relative to given position
...@@ -167,7 +167,7 @@ insertString rg s gen | elemAt rg (x,y) /= Rand && ...@@ -167,7 +167,7 @@ insertString rg s gen | elemAt rg (x,y) /= Rand &&
vDirs = validDirs rg s (x,y) vDirs = validDirs rg s (x,y)
addToGrid :: Orientation -> String -> (Int,Int) -> RandGrid addToGrid :: Orientation -> String -> (Int,Int) -> RandGrid
addToGrid dir (c:cs) (x',y') = addToGrid dir cs (nextPos dir (x',y')) charAdded addToGrid dir (c:cs) (x',y') = addToGrid dir cs charAdded (nextPos dir (x',y'))
where where
charAdded = insertAt2D (Letter c) (x',y') rg 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))) --addToGrid dir = map (\(c,(m,n)) -> insertAt2D (Letter c) (m,n) rg) (zip s (take (length s) $ iterate (nextPos dir) (x,y)))
...@@ -175,11 +175,15 @@ insertString rg s gen | elemAt rg (x,y) /= Rand && ...@@ -175,11 +175,15 @@ insertString rg s gen | elemAt rg (x,y) /= Rand &&
--inserts element at location in 2d array --inserts element at location in 2d array
insertAt2D :: a -> (Int,Int) -> [[a]] -> [[a]] insertAt2D :: a -> (Int,Int) -> [[a]] -> [[a]]
insertAt2D newElement (x,y) grid = insertAt a x (grid !! y) insertAt2D newElement (x,y) grid | y == 0 = insertAt newElement x (grid !! y) : drop 1 belowRows
| y == length grid - 1 = aboveRows ++ [insertAt newElement x (grid !! y)]
| otherwise = aboveRows ++ [insertAt newElement x (grid !! y)] ++ drop 1 belowRows
where
(aboveRows,belowRows) = splitAt y grid
--using code from https://stackoverflow.com/questions/43291442/haskell-insert-an-element-on-nth-position --using code from https://stackoverflow.com/questions/43291442/haskell-insert-an-element-on-nth-position
insertAt :: a -> Int -> [a] -> [a] insertAt :: a -> Int -> [a] -> [a]
insertAt newElement 0 as = newElement:as insertAt newElement 0 as = newElement : drop 1 as
insertAt newElement i (a:as) = a : insertAt newElement (i - 1) as insertAt newElement i (a:as) = a : insertAt newElement (i - 1) as
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment