--Liz Evans --Cs429 module Math where type Board = [Int] -- the slide slide :: Int -> Int -> [Int] -> [Int] slide x n xs = if ((inList' (x-1) xs) && (swapable x n xs)) then swap (numOf x 1 xs) 0 xs else xs --check to make sure it's valid input inList':: Int -> [Int] -> Bool inList' x [] = False inList' x (h:t) = if (x == h) then True else inList' x t --we assume that a and b are valid swap' :: Int -> Int -> [Int] -> [Int] swap' a b (x:xs) = if (a == x) then b:(swap' a b xs) else if (b == x) then a:(swap' a b xs) else x:(swap' a b xs) swap' a b [] = [] swap :: Int -> Int -> [Int] -> [Int] swap a b xs = if ((inList' a xs) && (inList' b xs)) then (swap' a b xs) else error "arguments not on board" -- are the two squares swapable, the validation --helpers swapable :: Int -> Int -> [Int] -> Bool swapable x n ls= let px = x p0 = (posOf 0 1 ls) in if (( ( ((p0+1) == px) && notMulOf p0 n) || ( ((px+1) == p0) && notMulOf px n) ) || ((p0+n) == px) || ((px+n) == p0) ) then True else False posOf :: Int -> Int -> [Int] -> Int posOf n acc (x:xs) = if n == x then acc else (posOf n (acc+1) xs) numOf :: Int -> Int -> [Int] -> Int numOf n acc (x:xs) = if n == acc then x else (numOf n (acc+1) xs) --X is not a multiple of Y notMulOf :: Int -> Int -> Bool notMulOf x y = if (x `mod` y) == 0 then False else True --board creation boardGen :: Int -> [Int] boardGen n = let ls = gen 0 (n*n) in rshuffle ls n shuffle :: [a] -> Int -> [a] shuffle [] _ = [] shuffle [x] _ = x : [] shuffle (x:xs) n = x : (last xs) : shuffle (take (n-2) xs) (n-2) rshuffle :: [Int] -> Int -> [Int] rshuffle xs n = let ls = (shuffle xs (n*n)) in (filter (\x -> if (x == 0 || x == 1) then False else True) ls) ++ [1, 0] gen :: Int-> Int -> [Int] gen x n = if (x < n) then x:(gen (x+1) n) else []