Skip to content
Snippets Groups Projects
Commit 01a896ad authored by ik1g19's avatar ik1g19
Browse files

changed nextLetter to nextElem + added test

parent d6595281
No related branches found
No related tags found
No related merge requests found
......@@ -69,9 +69,9 @@ findPlacement css x y s | checkWordDir css (x,y) Forward s = Just ((x,
| otherwise = Nothing
checkWordDir :: WordSearchGrid -> (Int,Int) -> Orientation -> String -> Bool
checkWordDir css (x,y) dir (l:[]) | nextLetter css x y dir == Just l = True
checkWordDir css (x,y) dir (l:[]) | nextElem css x y dir == Just l = True
| otherwise = False
checkWordDir css (x,y) dir (l:ls) | nextLetter 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 (x,y) dir) dir ls
| otherwise = False
......@@ -88,26 +88,9 @@ nextPos (x,y) UpBack = (x-1,y-1)
nextPos (x,y) DownForward = (x+1,y+1)
nextPos (x,y) DownBack = (x-1,y+1)
--returns element
nextLetter :: WordSearchGrid -> Int -> Int -> Orientation -> Maybe Char
-- nextLetter css x y Forward | x == length css - 1 = Nothing
-- | otherwise = Just ((css !! y) !! (x + 1))
-- nextLetter css x y Back | x == 0 = Nothing
-- | otherwise = Just ((css !! y) !! (x - 1))
-- nextLetter css x y Up | y == 0 = Nothing
-- | otherwise = Just ((css !! (y - 1)) !! x)
-- nextLetter css x y Down | y == length css - 1 = Nothing
-- | otherwise = Just ((css !! (y + 1)) !! x)
-- nextLetter css x y UpForward | y == 0 || x == length css - 1 = Nothing
-- | otherwise = Just ((css !! (y - 1)) !! (x + 1))
-- nextLetter css x y UpBack | y == 0 || x == 0 = Nothing
-- | otherwise = Just ((css !! (y - 1)) !! (x - 1))
-- nextLetter css x y DownForward | y == length css - 1 || x == length css - 1 = Nothing
-- | otherwise = Just ((css !! (y + 1)) !! (x + 1))
-- nextLetter css x y DownBack | y == length css - 1 || x == 0 = Nothing
-- | otherwise = Just ((css !! (y + 1)) !! (x - 1))
nextLetter css x y dir | x' < 0 || y' < 0 || x' > length css - 1 || y' > length css - 1 = Nothing
--returns specified adjacent element in grid, relative to given position
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 ((css !! y') !! x')
where
(x',y') = nextPos (x,y) dir
......
......@@ -6,16 +6,14 @@ main = challenge1Test
exGrid1'1 = [ "HAGNIRTSH" , "SACAGETAK", "GCSTACKEL","MGHKMILKI","EKNLETGCN","TNIRTLETE","IRAAHCLSR","MAMROSAGD","GIZKDDNRG" ]
exWords1'1 = [ "HASKELL","STRING","STACK","MAIN","METHOD"]
-- exAns1'1 = [("HASKELL",Just((0,0),DownForward)),("STRING",Just((7,0),Back)),("STACK",Just((2,2),Forward)),
-- ("MAIN", Just((2,7),Up )),("METHOD",Just((4,3),Down))]
exAns1'1 = [("HASKELL",Just((0,0),DownForward)),("STRING",Just((7,0),Back)),("STACK",Just((2,2),Forward)),("MAIN",Just((2,7),Up)),("METHOD",Just((4,3),Down))]
exAns1'1 = [("HASKELL",Just((0,0),DownForward)),("STRING",Just((7,0),Back)),("STACK",Just((2,2),Forward)),
("MAIN", Just((2,7),Up )),("METHOD",Just((4,3),Down))]
exGrid1'2 = ["ROBREUMBR","AURPEPSAN","UNLALMSEE","YGAUNPYYP","NLMNBGENA","NBLEALEOR","ALRYPBBLG","NREPBEBEP","YGAYAROMR"]
exWords1'2 = [ "BANANA", "ORANGE", "MELON", "RASPBERRY","APPLE","PLUM","GRAPE" ]
-- exAns1'2 = [("BANANA", Just((5,6),UpBack )),("ORANGE",Just((1,0),DownForward)),("MELON",Just((7,8),Up )),
-- ("RASPBERRY",Just((8,0),DownBack)),("APPLE", Just((2,8),UpForward )),("PLUM", Just((5,1),DownBack)),
-- ("GRAPE", Just((8,6),Up ))]
--exAns1'2 = [("BANANA",Just((5,6),UpBack)),("ORANGE",Just((1,0),DownForward)),("MELON",Just((7,8),Up)),("RASPBERRY",Just((8,0),DownBack)),("APPLE", Just((2,8),UpForward)),("PLUM", Just((5,1),DownBack)),("GRAPE",Just((8,6),Up))]
exAns1'2 = [("BANANA", Just((5,6),UpBack )),("ORANGE",Just((1,0),DownForward)),("MELON",Just((7,8),Up )),
("RASPBERRY",Just((8,0),DownBack)),("APPLE", Just((2,8),UpForward )),("PLUM", Just((5,1),DownBack)),
("GRAPE", Just((8,6),Up ))]
exGrid1'3 = ["TEST","ASBD","GDFI","FDGS"]
exWords1'3 = ["TEST"]
......@@ -38,6 +36,6 @@ challenge1Test = do putStrLn "Challenge 1 Start Test"
putStrLn "========================================="
assert1 (solveWordSearch exWords1'3 exGrid1'3) exAns1'3
assert1 (solveWordSearch exWords1'1 exGrid1'1) exAns1'1
--assert1 (solveWordSearch exWords1'2 exGrid1'2) exAns1'2
assert1 (solveWordSearch exWords1'2 exGrid1'2) exAns1'2
putStrLn "========================================="
putStrLn "Challenge 1 End Test"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment