From 01a896ad688a61d4a1d25299bcd0d2bfdbb6604f Mon Sep 17 00:00:00 2001 From: ik1g19 <ik1g19@soton.ac.uk> Date: Mon, 28 Dec 2020 14:18:35 +0000 Subject: [PATCH] changed nextLetter to nextElem + added test --- cw/src/Challenges.hs | 27 +++++---------------------- cw/test/Spec.hs | 14 ++++++-------- 2 files changed, 11 insertions(+), 30 deletions(-) diff --git a/cw/src/Challenges.hs b/cw/src/Challenges.hs index 965da44..03e238f 100644 --- a/cw/src/Challenges.hs +++ b/cw/src/Challenges.hs @@ -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 diff --git a/cw/test/Spec.hs b/cw/test/Spec.hs index 1708f91..6308a11 100644 --- a/cw/test/Spec.hs +++ b/cw/test/Spec.hs @@ -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 -- GitLab