1
0
Fork 0
pr3-sose2026-fork/haskell/04-hanoi/hanoi.hs

71 lines
2.0 KiB
Haskell

{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use void" #-}
import Test.HUnit
-- some helper function that describes a single move
move :: Int -> Char -> Char -> String
move n src dst = "move disk " ++ show n ++ " from " ++ [src] ++ " to " ++ [dst]
-- transfers a number of disks (the Int) from a source (the first Char) rod to a destination (the second Char) rod via a temp (the third char) rod
-- returns a list of required operations
hanoi :: (Int,Char,Char,Char) -> [String]
--TODO: implement here
-- Source, Destination, Temp
hanoiTests :: [((Int, Char, Char, Char), [String])]
hanoiTests =
[ ( (1, 'a', 'c', 'b'),
[ "move disk 1 from a to c"
]
),
( (1, 'b', 'a', 'c'),
[ "move disk 1 from b to a"
]
),
( (2, 'a', 'c', 'b'),
[ "move disk 1 from a to b",
"move disk 2 from a to c",
"move disk 1 from b to c"
]
),
( (3, 'a', 'c', 'b'),
[ "move disk 1 from a to c",
"move disk 2 from a to b",
"move disk 1 from c to b",
"move disk 3 from a to c",
"move disk 1 from b to a",
"move disk 2 from b to c",
"move disk 1 from a to c"
]
),
( (4, 'a', 'c', 'b'),
[ "move disk 1 from a to b",
"move disk 2 from a to c",
"move disk 1 from b to c",
"move disk 3 from a to b",
"move disk 1 from c to a",
"move disk 2 from c to b",
"move disk 1 from a to b",
"move disk 4 from a to c",
"move disk 1 from b to c",
"move disk 2 from b to a",
"move disk 1 from c to a",
"move disk 3 from b to c",
"move disk 1 from a to b",
"move disk 2 from a to c",
"move disk 1 from b to c"
]
)
]
hanoiTestCases :: [Test]
hanoiTestCases = map (\(input, expected) -> TestCase (assertEqual ("hanoi " ++ show input) expected (hanoi input))) hanoiTests
tests :: Test
tests = TestList hanoiTestCases
main :: IO ()
main = runTestTT tests >> return ()